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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8766525/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 12:40:07  来源:igfitidea点击:

'git branch -av' showing remote branch that no longer exists

git

提问by CarpeNoctem

This is probably a dumb question, but I'm brand new to git and am seeing a remote branch that no longer exists.

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

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

I don't believe the production branch exists remotely and can't figure out why it still shows locally. How can I delete/remove this branch? Here's what an attempt to remove it looks like:

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

$ 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'

I can checkout the supposedly remote production branch but get this:

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

$ 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

I have no clue what the heck I'm doing. Any help would be appreciated.

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

回答by manojlds

You have to do:

你必须要做:

git remote prune origin

回答by Pablo Maurin

So there are two problems. In both cases, remember Git is distributed.

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

First. When you do things like

第一的。当你做这样的事情时

$ git branch -a

$ git 分支 -a

the operation is performed on your local repo NOT the remote computer. In other words, your local repo is reporting all the branches that is knows about. These could be local branches (like 'master') or remote branches that it has fetchedfrom a remote. Since the last fetch, the 'production' branch of the remote repo has changed, but your local repo does not know this. The answer from manojlds, is correct. Run

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

$ git remote prune origin

$ git remote prune origin

to remove stale branches.

删除陈旧的分支。

The 'git push origin :production' command is used for deleting the branch from the remote computer's git repo. Not your local repo. In this case, someone else has already deleted the branch on the remote computer's git repo, so you see this error message.

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

Here is a linkthat summarizes these commands.

这是一个总结这些命令的链接

The second problem deals with checkout.

第二个问题涉及结帐。

When checking out a branch, you want to do so from a localbranch, not the remote branch. That is why you get the error about a detached HEAD. The git-notes repohas a good explanation of the problem in gory detail. Basically the key phrase is

签出分支时,您希望从本地分支而不是远程分支执行此操作。这就是为什么您会收到有关分离的 HEAD 的错误的原因。该混帐票据回购在血淋淋的细节问题的一个很好的解释。基本上关键短语是

However, when you checkout anything that is not a proper, local, branch name, then HEAD is no longer a symbolic reference to anything. Instead, it actually contains the SHA-1 hash (the commit id) of the commit you are switching to.

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

Now, how to check out a local branch, that is the same as the remote branch?

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

Easy, you create a local branch, at the time of checkout remote branch.

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

$ git checkout -b my_local_branch origin/production

$ git checkout -b my_local_branch 来源/生产

回答by 0xcrab

git remote prune origin

is right, just adding you can use --dry-runoption, that reports what branches will be pruned from your local repo, but doesnt actually prune them

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

git remote prune origin --dry-run