本文翻译自:Revert to a commit by a SHA hash in Git? [duplicate]
This question already has an answer here: 这个问题在这里已有答案:
- How do I revert a Git repository to a previous commit? 如何将Git存储库还原为先前的提交? 41 answers 41个答案
I'm not clear on how git revert
works. 我不清楚git revert
是如何工作的。 For example, I want to revert to a commit six commits behind the head, reverting all the changes in the intermediary commits in between. 例如,我想在头部后面恢复提交六次提交,恢复中间提交之间的所有更改。
Say its SHA hash is 56e05fced214c44a37759efa2dfc25a65d8ae98d
. 说它的SHA哈希是56e05fced214c44a37759efa2dfc25a65d8ae98d
。 Then why can't I just do something like: 那我为什么不能做以下事情:
git revert 56e05fced214c44a37759efa2dfc25a65d8ae98d
#1楼
参考:https://stackoom.com/question/7wzT/在Git中恢复由SHA哈希提交-重复
#2楼
This might work: 这可能有效:
git checkout 56e05f
echo ref: refs/heads/master > .git/HEAD
git commit
#3楼
The best way to rollback to a specific commit is: 回滚到特定提交的最佳方法是:
git reset --hard <commit-id>
Then: 然后:
git push <reponame> -f
#4楼
Should be as simple as: 应该如此简单:
git reset --hard 56e05f
That'll get you back to that specific point in time. 那会让你回到那个特定的时间点。
#5楼
Updated: 更新:
This answer is simpler than my answer: https://stackoverflow.com/a/21718540/541862 这个答案比我的答案简单: https : //stackoverflow.com/a/21718540/541862
Original answer: 原始答案:
# Create a backup of master branch
git branch backup_master
# Point master to '56e05fce' and
# make working directory the same with '56e05fce'
git reset --hard 56e05fce
# Point master back to 'backup_master' and
# leave working directory the same with '56e05fce'.
git reset --soft backup_master
# Now working directory is the same '56e05fce' and
# master points to the original revision. Then we create a commit.
git commit -a -m "Revert to 56e05fce"
# Delete unused branch
git branch -d backup_master
The two commands git reset --hard
and git reset --soft
are magic here. 两个命令git reset --hard
和git reset --soft
在这里很神奇。 The first one changes the working directory, but it also changes head (the current branch) too. 第一个更改工作目录,但它也会更改head(当前分支)。 We fix the head by the second one. 我们通过第二个修复头部。
#6楼
It reverts the said commit, that is, adds the commit opposite to it. 它恢复了所述提交,即添加与其相反的提交。 If you want to checkout an earlier revision, you do: 如果要签出早期版本,请执行以下操作:
git checkout 56e05fced214c44a37759efa2dfc25a65d8ae98d