Monday, January 21, 2013

git branch cleanup -- show commits that need to merge

I'm cleaning up my feature branches. I want to look at any dangling commits that only exist in the branches.

First Pass: Remove branches that have already been merged into master

These branches can be detected via git branch --merged, pipe this to git branch -d and delete them.
% git branch|wc -l
93

% git branch --merged|wc -l
24

# not quite right, want to skip current branch
% git branch --no-color --merged | grep -v '\*' | xargs -n 1 git branch -d 
...
Deleted branch deleteme (was 4358c15).

% git branch --merged
* master

% git branch|wc -l
70

Second Pass: Find dangling commits

Look at the dangling commits in the remaining branches and see if they are important. If we want the commits, we'll merge them into master. If not, we'll force delete the branch with git branch -D.

Use git merge-base to find the most recent ancestor between this branch and master.

# pick a branch
% git checkout makefile_dirs
% git merge-base --all HEAD master
9906334e464c6e93103b786672b14c31c27f8df8

#The trailing ".." is important, as this specifies a range of commits
% git log --pretty=oneline 
9906334e464c6e93103b786672b14c31c27f8df8..

f48d27a93239558d5737652bc0e397d99d0f43fc improves directory creation in makefile

#We can merge those latter two steps into:
% git log --pretty=oneline $(git merge-base --all HEAD master)..

f48d27a93239558d5737652bc0e397d99d0f43fc improves directory creation in makefile
Including the prior commit to the log will help determine how old this branch is. We'll add a ^ to look at the parent of the branch commit.
% git log --pretty=oneline $(git merge-base --all HEAD master)^..

f48d27a93239558d5737652bc0e397d99d0f43fc improves directory creation in makefile
9906334e464c6e93103b786672b14c31c27f8df8 passes site_id and rad_id through to ou