git remote prune origin does not delete the local branch even if its upstream remote branch is deleted

╄→гoц情女王★ 提交于 2019-12-03 05:49:14
VonC

The git remote prune command only deletes the remote tracking branches in the remotes/origin namespace.

Not the local branches.
The usual practice is to delete only merged local branches.

git branch (even with -vv) only shows local branches.
A branch can have a slash in its name

A remote tracking branch is in the remotes/origin namespace, and record what was fetch.
An upstream branch is a remote branch associated to a local branch in order for said local branch to know where to push.

git remote prune correctly remove the remote tracking branch, which happens to be the upstream branch for the local bugfix/encrdb_init branch.
That is why you see origin/bugfix/encrdb_init: gone: the remote tracking branch is gone.


The OP adds:

from the description, it seemed like git remote prune origin is doing this exactly. But it doesn't seem to be working for me.

No, the description does not mention local branches.

Deletes all stale remote-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>".

<name> here is the name of the remote repo referenced by git remote -v.
Usually "origin".
git remote prune will delete branches registered in remotes/origin (not the "remote(s)"). It will not delete local branches.

To "safely" delete local branches, you should:

That last option is flaky:

  • I prefer using git branch -d instead of -D, in order to delete only branches that are already merged.
  • you might end up deleting branches with a commit message contains the string ": gone"

A better way to list those branches is:

git branch --list --format "%(if:equals=[gone])%(upstream:track)%(then)%(refname)%(end)"
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!