Git结账区别git checkout origin / <branch-name>和git checkout <branch-name>?

时间:2017-08-22 08:56:26

标签: git

当我做git checkout origin/bugfix/NTP-183-datefns git show

Note: checking out 'origin/bugfix/NTP-183-datefns'.

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 6fd089d.

但是当我尝试git checkout bugfix/NTP-183-datefns

Switched to branch 'bugfix/NTP-183-datefns'
Your branch is up-to-date with 'origin/bugfix/NTP-183-datefns'.

这里发生了什么?

3 个答案:

答案 0 :(得分:1)

Git与分支机构合作具有自动化功能。默认远程在您的存储库中称为origin。如果你在命令行中使用遥控器,git将只检查没有分支的这个分支的提交。你在detached HEAD state。本文解释了这里发生的事情以及您的复杂情况。

如果您的存储库中只配置了一个远程数据库并且您想要检出来自此远程数据库的分支,那么它将自动为您创建一个本地分支。

答案 1 :(得分:1)

你可以在任何提交(哈希)结账,包括提交&#34;指向&#34;通过远程分支机构。但是如果你签出远程分支指向的提交,那么本地你将指向一个原始提交。

这是警告告诉你的。为了能够做任何有用的事情,你需要在本地分支上。签出远程分支不会自动生成一个本地分支来处理 - 它只会将您转移到该提交。要一气呵成地做你想做的事:

git checkout -t -b bugfix/NTP-183-datefns origin/bugfix/NTP-183-datefns

其中:

  • &#34; -t&#34;使这个跟踪分支(这是一个很好的可选)
  • &#34; -b bugfix / NTP-183-datefns&#34;创建一个名为bugfix / NTP-183-datefns的本地分支。

这基本上是你在一步中完成的两个步骤,所以它相当于(没有-t选项):

git checkout origin/bugfix/NTP-183-datefns
git branch bugfix/NTP-183-datefns
git checkout bugfix/NTP-183-datefns

与:

相同
git checkout origin/bugfix/NTP-183-datefns
git checkout -b bugfix/NTP-183-datefns

答案 2 :(得分:1)

origin/<branch-name>是远程分支引用。它无法修改 因此,当您签出此引用时,git无法将您移动到此分支,但它会将您移动到分支引用的提交。然后您处于分离的HEAD 状态,这意味着您不在分支上,而是直接在提交上(在命令的输出中解释了含义)。

<branch-name>只是一个本地分支,所以你可以继续工作 所以当你签出这个引用时,git会把你带到分支。

有一点提示:如果本地分支<branch-name>不存在,但在所有遥控器上只存在一个具有相同名称的远程分支,git将自动创建跟踪远程的本地分支分支和结帐(在这种情况下git checkout <branch-name>相当于git checkout --track -b <branch-name> any_remote/<branch-name>