通过 Git 中的 SHA 哈希恢复提交?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1895059/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Revert to a commit by a SHA hash in Git?
提问by JP Silvashy
I'm not clear on how git revert
works. For example, I want to revert to a commit six commits behind the head, reverting all the changes in the intermediary commits in between.
我不清楚如何git revert
工作。例如,我想恢复到头部后面的六个提交,恢复中间提交中的所有更改。
Say its SHAhash is 56e05fced214c44a37759efa2dfc25a65d8ae98d
. Then why can't I just do something like:
说它的SHA哈希是56e05fced214c44a37759efa2dfc25a65d8ae98d
. 那为什么我不能做这样的事情:
git revert 56e05fced214c44a37759efa2dfc25a65d8ae98d
回答by CB Bailey
If you want to commit on top of the current HEAD with the exact state at a different commit, undoing all the intermediate commits, then you can use reset
to create the correct state of the index to make the commit.
如果您想在当前 HEAD 之上以不同提交的确切状态提交,撤消所有中间提交,那么您可以使用reset
来创建索引的正确状态以进行提交。
# Reset the index and working tree to the desired tree
# Ensure you have no uncommitted changes that you want to keep
git reset --hard 56e05fced
# Move the branch pointer back to the previous HEAD
git reset --soft HEAD@{1}
git commit -m "Revert to 56e05fced"
回答by Jakub Nar?bski
What git-revertdoes is create a commit which undoes changes made in a given commit, creating a commit which is reverse (well, reciprocal) of a given commit. Therefore
什么混帐复归做的就是创建一个提交,撤消变化在给定的提交作出,创造了一个指定的提交的提交是相反的(当然,倒数)。所以
git revert <SHA-1>
should and does work.
应该并且确实有效。
If you want to rewind back to a specified commit, and you can do this because this part of history was not yet published, you need to use git-reset, not git-revert:
如果您想回退到指定的提交,并且您可以这样做,因为这部分历史记录尚未发布,您需要使用git-reset,而不是 git-revert :
git reset --hard <SHA-1>
(Note that --hard
would make you lose any non-committed changes in the working directory).
(请注意,--hard
这会使您丢失工作目录中任何未提交的更改)。
Additional Notes
补充说明
By the way, perhaps it is not obvious, but everywhere where documentation says <commit>
or <commit-ish>
(or <object>
), you can put an SHA-1identifier (full or shortened) of commit.
顺便说一句,也许这并不明显,但是在文档说<commit>
或<commit-ish>
(或<object>
)的任何地方,您都可以放置提交的SHA-1标识符(完整或缩短)。
回答by Michael Krelin - hacker
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
回答by darshit khatri
The best way to rollback to a specific commit is:
回滚到特定提交的最佳方法是:
git reset --hard <commit-id>
Then:
然后:
git push <reponame> -f
回答by Flueras Bogdan
If your changes have already been pushed to a public, sharedremote, and you want to revert all commits between HEAD
and <sha-id>
, then you can pass a commit range to git revert
,
如果您的更改已被推送到公共共享远程,并且您想恢复HEAD
和之间的所有提交<sha-id>
,那么您可以将提交范围传递给git revert
,
git revert 56e05f..HEAD
and it will revert all commits between 56e05f
and HEAD
(excluding the start point of the range, 56e05f
).
它将恢复56e05f
和之间的所有提交HEAD
(不包括范围的起点,56e05f
)。
回答by Jacob Dam
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. The first one changes the working directory, but it also changes head (the current branch) too. We fix the head by the second one.
这两个命令git reset --hard
和git reset --soft
在这里很神奇。第一个改变了工作目录,但它也改变了 head(当前分支)。我们通过第二个固定头部。
回答by Tuyen Tran
This is more understandable:
这更容易理解:
git checkout 56e05fced -- .
git add .
git commit -m 'Revert to 56e05fced'
And to prove that it worked:
并证明它有效:
git diff 56e05fced
回答by longda
Should be as simple as:
应该很简单:
git reset --hard 56e05f
That'll get you back to that specific point in time.
这会让你回到那个特定的时间点。
回答by Jake
This might work:
这可能有效:
git checkout 56e05f
echo ref: refs/heads/master > .git/HEAD
git commit