git - 在 Windows 上 checkout HEAD(和变体)时奇怪的 Git 行为

标签 git git-checkout git-reflog

设置

让我们想象一个简单的存储库,如下所示。可以看到 HEAD 指向 master

$ 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

行动

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

$ 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 中产生任何条目
  • checkout master 会在 reflog 中插入一个新条目(声明 HEADmaster 移动到 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 中(说明 HEADmaster 移动到 head)

问题

我很难理解这些行为。

  • 为什么 check out HEAD 不会在 reflog 中产生任何东西,而 check out master(HEAD 指向的分支)却产生任何东西?
  • 为什么检查 head(小写)会分离 HEAD 而 git 可以成功地将它剥离到同一个提交?

注意:这些测试是在Windows/msysgit上进行的

最佳答案

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

  • git checkout HEAD 不会记录任何内容,无论 HEAD 是否分离。
  • 从“master”移动到“master”并不是真正有用的信息,因为分支及其目标都没有改变。用一些无用的信息来填充日志,但非常无害。
  • git checkout head 在 Linux 上不工作。它仅适用于 Windows,因为文件系统不区分大小写。

关于git - 在 Windows 上 checkout HEAD(和变体)时奇怪的 Git 行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16064279/

相关文章:

git checkout 上一条

git - 为什么 Ubuntu 机器无法访问 Github?

git branch 列出上游的分支

git - 为什么切换分支的 git 命令命名为 "git checkout"?

git - 如何从 git 存储库中删除旧的历史记录?

git - 你能备份 git reflog 吗?关于防止代码丢失的最佳实践的建议

git - 如何摆脱 git 中 post-receive 返回的每一行上出现的 'remote: ' 消息?

java - Intellij IDE 在哪里存储一个项目的工件设置

git 尝试在 checkout 时删除目录

git - "fatal: You are on a branch yet to be born"在这种情况下意味着什么