从 Git 删除旧的远程分支

当我在 Git 中使用 bash 自动完成时,它会不断地向我显示我不再拥有的旧远程分支。当我做一个 git branch -la它显示那些旧的遥控器和分支,而一个 git branch -l不会。ls .git/refs/remotes/也显示了它们。但是,它们不存在于我的 . git/config中,也不会在我运行 git remote show时显示出来。

因此,我如何摆脱它们,因为我的自动完成列表太长了。

我已经试过了:

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

我也知道我可以复制回购协议,但那只是作弊; -)

39658 次浏览

Push nothing to a branch to delete it:

git push remote :remote_branch

It's somewhere in the docs but it isn't really obvious.

Or did I misunderstand your question?

I use

git push origin :remote_branch

to remove a branch from server.

git remote prune origin

to remove remote references which do not exist on server anymore

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.

Ok I've got it. The problem was that the remotes don't exist anymore, but they do somewhere in the git database. I re-added the remotes, then did

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

After that they disappear from the list. Somehow I didn't remove them correctly before I guess.

Note: while git remote prune is the answer, know that, starting with git 2.0.1 (June 25th, 2014), a git remote rm starts by removing the remote tracking branches.
So hopefully, one shouldn't have to cleanup old branches after a git remote rm.

See commit b07bdd3 by Jens Lindström (jensl)

remote rm: delete remote configuration as the last

When removing a remote, delete the remote-tracking branches before deleting the remote configuration.
This way, if the operation fails or is aborted while deleting the remote-tracking branches, the command can be rerun to complete the operation.


But if you have to, a simple git fetch can be enough, provided you have set first:

git config --global fetch.prune true
cd /path/to/repo
git config remote.origin.prune true

I was getting confused when remote branches that had been deleted on the server side were still appearing when I ran:

$ git branch --all

The following command fixed this for me (on git version 2.25.0):

$ git remote update --prune