How do I clean both remote and local git branches on windows - windows

What is the best way to remove branch references that no longer exist and delete local branches already merged for git repos in Windows command prompt (i.e. not using bash)?

This will first remove any remote tracking references that no longer exist and then proceed to enumerate and remove already merged local branches. The exception is that current, develop and master branches will be kept.
git fetch -p && for /f "tokens=1,2" %b in ('git branch --merged ^| findstr /v /c:"master" /c:"develop" /c:"*"') do (git branch -d %b)

Related

Git annoyingly persistent in repo

I'm creating a project that covers frontend, backend, and mobile. I've set up git in the project root so all phases could be committed. However, I bootstrapped the frontend folder with create-react-app, which creates a project structure of its own (including git).
As it happens, when there's a git inside a git, it is interpreted as a subproject. I just wanted to commit them all to the same repository. I, therefore, proceeded to eliminate all git references from the frontend folder to have it only in the root.
I tried every trick I could think of and it still hasn't worked, the files are still tracked separately. Here's a list of commands I tried both in Powershell and Git Bash:
rm -rf .git
rm -rf .git*
find . | grep "\.git/" | xargs rm -rf
del /F /S /Q /A .git
rmdir .git
rm -rf .gitkeep
rd .git /S/Q
FOR /F "tokens=*" %G IN ('DIR /B /AD /S *.git*') DO RMDIR /S /Q "%G"
Any hints of how getting it the hell out of my folder?
Thanks!
Removing the .git is a good first step, but what you are missing is the gitlink (special entry in the index of your main repo referencing the SHA1 of the root tree of the subrepo)
You need to add:
git rm subrepoRootFolder # no trailing /
git commit -m "remove subrepo gitlink"
Then you can git add ., and all files within subrepoRootFolder will be (finally) added to your main repository. Then commit and push.

How to add empty folders when importing from SVN to Git under Windows?

I need to migrate multiple repositories from SVN to Git under Windows. Part of the repositories has empty folders, they are critical for projects and these folders cannot be deleted without breaking the project.
I tried the git svn clone command with the keys --preserve-empty-dirs --placeholder-filename = .gitkeep but this does not work at all.
As a result, the folder is not added to the commit history - this is a big problem because it is impossible to updade to the old version. After full migration the folder is also not added.
I tried to make a crutch to the mechanism, but I do not understand how to make it correctly:
This code creates zero-size files that Git does not process:
for /f "delims=" %d in ('dir /s /b /ad ^| sort /r') do (fsutil file createnew %d/.gitkeep 0 && echo.>%%d/.gitkeep)
This code creates files with a size of 2 bytes:
for /f "delims=" %d in ('dir /s /b /ad ^| sort /r') do (echo.>%d/.gitkeep)
In both cases, there is a problem with the fact that files are created in all folders, and not just empty ones. In addition, I lack an understanding of how to exclude the .git folder from processing.
Please, help.
No way, but I wrote a crutch under Ubuntu 18.04, it works successfully in WSL:
echo The crutches for git svn clone --preserve-empty-dirs command, because it is not working properly.
find . -type d -empty -not -path "./.git/*" -exec echo blablabla>.gitkeep \;
git add --all

Create archive of modified files in GIT via batch file

