git checkout my_branch与git checkout origin / my_branch [英] git checkout my_branch vs. git checkout origin/my_branch

查看:486
本文介绍了git checkout my_branch与git checkout origin / my_branch的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我检出 branch2 这样(两个分支都存在)时,我在 branch1 上。



git checkout origin / branch2



然后我得到了一个分离的头错误:

 您处于'分离状态'状态。你可以环顾四周,做一些实验性的
修改并提交它们,你可以放弃你在这个
状态下进行的任何提交,而不会影响任何分支,通过执行另一个结账。

但是我只是签出 branch2 (没有 origin )然后它就可以正常工作:

git checkout branch2



那么 git checkout 有和没有 origin / ;为什么在使用 origin /

分离的HEAD 错误=h2_lin>解决方案

分离的HEAD消息是一个警告,而不是错误。

原因很简单。 Git有两种状态可以在分支机构中使用:




  • 在分支上,或者

  • 不在分支上。



在分支上创建新的提交时,分支自动前进以包含新的提交。



当分支上的不是并进行新的提交时,非分支也会前进,但这是警告的原因 - 如果你切换到另一个分支(这样你就可以使用它),git 忘记你在哪里。 1 当你在分支上时,分支名称会记住新提交;当你不在时,没有什么可以记住它们的。



你不能在远程追踪分支上



分支 origin / branch2 是一个远程追踪分支:即一个记录当我们(我们的git和 origin 的git)最后一次使用branch2 时, origin 有关于分支的谈话。因为它的目的是跟踪他们的位置,所以git不会让你接触那个分支并做出新的提交(然后它会记住的位置)而不是他们所在的位置)。



因为你不能在上面,所以检查出来会让你看到分离的HEAD状态。



但是你可以在一个正常的(本地)分支上



分支 branch2 是普通的普通本地分支。你可以随心所欲地做。您可以登录并进行新的提交。



(您的本地分支也可以记住远程追踪分支,就像它所谓的上游。Git的令人困惑的术语是,你的本地分支随后跟踪远程跟踪分支,跟踪这个词在这里出现很多次,就像分支这个词一样,都有不同的含义) p>




1 实际上它会在 reflog 对于 HEAD ,但默认只有30天有效。


I was on branch1 when I checkout branch2 like this (both branches are existing).

git checkout origin/branch2

then I got a detached head error:

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.

But then I just checkout branch2 (without origin) then it works ok:

git checkout branch2

So what's the difference between git checkout with and without origin/; and why there was the detached HEAD error when using origin/?

解决方案

The "detached HEAD" message is a warning, not an error.

The reason for it is simple enough. Git has two states you can be in with respect to branches:

  • on a branch, or
  • not on a branch.

When you are on a branch and make new commits, the branch automatically advances to include the new commits.

When you are not on a branch and make new commits, the non-branch also advances, but—here's the reason for the warning—if you then switch to some other branch (so that you are on it), git forgets where you were.1 When you are on a branch, the branch name remembers the new commits; when you are not, there is nothing to remember them.

You can't be on a remote-tracking branch

The branch origin/branch2 is a remote-tracking branch: that is, a branch that remembers "where branch2 was on origin the last time we (our git and origin's git) had a conversation about branches". Because the point of this is to track where they were, git won't let you get "on" that branch and make new commits (which would then remember where you are instead of where they were).

Because you can't be on it, checking it out gives you that "detached HEAD" state instead.

But you can be on a normal (local) branch

The branch branch2 is a normal, ordinary, local branch. It is yours to do with as you wish. You can get on it and make new commits.

(Your local branch can also remember the remote-tracking branch, as its so-called upstream. Git's confusing terminology for this is that your local branch is then "tracking" the remote-tracking branch. The word "tracking" appears too many times here, as does the word "branch", all with different meanings.)


1Actually it saves it for a while, in the reflog for HEAD, but this is only good for 30 days by default.

这篇关于git checkout my_branch与git checkout origin / my_branch的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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