git – delete local tracking branches

(Just a summary of Stack Overflow “How do you Remove an Invalid Remote Branch Reference from Git?”).

To delete a local tracking branch (without deleting the remote branch), do:

git branch -rd remote/branch

And of course to delete the remote branch:

git push remote :branch

Occasionally a gc will help, but usually shouldn’t be used:

git gc --prune=now

So, when would you want to use this? Let’s say the repository you’re tracking has a lot of branches (eg the Linux Kernel). You start tracking branch “foo”, do some work with it, merge some of it in to your branch “bar”, then push “bar” up to the remote repository. Or, you’ve got a whole lot of dev branches you’ve merged pushed to your backup repository and also merged into your master branch.

In either case you’ve got a collection of tracking branches you don’t want to see anymore, so clean them up:

% git branch -r

% git branch -rd soniah/dev.a
Deleted remote branch soniah/dev.a (was deadbeef).

% git branch -rd soniah/dev.b
Deleted remote branch soniah/dev.b (was deadbeef).

# now remote branches is cleaner:
% git branch -r

Share This


Leave a Reply

Your email address will not be published. Required fields are marked *


You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>