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
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
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)
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.Have you tried this on branches that were not merged?
Lets make sure we are both talking about the same situation.
If this is in fact the situation we are discussing then here are some thoughts
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.
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
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.