如何在特定提交之前检出前一个提交?

11

在查看GitHub问题后,我找到了一个提交记录,可能是导致代码出错的原因,我想通过执行以下操作来确认这一怀疑:

git checkout --one-prior f1962b3cc771184a471e1350fa280d80d5fdd09d


1
https://www.kernel.org/pub/software/scm/git/docs/gitrevisions.html - SLaks
也许值得一看 git bisect 来查找破坏性变更:https://www.kernel.org/pub/software/scm/git/docs/git-bisect.html - Chris
Chris,谢谢你提醒我学习git bisect的魔法是如何工作的。 - Milktrader
2个回答

25

给你:

git checkout f1962b3cc771184a471e1350fa280d80d5fdd09d^

注意末尾的^。这意味着落后一个修订版本。

例如,这将落后于5个修订版本:

git checkout f1962b3cc771184a471e1350fa280d80d5fdd09d^^^^^

...等价于:

git checkout f1962b3cc771184a471e1350fa280d80d5fdd09d~5

顺便提一下,当你这样做时,你会处于一个分离的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

0

不是针对你的 checkout 问题进行回答,但你也可以使用 rebase

这是我的终端历史记录的简短日志,没有修改过。

Rebase to change first the second last and then the last commit
 1240  git rebase -i HEAD~2 
→ then an editor pops up, write “edit” in front of the commit you 
want to change (in my case the older commit, look up the commit msg 
in the git repo to find the right one), leave the other at “pick”
 1241  git rev-parse HEAD 
→ shows the hash, better use `git log`, then you do not need to 
remember the hashes!
 1242  git add .
 1243  git status
 1244  git commit --amend
 1245  git rebase --continue
 1246  git rev-parse HEAD
 1247  git reflog
 1248  git rebase -i HEAD~2
→ then an editor pops up, write “edit” in front of the commit you 
want to change (in my case the younger commit, look up the commit msg 
in the git repo to find the right one), leave the other at “pick”
 1249  git rev-parse HEAD
 1250  git log
 1251  git status
 1252  git add .
 1253  git commit --amend
 1254  git rebase --continue
 1255  git log
 1256  git push -f
 1257  git log

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接