top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

How to delete all merged branches in GIT?

+2 votes
675 views

I tend to accumulate lots of branches as I'd do one branch per feature. When cleaning up, I'd like to
delete all branches, which have been merged.

I could use

 $ git branch -d (which was merged already?) ^C
 $ git branch --merged # let's find out
 ...
 $ # Now run git branch -d with each of the branches.

This kind of question has already been discussed,
http://stackoverflow.com/questions/6127328/how-can-i-delete-all-git-branches-which-are-already-merged
suggests: git branch --merged | grep -v "*" | xargs -n 1 git branch -d

I could think of:

 $ git branch -d --merged # no need to specifiy a branch iff --merged is given with -d
 $ git branch --delete-merged # comes as an new extra option, doesn't clutter other commands
 $ git branch -d keyword-for-all-merged-branches

Before starting such a feature, I'd like to hear input of others.

posted Feb 16, 2014 by Majula Joshi

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

+1 vote

Maybe someting like:
- check you're on master
- only consider branches that begin with first-last-/

head=$(git rev-parse --verify HEAD)
master=$(git rev-parse --verify master)

if test "$head" != "$master"; then
 echo "You must be on master"
 exit 1
fi

git branch --merged |
sed -n -e 's/ *//' -e '/^[^/][^/]//p' |
xargs git branch -d
answer Feb 16, 2014 by anonymous
Similar Questions
0 votes

I need to provide a list of all branches I have merged into develop since a given date.
Can you recommend a git command that will do this?

+2 votes

I am learning, and experimenting. I was experimenting with "git merge", forgot the "--no-commit", and discovered the hard way that aborting the editor that comes up without saving does NOT abort the merge.

And git merge --abort fails, because the merge succeeded.

Git Gui has a way to amend the comment of the last merge, but no way to undo it. I figure this should be simple, but I don't know how.

+2 votes

Let's say I have two identical branches: master and topic. In master I remove some code, i.e. function bar(). In topic I do the same (commit) and after some time I realize I need bar() and revert previous commit
with removal.

So I end with master with no bar() and topic with bar() in its original state. When I merge I get code without bar() and no merge conflict (recursive or resolve strategies). Is it possible to detect such situations as conflicts? When bar() is C++ virtual there is no possibility to catch this with compiler.

+2 votes

I am getting some unexpected results from a merge and I'd like to understand why.

I have two commits X and Y that I want to merge.

git merge-base X Y # yields B
git diff B X -- F # is empty
git diff B Y -- F # contains the change I want merged
git rev-list X ^B -- F # is empty
git rev-list Y ^B -- F # contains one commit

git checkout X
git merge Y

fails with fixable merge conflicts on other files, but uses X's copy of F instead of Y's. I was expecting it to use Y's copy of F, since only Y has modified F since B. What could cause this?

+1 vote

At some point I added a large file into a git repository. It now exists on multiple branches, possibly with some changes to it. I'd like to remove it from git, but leave its current form (say the one on the master branch) on the file system.

I tried (on a dummy git archive)

git filter-branch --index-filter 'git rm --cached --ignore-unmatch bigfile' master branch1 branch2

That, however, does not leave a copy of bigfile on the file system.It isn't clear to me why not, though the description of the --tree-filteroption to filter-branch (I'm using the --index-filter option, but is is "similar") states:" (new files are auto-added, disappeared files are auto-removed ... )".
Is there a direct way to do what I want, with git? I've found similar requests;none of the responses point out that the above command actually deletes the file from the file system.

...