I'm using this git command to create an archive of the files modified within a specific commit:
git archive -o update.zip HEAD $(git diff --name-only COMMITID^)
where COMMITID is the id of the commit I'm looking to archive. This works fine from the command line but I would like to run it from a batch file. Here are the contents of the batch file I'm using:
git archive -o update.zip HEAD $(git diff --name-only %1^^)
where %1 is the commit id being passed in via SourceTree. The problem I'm having is that this command when run from a batch file returns the following error:
error: unknown option `name-only'
I'm guessing there may be some character escaping issues going but I'm unable to find what is specifically breaking.
How would I write that git command so that it will work from a batch file?
UPDATE
I tried removing the --name-only option and received the following error when trying the batch script via SourceTree:
fatal: path not found: $(git
Hopefully that helps narrow down what may be going on.
FURTHER UPDATE
Turns out my syntax was wrong for grabbing only the modified files from a specific commit but using msandiford's answer I came up with this batch file script that works perfectly:
setlocal enabledelayedexpansion
set output=
for /f "delims=" %%a in ('git diff-tree --no-commit-id --name-only -r %1^^') do ( set output=!output! "%%a" )
git archive -o update.zip HEAD %output%
endlocal
Assuming you need a windows batch file, and not a bash script, here is a minimal batch file that may do what you want:
setlocal enabledelayedexpansion
set output=
for /f "delims=" %%a in ('git diff --name-only %1^^') do ( set output=!output! "%%a" )
git archive -o update.zip HEAD %output%
endlocal
It works by collecting all lines of the output of the git diff ... command into an environment variable, and then using this to perform the git archive ... operation.
Documentation for the Windows for command is here, including the /f switch. Documentation for the setlocal command is here.

How to write a batch file for svn commit and update

so far I have used Tortoise SVN to commit and update folders under version control. When I commit I check "all" in the GUI dialog so that deletions as well as additions are committed.
Now I have more and more folders under version control and I would like to have a batch file for committing and updating all of them.
So far I have experimented with the command line and found this:
svn add . --force
svn commit -m"Adding missing files"
This adds new files but does not reflect any deletions.
Could you please help me with the batch files? It would make my work a lot easier but I am really too unexperienced with SVN/batch files to do this on my own...
I use Win7x64 and Tortoise SVN 1.7.12 with the command line extension.
Thank you!
I think I figured something out using gammay's and this input:
cd "C:\Users\User\Desktop"
for /f "usebackq tokens=2*" %%i in (`svn status ^| findstr /r "^\?"`) do svn add "%%i %%j"
for /f "usebackq tokens=2*" %%i in (`svn status ^| findstr /r "^\!"`) do svn delete "%%i %%j"
svn commit -m "Commit via Batch"
And
cd "C:\Users\User\Desktop"
svn update
and repeatedly for different paths!
Thank you :)
Firstly, which svn command line tools do you use? You can use CollabNet for 'svn' commands.
Secondly, to delete files, you need to checkout existing files from svn, then use svn delete and then svn commit.
Your question is not clear - if this doesn't answer your question, please provide a few more details.
Edited to answer asker's requirement (in comments below):
OK. What you want is a script which will find the new files in the folder and add them to SVN automatically and find deleted files in folder and delete them from SVN too. I can tell you this is a dangerous as undesired files can get added/deleted.
Still, if you want to go ahead with this script this is what the script can do:
Run svn status which displays missing (deleted) files and unknown (to be added) files
! FileA [Missing - deleted]
? FileD [Unknown - to be added]
Parse the output to find the ! files and run svn delete on these files
svn delete FileA
Parse the output to find the ? files and run svn add on these files
svn add FileD
svn commit
This commits the above deleted & added files and also any modifications.
If you do not want to commit modifications, commit individual added/deleted files

Subversion; checking out only trunk for multiple projects

I have a directory structure in svn like this:
Project A
branches
tags
trunk
Project B
branches
tags
trunk
...
I want to checkout only trunk directories for all these projects. There are about 400 of these kind of projects so checking out trunk manually won't be an option.
My first guess would be to use svn list, but my shell scripting skills are not up to par and I'm sure how to create the appropriate directories and append 'trunk' and do a checkout.
Anyone willing to point me in the right direction?
TL:DR;
svn list produces something like 'project_a'.
I want to checkout 'project_a/trunk' into 'project_a'.
You can store the list of projects to a file (projects_list), then run this script:
for p in $(cat projects_list); do
mkdir $p
svn co "$url/$p/trunk" $p
done
Here is a way to do it by using the depth flag:
echo Getting Projects the folder structure
svn co http://www.therepo.com/projectsParentFolder --depth immediates
echo Getting the structure for each Project
for /f %%f in ('dir /b .\projectsParentFolder') do svn co http://www.therepo.com/projectsParentFolder/%%f .\projectsParentFolder\%%f --depth immediates
echo Getting the trunk for each Project
for /f %%f in ('dir /b .\projectsParentFolder') do svn co http://www.therepo.com/projectsParentFolder/%%f/trunk .\projectsParentFolder\%%f\trunk --depth infinity

Resources