在Windows上检出HEAD(和变体)时出现奇怪的Git行为

时间:2013-04-17 15:34:43

标签: git git-checkout git-reflog

设置

让我们设想一个简单的存储库如下。可以看到HEADmaster

$ git log --decorate --graph
* commit 99d20608088ba9c74b57e36a1b0b79ff2be42d68 (HEAD, master)
| Author: Saaman <user@domain.com>
| Date:   Wed Apr 17 16:53:50 2013 +0200
|
|     My third commit
|
* commit a4a040c8b5c3923a2ba0f652caae0540f84c4c98
| Author: Saaman <user@domain.com>
| Date:   Wed Apr 17 16:53:27 2013 +0200
|
|     My second commit
|
* commit c5d20f203c11acbb9238ab77581e27a15ccde25e
  Author: Saaman <user@domain.com>
  Date:   Wed Apr 17 16:52:58 2013 +0200

      My first commit

$ git reflog
99d2060 HEAD@{0}: commit: My third commit
a4a040c HEAD@{1}: commit: My second commit
c5d20f2 HEAD@{2}: commit (initial): My first commit

现在,让我们执行一些结账操作

$ git checkout master
Already on 'master'

$ git reflog
99d2060 HEAD@{0}: checkout: moving from master to master
99d2060 HEAD@{1}: commit: My third commit
a4a040c HEAD@{2}: commit: My second commit
c5d20f2 HEAD@{3}: commit (initial): My first commit

$ git checkout HEAD

$ git reflog
99d2060 HEAD@{0}: checkout: moving from master to master
99d2060 HEAD@{1}: commit: My third commit
a4a040c HEAD@{2}: commit: My second commit
c5d20f2 HEAD@{3}: commit (initial): My first commit

reflog显示

  • 签出HEAD不会在reflog中生成任何条目
  • 签出master会在reflog中插入新条目(说明HEAD已从master移至master

让我们尝试别的东西

$ git checkout head
Note: checking out 'head'.

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 99d2060... My third commit

$ git reflog
99d2060 HEAD@{0}: checkout: moving from master to head
99d2060 HEAD@{1}: checkout: moving from master to master
99d2060 HEAD@{2}: commit: My third commit
a4a040c HEAD@{3}: commit: My second commit
c5d20f2 HEAD@{4}: commit (initial): My first commit

reflog现在显示

  • head已解决为相同的SHA 99d2060
  • HEAD现已分离
  • 已在reflog中插入新条目(说明HEAD已从master移至head

问题

我很难理解这些行为。

  • 为什么签出HEAD在reflog中没有产生任何内容,而签出master(指向HEAD的分支)呢?
  • 为什么签出head(小写)会分离HEAD,而git可以成功将其剥离到同一次提交?

注意:这些测试已在Windows / msysgit上执行

1 个答案:

答案 0 :(得分:1)

在分析代码之后,这里有一些答案:

  • git checkout HEAD不记录HEAD是否已分离。
  • 从“master”移动到“master”并不是真正有用的信息,因为分支和目标都没有改变。用一些无用的信息来填充日志,但是相当无害。
  • git checkout head无法在Linux上运行。它仅适用于Windows,因为文件系统不区分大小写。