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

标签 git

当我这样做时 git checkout origin/bugfix/NTP-183-datefns git 显示

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

这里发生了什么?

最佳答案

origin/<branch-name>是远程分支引用。它不能被修改。
因此,当您 checkout 此引用时,git 无法将您移至此分支,但会将您移至该分支引用的提交。然后您在 分离头 状态,这意味着您不在分支上,而是直接在提交上(在命令的输出中解释了含义)。
<branch-name>只是一个本地分支,因此您可以对其进行处理。
因此,当您 checkout 此引用资料时,git 会将您移至该分支。

有一点提示:如果本地分支<branch-name>不存在,但在所有 Remote 上确实存在一个同名的远程分支,git 将自动创建跟踪远程分支的本地分支并 checkout 它(在这种情况下 git checkout <branch-name> 等效于 git checkout --track -b <branch-name> any_remote/<branch-name> )

关于Git checkout 区别 git checkout origin/<branch-name> 和 git checkout <branch-name>?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45813147/

相关文章:

GIT 在 rebase 期间创建无关的 ~HEAD 文件

git - 如何使 Jenkins 过滤器文件在上次提交时更改并通过 FTP/SFTP 发送

git - TeamCity 无法从 VCS 加载build设置,忽略新设置

git 无法在没有完整索引行的情况下应用二进制补丁 ***

git - 通过 --tags 获取 git refs 时, "upload-pack: not our ref"是什么意思?

git - 如何只获取特定分支的 Git 日志?

git - 如何使用自定义消息 git stash stash 未跟踪的文件?

git丢弃专门更改暂存区

git - 如何为 `git difftool` 设置默认 diff 工具

Git:如何删除远程可用文件并仅保留本地更改的文件