【问题标题】:'git branch -av' showing remote branch that no longer exists'git branch -av' 显示不再存在的远程分支
【发布时间】:2012-02-04 16:41:25
【问题描述】:

这可能是一个愚蠢的问题,但我是 git 的新手,并且看到一个不再存在的远程分支。

$ git branch -a
* master
  remotes/origin/master
  remotes/origin/production

我不相信生产分支远程存在并且无法弄清楚为什么它仍然在本地显示。如何删除/删除此分支?以下是尝试删除它的样子:

$ git push origin :production

error: unable to push to unqualified destination: production
The destination refspec neither matches an existing ref on the remote nor
begins with refs/, and we are unable to guess a prefix based on the source ref.
error: failed to push some refs to 'git@IP:puppet.git'

我可以签出所谓的远程生产分支,但得到这个:

$ git checkout origin/production
Note: checking out 'origin/production'.

You are in 'detached HEAD' state. You can look around, make experimental
changes and commit them, and you can discard any commits you make in this
state without impacting any branches by performing another checkout.

If you want to create a new branch to retain commits you create, you may
do so (now or later) by using -b with the checkout command again. Example:

  git checkout -b new_branch_name

HEAD is now at c323996... added powerdns module, no really

我不知道我在做什么。任何帮助将不胜感激。

【问题讨论】:

标签: git


【解决方案1】:
git remote prune origin

是的,只需添加您可以使用--dry-run 选项,该选项会报告将从您的本地存储库中修剪哪些分支,但实际上并未修剪它们

git remote prune origin --dry-run

【讨论】:

    【解决方案2】:

    所以有两个问题。在这两种情况下,请记住 Git 是分布式的。

    首先。当你做类似的事情时

    $ git 分支 -a

    该操作是在您的本地仓库而不是远程计算机上执行的。换句话说,您的本地存储库正在报告所有已知的分支。这些可能是本地分支(如“master”)或从远程获取的远程分支。自上次获取以来,远程仓库的“生产”分支已更改,但您的本地仓库不知道这一点。 manojlds 的答案是正确的。运行

    $ git remote prune origin

    删除陈旧的分支。

    'git push origin :production' 命令用于从远程计算机的 git 存储库中删除分支。不是您的本地仓库。在这种情况下,其他人已经删除了远程计算机的 git repo 上的分支,所以您会看到此错误消息。

    这是一个总结了这些命令的link

    第二个问题涉及结帐。

    签出分支时,您希望从 本地 分支而不是远程分支执行此操作。这就是为什么您会收到有关分离的 HEAD 的错误。 git-notes repo 对问题的详细解释很好。基本上关键词是

    但是,当您签出任何不是正确的本地分支名称的内容时,HEAD 不再是对任何内容的符号引用。相反,它实际上包含您要切换到的提交的 SHA-1 哈希(提交 ID)。

    现在,如何检出本地分支,与远程分支相同?

    很简单,您在结帐远程分支时创建一个本地分支。

    $ git checkout -b my_local_branch 起源/生产

    【讨论】:

      【解决方案3】:

      你必须这样做:

      git remote prune origin
      

      【讨论】:

      • 感谢工作!你能详细说明幕后发生的事情吗?
      • 这些是您本地仓库中的远程跟踪分支,如果在远程仓库中删除了这些分支,您必须清除它们。
      • 我很少能如此快速地找到并实施如此简单的问题和答案。
      • 非常棒的一个。在我的本地,我看到了与代码库 repo 无关的奇怪分支。当我运行这个命令时,它调整了我的本地原始分支,然后我添加了上游 master。谢谢
      猜你喜欢
      • 2012-09-01
      • 2016-06-14
      • 1970-01-01
      • 2019-05-18
      • 1970-01-01
      • 2020-05-22
      • 2023-04-10
      • 2021-04-15
      • 2018-07-15
      相关资源
      最近更新 更多