' git branch -av'显示不再存在的远程分支

时间:2012-01-07 01:18:00

标签: git

这可能是一个愚蠢的问题,但我是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

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

3 个答案:

答案 0 :(得分:294)

你必须这样做:

git remote prune origin

答案 1 :(得分:48)

所以有两个问题。在这两种情况下,请记住Git已分发。

首先。当你做

之类的事情
  

$ git branch -a

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

  

$ git remote prune origin

删除过时的分支。

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

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

第二个问题涉及结账。

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

  

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

现在,如何签出一个与远程分支相同的本地分支?

很简单,您可以在结账远程分支时创建本地分支。

  

$ git checkout -b my_local_branch origin / production

答案 2 :(得分:16)

git remote prune origin

是对的,只是添加你可以使用--dry-run选项,报告将从你的本地仓库修剪哪些分支,但实际上并没有修剪它们

git remote prune origin --dry-run