检出一个分支对应的提交

Checkout the commit corresponding to a branch

我正在使用 git worktree,并且想要签出一个分支,该分支也在不同的工作目录上签出。我不需要分支本身,但我需要与分支对应的提交作为分离的 HEAD。 git checkout $(git rev-parse mybranch) 是一个解决方案,但它很冗长(如果已经存在简单的方法,我宁愿不为其添加别名)。有没有什么办法可以简洁的查看一个分支对应的commit?

git checkout mybranch^0git checkout mybranch~0 都有效。

~0 表示 "The 0th ancestor",这是提交本身。 ^0 表示 "the 0th parent" 用于多个父级的提交(合并提交)。我不确定为什么这会解决提交本身,但确实如此。

由于 git checkout <anything that is not just the name of a branch> 会将提交作为分离的 HEAD 检出,因此 git checkout mybranch~0 将工作,即使 git checkout mybranch 不工作。

根据git help checkout,直接且不神奇的解决方案是:

git checkout --detach mybranch