Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove old remote branches from Git

Tags:

git

When I use bash autocompletion in Git, it keeps showing me branches of old remotes that I don't have anymore. When I do a git branch -la it shows those old remotes and branches while a git branch -l won't. A ls .git/refs/remotes/ also shows them. However, they are not present in my .git/config and neither are they shown when I run git remote show.

So how do I get rid of them because my autocomplete list is too long right now.

I have already tried:

git reflog expire --expire=now --all git gc --prune=now rm .git/refs/remotes/theoldremote git remote prune theoldremote 

I'm also aware of the fact that I can just re-clone the repo but that's just cheating ;-)

like image 601
Alex Avatar asked Jul 04 '13 12:07

Alex


People also ask

How do I remove old branches in git?

The easiest way to delete local Git branches is to use the “git branch” command with the “-d” option. The “-d” option stands for “–delete” and it can be used whenever the branch you want to clean up is completely merged with your upstream branch. $ git branch -d release Deleted branch feature (was bd6903f).

Should I delete old branches git?

They're unnecessary. In most cases, branches, especially branches that were related to a pull request that has since been accepted, serve no purpose. They're clutter. They don't add any significant technical overhead, but they make it more difficult for humans to work with lists of branches in the repository.

How do I delete all remote branches?

Let's break this command: First we get all remote branches using the git branch -r command. Next, we get the local branches not on the remote using the egrep -v -f /dev/fd/0 <(git branch -vv | grep origin) command, Finally we delete the branches using the xargs git branch -d command.

Does git prune remove remote branches?

git fetch --prune is the best utility for cleaning outdated branches. It will connect to a shared remote repository remote and fetch all remote branch refs. It will then delete remote refs that are no longer in use on the remote repository.


1 Answers

Git does not delete the (local) remote-tracking branches automatically if the branch was deleted in the remote repository. Additionally, before V2.0.1 remote-tracking branches were in some cases not deleted when you removed the remote from your git config (see VonC's answer).

To delete stale remote-tracking branches (branches that were deleted in the remote repository) for one of your remote repositories, run

git remote prune <remote> 

To cite the man page or git remote:

prune

Deletes all stale tracking branches under <name>. These stale branches have already been removed from the remote repository referenced by <name>, but are still locally available in "remotes/<name>".

With --dry-run option, report what branches will be pruned, but do not actually prune them.

However, from your question it seems you manually removed .git/refs/remotes/theoldremote, so Git no longer knows about the remote repository that the remote-tracking branches belonged to. That's not how you're supposed to do it.

The normal way to remove a remote repository is to run

git remote rm <remote> 

This will remove the remote from your .git/config, and will delete the remote-tracking branches.

If you just delete the directory under .git/refs/remotes/, the branches will remain behind. Then you will need to remove them manually:

git branch -rd <remote>/<branchname> 

You need option -r to delete a remote branch.

like image 136
sleske Avatar answered Sep 24 '22 00:09

sleske