Git-svn: Bulk removing orphaned remote branches
A project in SVN I'm working on (via git-svn) has frequently-created branches that are then --reintegrated with trunk, and then deleted.
开发者_如何学编程Right now the project has about 10 branches that have not been deleted, but in git, git branch -r shows about 50.
I can remove these one at a time, checking whether they still exist in the svn repository but it's slow and tedious. Is there a way to sync my list of git remote branches with the svn repo?
This is a quick-n-dirty solution I made in a few minutes. It makes use of nice regex patterns that are not available everywhere.
This grabs a clean list of branches. I remove formatting spaces at the beginning of each line, and I'm ignoring tags for now:
git branch -r | sed 's|^[[:space:]]*||' | grep -v '^tags/' > git-branch-list
I grab a similar list of branches from svn, again removing formatting and trailing forward-slashes:
svn ls svn://path/to/branch/dir/ | sed 's|^[[:space:]]*||' | sed 's|/$||' > svn-branch-list
I diff the lists, find the lines that don't exist in the svn list anymore, remove the diff formatting, get rid of the "trunk" branch (which is a git-svn convenience) and save the results to a new list:
diff -u git-branch-list svn-branch-list | grep '^-' | sed 's|^-||' | grep -v '^trunk$' | grep -v '^--' > old-branch-list
Now I just perform standard branch removal procedures for git-svn:
for i in `cat old-branch-list`; do git branch -d -r "$i"; rm -rf .git/svn/refs/remotes/"$i"; done
There's probably better ways of doing this, but it works. Someone else is welcome to take this and improve on it.
In order to get more relevant answer, please state what your branch list looks lie (git branch -a
) and which refs you want gone.
In absense of this, i'll just make something up so you can adapt:
Remove the obsolete refs (presumably after manually dropping a remote?):
git for-each-ref --format="%(refname)" refs/obsoleteremote/ |
xargs -n 1 git update-ref -d
Garbage collect all unreferenced objects with
git gc --prune=now
or if your git-gc is not new enough to support arguments to --prune, use
git repack -ad; git prune
Here's my solution. Need to replace trailing slashes used by Subversion in branch names and leading spaces in Git branch listing. The IFS should handle spaces within the branch names themselves.
cd $git_repo
IFS="$(printf '\n')"; diff <(for i in `git branch -r`; do echo "$i" | sed 's/^ //g'; done | sort) <(for i in `svn ls http://host/my/svn/dir/branches`; do echo "$i" | sed 's/\/$//g'; done | sort) | grep '<' | cut -d' ' -f2 | xargs /usr/bin/git branch -r -d
精彩评论