Git house cleaning

I use *Git* for several years and there are tons of articles out there about how to create new branch, pull, fetch, merge, conflict resolve, etc…

I looked at how we are doing with branches on current project – holds over 1M of code lines and +10 years of its history – I was surprised that we had almost 2 000 branches (features, patches,….). We don’t follow the standard git flow (yeah I know we should, but some in our team thinks that for  current project it is better how we do it now).  So as you can see there are many branches which needs to be cleaned up and GIT helps a lot with that.

Some Git commands which are useful for cleanup:

Clean of local branches which has been deleted on remote server:

git fetch --prune

list of all branches (local & remote )

git branch --all

List branches which are merged to remote master:

git branch --all --merged origin/master

Result of above command can be deleted

Delete local branch:

git branch -d branch_name

Delete branch from remote “origin” :

git push origin --delete branch_name

Show list of branches which are not merged into remote master branch:

git branch --all --no-merged origin/master

Results from above command are not merged into master. You need to check whether those branches are new feature/patch branches (still in work in progress) or they were not merged because you feature/patch is not needed any more (absolete and can be deleted) or you simply forget to merged them into your master (which should not happen by using tools like “git flow”)

You can scripts to combine of commands together. Let’s say we want to see last commit day and author to find out whether branch is still under development or not.

for branch in `git branch -r --merged origin/master | egrep -v 'HEAD|master'`; do echo -e `git show --format="%ci %cr %an" $branch | head -n 1` \\t$branch; done | sort -r

Similarly you can create scripts, which deletes not used merged branches and many other things you need.