Delete old deleted branches with git

When I use bash autocompletion in Git, it continues to show me branches of old remotes that I no longer have. When I do git branch -la , it shows those old remotes and branches, but git branch -l will not. A ls .git/refs/remotes/ also shows them. However, they are not present in my .git / config, and they are not displayed 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 also know that I can just re-clone the repo, but it just fools; -)

+60
git
Jul 04 '13 at 12:46 on
source share
5 answers

Git does not automatically delete (local) remote tracking branches if the branch is deleted in the remote repository. Also, before the V2.0.1 remote tracking branches were not deleted in some cases when you deleted the deleted file from the git configuration (see VonC answer).

To remove old branches of remote tracking (branches deleted in a remote repository) for one of your remote repositories, run

 git remote prune <remote> 

To git remote page or git remote :

undercut

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

With the --dry-run option, tell which branches will be trimmed, but do not actually trim them.

However, from your question, it seems that you manually deleted .git/refs/remotes/theoldremote , so git no longer knows about the remote repository to which the remote tracking branches belong. This is not how you should do it.

The usual way to delete a remote repository is to run

 git remote rm <remote> 

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

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

 git branch -rd <remote>/<branchname> 

You need the -r option to delete the remote branch.

+103
Jul 04 '13 at
source share

I use

 git push origin :remote_branch 

delete a branch from the server.

 git remote prune origin 

delete deleted links that no longer exist on the server

+14
Jul 04 '13 at 13:07 on
source share

Note: while git remote trimming is the answer, be aware that starting with git 2.0.1 (June 25, 2014), git remote rm starts by deleting the remote tracking branches .
So hopefully you should not clear old branches after git remote rm .

See commit b07bdd3 by Jens Lindstrom ( jensl )

remote rm : remove the remote configuration as the last

When removing a remote, delete the remote tracking branches before deleting the remote configuration.
Thus, if the operation fails or is canceled when the remote tracking branches are deleted, the command can be re-run to complete the operation .

+6
Jul 27 '14 at 18:58
source share

Do not delete anything in the branch to remove it:

git push remote :remote_branch

It's somewhere in the docs, but it's not entirely obvious.

Or didn’t I understand your question?

+4
Jul 04 '13 at 12:47 on
source share

Well, I did it. The problem was that the remotes no longer exist, but they are somewhere in the git database. I added remotes again and then did

 git remote prune theremote git remote rm theremote git gc --prune=now 

After that they will disappear from the list. Somehow I did not delete them correctly before I guess.

+4
Jul 04 '13 at 14:11
source share



All Articles