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

Mac*_*nto 4 git

当我做git checkout origin/bugfix/NTP-183-datefnsgit 显示时

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.
Run Code Online (Sandbox Code Playgroud)

但当我尝试时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'.
Run Code Online (Sandbox Code Playgroud)

这里发生了什么?

zig*_*arn 5

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>