如何在Git的当前分支中获取最新的头名?

时间:2012-08-21 01:41:23

标签: git

例如,在Linux Kernel的git工作树中。

$ git checkout v2.6.6
$ git checkout v3.3

如何在当前分支v3.3中找到最后一个头名或HASHID?在上面的例子中,它应该得到v2.6.6或者v2.6.6的HASHID。

感谢。

2 个答案:

答案 0 :(得分:5)

每次移动头部时,git都会将其记录在reflog

$ git reflog

如果您在问题中的命令之后运行此命令,旧的提交哈希将在第二行向下,并将引用您要移动到的标记。

例如

ff06760 HEAD@{0}: checkout: moving from 9b49c22462f5dd73ff18eacff5983f141f98cb82 to v3.3
9b49c22 HEAD@{1}: checkout: moving from ff06760cd0db8cef49915e68886c66c09b1cade1 to v2.6.6

答案 1 :(得分:2)

扩展Blake Taylor的答案,最后HEAD位置总是HEAD@{1},但这种类型的修订规范比这更强大:

git checkout HEAD@{n} ;# checkout the nth prior position of HEAD
git checkout HEAD@{"1 hour ago"} ;# checkout HEAD an hour back
git checkout master@{"yesterday"} ;# checkout yesterday's master
git checkout master@{"1 week ago"} ;# checkout last week's
git checkout @{-1} ;# checkout the last branch checked out
git checkout @{-n} ;# checkout the nth last branch

和我个人的最爱:

git checkout :/"<regex>" ;# checkout the youngest commit matching <regex>

......等什么?此语法 awesome:

git checkout :/"Case 959" ;# see the state when we fixed case 959
git checkout :/"ct/topic" ;# checkout the state when 'ct/topic' was merged
git checkout :/"^Hotfix" ;# checkout the most recent commit that started 'Hotfix'

了解这些refspecs可以轻松浏览历史记录。

相关问题