git remote prune origin即使删除其上游远程分支也不会删除本地分支 [英] git remote prune origin does not delete the local branch even if its upstream remote branch is deleted

查看:600
本文介绍了git remote prune origin即使删除其上游远程分支也不会删除本地分支的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的常见用例,我克隆了一个存储库,签出一个分支,进行了一些代码更改,多次提交,然后在其稳定后,推送到远程,最终分支被合并并删除了.我留下了一个本地分支,而上游却消失了.

This is a common use-case for me, I clone a repository, checkout a branch, do some code changes, make multiple commits, then when its stable, i do a push to remote, eventually the branch gets merged and deleted. and I'm left with a local branch with upstream gone.

我一直在寻找删除所有此类分支的安全方法.从描述中可以看出,git remote prune起源确实在执行此操作.但这似乎对我不起作用.

I was looking for a safe way of deleting all such branches. from the description, it seemed like git remote prune origin is doing this exactly. But it doesn't seem to be working for me.

看到以下行为,分支encrdb_init已从remote中删除,但git remote prune origin命令似乎并未对其进行修剪.我不确定为什么.

Seeing the following behaviour, the branch encrdb_init has been deleted from remote but the git remote prune origin command does not seem to prune it. I am not sure why.

$ git branch
  bugfix/encrdb_init
  * master
$
$ git remote prune origin
$
$ git checkout bugfix/encrdb_init
  Switched to branch 'bugfix/encrdb_init'
  Your branch is based on 'origin/bugfix/encrdb_init', but the upstream 
  is gone.
  (use "git branch --unset-upstream" to fixup)
$
$ git branch
  bugfix/encrdb_init <<< shouldn't this have been pruned?
  * master

供参考,添加git remote show origin

$ git remote show origin
* remote origin
  Fetch URL: <redacted>
  Push  URL: <redacted>
  HEAD branch: master
  Remote branches:
    SSL_test                                                  tracked
    addNodeFix                                                tracked
    autoprefix                                                tracked
    release/1.0.2                                             tracked
  Local branches configured for 'git pull':
    bugfix/encrdb_init          merges with remote bugfix/encrdb_init
    master                      merges with remote master
    release/1.0.2               merges with remote release/1.0.2
  Local refs configured for 'git push':
    master                 pushes to master                 (up to 
date)
    release/1.0.2          pushes to release/1.0.2          (up to 
date)

$ git branch -vv
* bugfix/encrdb_init          341a078c [origin/bugfix/encrdb_init: gone] <redacted comment>`

推荐答案

git remote prune 命令仅删除remotes/origin名称空间中的远程跟踪分支.

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分支(即使带有-vv)也仅显示 local 分支.
分支的名称中可以有斜杠

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

remotes/origin名称空间中有一个远程跟踪分支,并记录获取的内容.
上游分支 是与本地分支相关联的远程分支,以使所述本地分支能够知道要在哪里推送.

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正确删除了远程跟踪分支,该分支恰好是本地bugfix/encrdb_init分支的上游分支.
这就是为什么您看到origin/bugfix/encrdb_init: gone:远程跟踪分支已消失的原因.

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.

OP添加:

从描述中的

看来,git remote prune origin正是在这样做.但这似乎对我不起作用.

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.

删除<name>下的所有过时的远程跟踪分支. 这些陈旧的分支已从<name>引用的远程存储库中删除,但仍在"remotes/<name>"中本地可用.

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>这是git remote -v引用的 remote 存储库的名称.
通常为"origin".
git remote prune将删除在remotes/origin中注册的分支(不是远程").它不会删除本地分支.

<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:

  • either considered the ones that are merged locally, for instance merged to master:

git fetch -p && git branch -d $(git branch master --merged | grep master -v)

  • 或者,如果您确实要立即删除上游分支为已消失"的那一个:

    git fetch -p && for branch in `git branch -vv | grep ': gone]' | awk '{print $1}'`; do git branch -D $branch; done
    

  • 最后一个选择是片状的:

    That last option is flaky:

    • 我更喜欢使用git branch -d而不是-D,以便仅删除已经合并的分支.
    • 您可能最终会删除带有包含字符串": gone"的提交消息的分支
    • 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"

    列出这些分支的更好方法是:

    git branch --list --format "%(if:equals=[gone])%(upstream:track)%(then)%(refname)%(end)"
    

    这篇关于git remote prune origin即使删除其上游远程分支也不会删除本地分支的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