繁体   English   中英

Git结账差异git结账来源/ <branch-name> 和git结帐 <branch-name> ?

[英]Git checkout difference git checkout origin/<branch-name> and git checkout <branch-name>?

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

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

这里发生了什么?

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

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

您可以在任何提交(哈希)处签出,包括远程分支“指向”的提交。 但是如果你签出远程分支指向的提交,那么本地你将指向一个原始提交。

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

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

哪里:

  • “-t”使这成为一个跟踪分支(这是一个很好的可选)
  • “-b bugfix / NTP-183-datefns”创建一个名为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

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>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM