Just a quick Git usage note to self on this…
From time to time a branch will inevitably end up redundant in your history as it is merged into the ultimate target branch (referred to here as live).
First checkout your live branch
git checkout live
To determine candidates to remove. You can find all merged branches by running -
git branch -r --merged
Note – this will likely show your current head too, so review the results carefully! Also the branches are listed prefixed with the remote name – this goes before the colon and is not needed in the branch name to delete.
To remove your finished branch you need to push a deletion – i.e.
git push origin :branch_to_delete
When the subscribers to your remote next run prune the branches will auto-magically be removed from their histories, however their local branches may still exist. To clean these up you can use the following commands -
git branch -a --merged
This will list both remotes and local branches that are merged as before. This time we're only interested in the local branches.
For each of the local branches that need removing you can then run -
git branch -d branch_name
The -d option ensures that the branch your deleting is merged into the history…
This issue can widely be avoided by making sure than when your finished with a branch and it has made it into the "trunk" make sure it is mopped up.
prune
Sometimes branches are deleted from a remote repo. By default, git fetch will not remove any remote-tracking branches that have been deleted on the remote repo. Running git remote prune REMOTENAME will delete these tracking branches.
So the last command you need to run to remove remotes others have pruned is something like …
git remote prune origin --dry-run
That will give you the deletion list, just remove the –dry-run to do it for real.
Whose eaten all the Prunes? Happy Gitting!



