使用 Git 根据提交 ID 恢复到特定提交?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3639115/
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
Reverting to a specific commit based on commit id with Git?
提问by prosseek
With git log
, I get a list of commits that I have made so far.
使用git log
,我会得到迄今为止我所做的提交列表。
commit f5c5cac0033439c17ebf905d4391dc0705dbd5f1
Author: prosseek
Date: Fri Sep 3 14:36:59 2010 -0500
Added and modified the files.
commit c14809fafb08b9e96ff2879999ba8c807d10fb07
Author: prosseek
Date: Tue Aug 31 08:59:32 2010 -0500
Just simple test for core.editor.
... etc ...
How can I revert it back to a specific commit? For example, what should I do if I want to go back to
commit c14809fafb08b9e96ff2879999ba8c807d10fb07
?Is there any other/better way to go back to a specific commit with Git? For example, can I put some label of each commit to get it back with the label?
如何将其恢复为特定的提交?例如,如果我想回到 ,我该怎么办
commit c14809fafb08b9e96ff2879999ba8c807d10fb07
?有没有其他/更好的方法可以使用 Git 返回到特定的提交?例如,我可以为每个提交添加一些标签以将其带回标签吗?
回答by bwawok
Do you want to roll back your repo to that state? Or you just want your local repo to look like that?
你想将你的回购回滚到那个状态吗?或者你只是想让你的本地仓库看起来像那样?
if you do
如果你这样做
git reset --hard c14809fa
It will make your local code and local history be just like it was at that commit. But then if you wanted to push this to someone else who has the new history, it would fail.
它将使您的本地代码和本地历史记录就像在提交时一样。但是如果你想把它推送给拥有新历史的其他人,它就会失败。
if you do
如果你这样做
git reset --soft c14809fa
It will move your HEAD to where they were , but leave your local files etc. the same.
它会将您的 HEAD 移动到它们所在的位置,但让您的本地文件等保持不变。
So what exactly do you want to do with this reset?
那么你到底想用这个重置做什么?
Edit -
编辑 -
You can add "tags" to your repo.. and then go back to a tag. But a tag is really just a shortcut to the sha1.
您可以将“标签”添加到您的存储库中……然后返回到标签。但是标签实际上只是 sha1 的快捷方式。
You can tag this as TAG1.. then a git reset --soft c14809fa
, git reset --soft TAG1
, or git reset --soft c14809fafb08b9e96ff2879999ba8c807d10fb07
would all do the same thing.
您可以将其标记为 TAG1.. 然后 a git reset --soft c14809fa
、git reset --soft TAG1
、 或git reset --soft c14809fafb08b9e96ff2879999ba8c807d10fb07
都可以做同样的事情。
回答by Peter A
I think, bwawok's answer is wrong at some point:
我认为,bwawok 的回答在某些时候是错误的:
if you do
git reset --soft c14809fa
It will make your local files changed to be like they were then, but leave your history etc. the same.
如果你这样做
git reset --soft c14809fa
它会使您的本地文件更改为与当时一样,但保留您的历史记录等。
According to manual: git-reset, "git reset --soft"...
根据手册: git-reset,“git reset --soft”...
does not touch the index file nor the working tree at all (but resets the head to <commit>, just like all modes do). This leaves all your changed files "Changes to be committed", as git status would put it.
根本不接触索引文件或工作树(但将头部重置为 <commit>,就像所有模式一样)。这会留下所有更改的文件“要提交的更改”,正如 git status 所说的那样。
So it will "remove" newer commits from the branch. This means, after looking at your old code, you cannot go to the newest commit in this branch again, easily. So it does the opposide as described by bwawok: Local files are not changed (they look exactly as before "git reset --soft"), but the history is modified (branch is truncated after the specified commit).
因此,它将从分支中“删除”较新的提交。这意味着,在查看旧代码后,您无法轻松地再次转到此分支中的最新提交。所以它与 bwawok 描述的相反:本地文件没有改变(它们看起来和“git reset --soft”之前的完全一样),但历史被修改(分支在指定的提交后被截断)。
The command for bwawok's answer might be:
bwawok 的回答命令可能是:
git checkout <commit>
You can use this to peek at old revision: How did my code look yesterday?
您可以使用它来查看旧版本:我的代码昨天看起来如何?
(I know, I should put this in comments to this answer, but stackoverflow does not allow me to do so! My reputation is too low.)
(我知道,我应该将此放在对此答案的评论中,但 stackoverflow 不允许我这样做!我的声誉太低了。)
回答by Doches
git reset c14809fafb08b9e96ff2879999ba8c807d10fb07
is what you're after...
git reset c14809fafb08b9e96ff2879999ba8c807d10fb07
是你在追求什么...
回答by kenzaraque
If you want to force the issue, you can do:
如果你想强制这个问题,你可以这样做:
git reset --hard c14809fafb08b9e96ff2879999ba8c807d10fb07
send you back to how your git clone looked like at the time of the checkin
将您发送回您的 git clone 在签入时的样子