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
The output of the branch command above can be regex replaced with –
git push \1 : you the commands to run, just remove those lines referring to remotes that you want to keep.
When the subscribers to your remote next run prune the remote branches will auto-magically be removed from their histories, however their and your local branches will still exist. To list these run the following –
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
Once again the output of the branch command above can be regex replaced with –
git branch -d \1 you the commands to run, just remove those lines referring to remotes that you want to keep.
The -d option ensures that the branch your deleting is merged into the history…
The issues here can be avoided by making sure than when a branch is complete and it has made it into the "trunk" make sure you clean the history immediately.
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 that have been prunes run –
git remote prune origin --dry-run
That will give you the deletion list, just remove the –dry-run to do it for real.
In practice if you have cleaned up a repository then you will need to share the local branch delete commands and get your subscribers to run them alone with the prune commands. This will ensure that these old branches don’t re-appear next time any one of the subscribers pushes their history. Of course as mentioned before – managing branches as you go will greatly improve the day to day management of your repo. Your chosen GUI may also support automatic or selective pruning when you pull.
Whose eaten all the Prunes? Happy Gitting!