Git: Keep your repository tidy

When you're using feature branches, they will stack up if you don't delete them after the merge to master. Here's how to tidy them up.

Delete feature branches

Find already-merged branches by running

# On branch master
git branch --merged

You may safely delete each of the listed branches, because they point to commits that are contained in the history of your current branch (i.e. master).

git branch -d my/feature-branch # Delete feature branch locally
git push origin :my/feature-branch # Push *nothing* to the feature branch on origin = delete it

If there are many old branches you don't know anything about, listing information about the branches' most recent commit will help you make a decision.

Caution

Don't use GitLab's mass delete for merged branches!

GitLab provides a very convenient option to delete all your merged branches at once. However, this considers production a merged branch and deletes it.

Update your local copy

As described here, your local repository copy will keep branches that other people removed from origin (e.g. when deleting their feature branches as written above). Remove those stale branches with

git fetch --prune 
Dominik Schöler Over 8 years ago