为什么在我运行“git checkout origin/<branch>”后,Git 会告诉我“当前不在任何分支上”?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25670173/
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-09 02:25:46  来源:igfitidea点击:

Why does Git tell me "Not currently on any branch" after I run "git checkout origin/<branch>"?

gitgit-checkoutremote-branch

提问by hubatish

I was trying to follow the instructions from Git: "Not currently on any branch." Is there an easy way to get back on a branch, while keeping the changes?but git checkoutappears to be broken:

我试图按照Git的指示进行操作:“目前不在任何分支上。” 有没有一种简单的方法可以在保留更改的同时返回分支?git checkout似乎坏了:

$ git checkout origin/web-zach
HEAD is now at 1366cb1... Changed so css files not ignored

$ git status
# Not currently on any branch.
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
#       .cordova/config.xml
#       www/languages/pt/sounds/
nothing added to commit but untracked files present (use "git add" to track)

More specifically, I'm worried about the "Not currently on any branch" message. git checkoutdoesn't seem to do anything here... Isn't the entire purpose of that command to put me on a branch? How can I get back on a branch and commit/push again?

更具体地说,我担心“当前不在任何分支上”消息。git checkout在这里似乎没有做任何事情......该命令的全部目的不是将我放在分支上吗?我怎样才能回到分支并再次提交/推送?

回答by jub0bs

The output of git statusindicates that your working directory is clean; good.

的输出git status表明您的工作目录是干净的;好的。

Now, by running

现在,通过运行

git checkout origin/web-zach

you are attempting to check out a remote-trackingbranch, called origin/web-zach; it's a special type of branch, local to your repo, that keeps track of the corresponding branch, web-zach, living in the remote repository called origin.

您正在尝试检查一个名为的远程跟踪分支origin/web-zach;它是一种特殊类型的分支,web-zach位于您的存储库本地,用于跟踪位于名为origin.

However, the HEADreference (which you can think of as a "You Are Here" marker on a map) cannotpoint to a remote-tracking branch; only to a "normal" local branch, or to a commit directly. When you attempt to check out a remote-tracking branch, the HEADreference ends up pointing directlyat the tipof the remote-tracking branch (i.e. the commit to which that remote-tracking branch points):

但是,HEAD参考(您可以将其视为地图上的“您在这里”标记)不能指向远程跟踪分支;仅到“普通”本地分支,或直接提交。当您尝试检出远程跟踪分支时,HEAD引用最终直接指向远程跟踪分支的尖端(即该远程跟踪分支指向的提交):

enter image description here

在此处输入图片说明

When HEADdoes not point to a "normal" local branch, but points to a commit directly, you end up in so-called "detached-HEAD state". It's not the end of the world, but avoiding landing in that state as much as possible (at least at the beginning of your Git learning) will likely spare you some surprises.

HEAD不指向“正常”本地分支,而是直接指向提交时,您最终会处于所谓的“分离头状态”。这不是世界末日,但尽可能避免进入该状态(至少在您开始学习 Git 时)可能会给您带来一些惊喜。

To remedy the situation, you need to reattach HEADto some localbranch. Here, you may want to create and check out such a localbranch, by running, for instance

要纠正这种情况,您需要重新连接HEAD到某个本地分支。在这里,您可能希望创建并签出这样的本地分支,例如通过运行

git checkout -b web-zach

HEADwill then be pointing at the newly created local branch called web-zach:

HEAD然后将指向新创建的本地分支,名为web-zach

enter image description here

在此处输入图片说明

Then, you should get

那么,你应该得到

$ git status
On branch web-zach
Untracked files:
  (use "git add <file>..." to include in what will be committed)
      .cordova/config.xml
      www/languages/pt/sounds/
nothing added to commit but untracked files present (use "git add" to track)

After that, you'll be free to make changes, stage them, commit, and (if you have write access to the remote repo corresponding to originand no one else has pushed anything to origin/web-zachsince your last git fetch), push, using

之后,您可以自由地进行更改、暂存、提交和(如果您对对应的远程存储库具有写访问权限,origin并且origin/web-zach自上次以来没有其他人推送任何内容git fetch),推送,使用

git push -u origin web-zach