git checkout <commit hash =“”>

时间:2018-05-23 03:06:45

标签: git github version-control

如何使用git checkout进行上一次提交? 问题:我有3次提交,如 A,B和C (C是最后一次提交)A&lt; -B&lt; - C 我想去我的项目的先前版本,即提交A,然后使用git checkout <A hash>现在我在提交A中,之后我想要提交B,我想要提交哈希{{ 1}}没有显示,那么我想切换回一个接一个地提交B和C.

2 个答案:

答案 0 :(得分:2)

您可以使用

git reflog

显示它。

答案 1 :(得分:0)

我认为您所描述的问题是,一旦您在提交A处完成了结帐,您就很难找到要返回的提交C的哈希值。最简单的方法是在结帐提交A之前在提交C处创建分支或标记。但是如果你没有这样做,git reflog来救援:

$ git init
$ for i in a b c; do echo $i > file; git add file; git commit -m "Write $i to file"; done
[master (root-commit) 0d89e41] Write a to file
 1 file changed, 1 insertion(+)
 create mode 100644 file
[master a8f774a] Write b to file
 1 file changed, 1 insertion(+), 1 deletion(-)
[master c52fd0c] Write c to file
 1 file changed, 1 insertion(+), 1 deletion(-)
$ git log --pretty=oneline
c52fd0c4ae9fb40b4e7355c7a2f8ecbe80d9465c (HEAD -> master) Write c to file
a8f774a88af68481db6106f1d613680769c1cde9 Write b to file
0d89e41907055d75ac8c65d40feec4fc1ee9e381 Write a to file
$ git checkout HEAD~2
Note: checking out 'HEAD~2'.

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 0d89e41... Write a to file
$ # Oh no!  I can't figure out the hash of commit C
$ git reflog
0d89e41 (HEAD) HEAD@{0}: checkout: moving from master to HEAD~2
c52fd0c (master) HEAD@{1}: commit: Write c to file
a8f774a HEAD@{2}: commit: Write b to file
0d89e41 (HEAD) HEAD@{3}: commit (initial): Write a to file

现在,要返回提交C,您可以将其引用为c52fd0cHEAD@{1}。 (后者会随着您对历史记录的修改而改变,每次大括号中的数字都会递增。)