DEV Community

Cover image for Git garbage: what if your local have a lot of old branches?
Fabio Peluso
Fabio Peluso

Posted on

Git garbage: what if your local have a lot of old branches?

In the last few days I've seen that my local git repository had a lot of old branches that are no more on remote. So I asked Google this question:

git how to remove branch locally and not remotely

And then I come out with a solution by reading some stackoverflow stuff.

First of all: you check the branches in your local repository to discover if they are gone on the remote; you can use the following command:

git branch -vv
Enter fullscreen mode Exit fullscreen mode

This will give you a list of all branches on your local repository, the current commit of the branch and the corresponding remote branch if there is one and the indication gone that indicates that the branch was deleted on remote.

Now you may want to delete all the branches that are on your local but are no longer present on remote, but if there are to many of them you will need an automated procedure. I've find that one usefull:

git fetch -p && git branch -vv | awk '/: gone]/{print $1}' | xargs git branch -d
Enter fullscreen mode Exit fullscreen mode

After that my local repository was finally cleared of all the dead branches.
And you? Have you ever faced this problem? How did you faced it?
Leave a comment if you have!

Top comments (5)

Collapse
 
jessekphillips profile image
Jesse Phillips

git fetch origin --prune

First I want the remote tracking branches to be cleaned up with the latest information about the remote repo.

git switch --detach origin/master

Navigate to an important branch which resides on the remote.

git branch --merged

List all of the branches which are a parent to my current branch.

Looking at the results of git branch -vv I'm probably sticking to the above approach.

Collapse
 
fpeluso profile image
Fabio Peluso

Have you tried this on branches that were not merged?

Collapse
 
jessekphillips profile image
Jesse Phillips

Lets make sure we are both talking about the same situation.

  • There is a local branch (not a tracking branch) in your repo
  • The branch has not been merged
  • The corresponding remote branch has been deleted (gone)
  • The branch on the remote was not merged

If this is in fact the situation we are discussing then here are some thoughts

  1. We may not care about the work done for that branch and it can be removed
  2. The remote branch was accidentally deleted and so we want to get those back up there and merged in

I think that once you have cleaned up the work that has been merged, it becomes an exercise in reviewing all branches and determining if they are desired and press to get items merged in that are ready. If you aren't ready to do such a review, leave the branches until you are.

Thread Thread
 
fpeluso profile image
Fabio Peluso

Yeah, almost! The forth bulletpoint is not always there. I really appreciate your comment, it is a very clean way to deal with the situation

Thread Thread
 
jessekphillips profile image
Jesse Phillips

If the branch was merged on the remote, then my approach will find it. It is the reason I switch to origin/master. Any other branch can also be used, which checks for what is merged into that branch.

Have a good Thanksgiving if you celebrate.