如何将 Git 存储库回滚(重置)到特定提交?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1616957/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 07:15:34  来源:igfitidea点击:

How do you roll back (reset) a Git repository to a particular commit?

git

提问by kauppi

I cloned a Git repository and then tried to roll it back to a particular commit early on in the development process. Everything that was added to the repository after that point is unimportant to me so I want to omit all subsequent changes from my local source code.

我克隆了一个 Git 存储库,然后在开发过程的早期尝试将其回滚到特定的提交。在此之后添加到存储库的所有内容对我来说都不重要,因此我想省略本地源代码中的所有后续更改。

However, when I try to roll back in the GUI tool it doesn't update my local file system - I always end up with the latest source code for the project.

但是,当我尝试在 GUI 工具中回滚时,它不会更新我的本地文件系统 - 我总是以项目的最新源代码结束。

What's the correct way to just get the source for a repository as of a particular commit in the project's history and omit all later updates?

从项目历史记录中的特定提交中获取存储库源并忽略所有后续更新的正确方法是什么?

回答by kauppi

git reset --hard <tag/branch/commit id>


Notes:

笔记:

  • git resetwithout the --hardoption resets the commit history, but not the files. With the --hardoption the files in working tree are also reset. (credited user)

  • If you wish to commit that state so that the remote repository also points to the rolled back commit do: git push <reponame> -f(credited user)

  • git reset没有该--hard选项会重置提交历史,但不会重置文件。使用该--hard选项,工作树中的文件也会被重置。(信用用户

  • 如果你想提交这个状态,使得远程仓库也指向回滚承诺做到:git push <reponame> -f存入用户

回答by Neall

Update:

更新:

Because of changes to how tracking branches are created and pushed I no longer recommend renaming branches. This is what I recommend now:

由于跟踪分支的创建和推送方式发生了变化,我不再建议重命名分支。这是我现在推荐的:

Make a copy of the branch at its current state:

复制当前状态的分支:

git branch crazyexperiment

(The git branch <name>command will leave you with your current branch still checked out.)

(该git branch <name>命令将使您保持当前分支仍处于签出状态。)

Reset your current branch to your desired commit with git reset:

使用以下命令将当前分支重置为所需的提交git reset

git reset --hard c2e7af2b51

(Replace c2e7af2b51with the commit that you want to go back to.)

(替换c2e7af2b51为您要返回的提交。)

When you decide that your crazy experiment branch doesn't contain anything useful, you can delete it with:

当您确定您的疯狂实验分支不包含任何有用的内容时,您可以使用以下命令将其删除:

git branch -D crazyexperiment

It's always nice when you're starting out with history-modifying git commands (reset, rebase) to create backup branches before you run them. Eventually once you're comfortable you won't find it necessary. If you do modify your history in a way that you don't want and haven't created a backup branch, look into git reflog. Git keeps commits around for quite a while even if there are no branches or tags pointing to them.

当您开始使用修改历史的 git 命令(reset、rebase)在运行它们之前创建备份分支时,总是很好。最终,一旦您感到舒适,您就不会觉得有必要了。如果您确实以不想要的方式修改了历史记录并且尚未创建备份分支,请查看git reflog. 即使没有分支或标签指向它们,Git 也会在相当长的一段时间内保持提交。

Original answer:

原答案:

A slightly less scary way to do this than the git reset --hardmethod is to create a new branch. Let's assume that you're on the masterbranch and the commit you want to go back to is c2e7af2b51.

一个比这个git reset --hard方法稍微不那么可怕的方法是创建一个新分支。假设您在master分支上,并且要返回的提交是c2e7af2b51.

Rename your current master branch:

重命名您当前的主分支:

git branch -m crazyexperiment

Check out your good commit:

看看你的好提交:

git checkout c2e7af2b51

Make your new master branch here:

在此处创建新的主分支:

git checkout -b master

Now you still have your crazy experiment around if you want to look at it later, but your master branch is back at your last known good point, ready to be added to. If you really want to throw away your experiment, you can use:

现在,如果您想稍后查看它,您仍然可以进行疯狂的实验,但是您的主分支又回到了您最后一个已知的好点,随时可以添加。如果你真的想扔掉你的实验,你可以使用:

git branch -D crazyexperiment

回答by ReggieB

For those with a git gui bent, you can also use gitk.

对于那些有 git gui 弯曲的人,你也可以使用 gitk。

Right click on the commit you want to return to and select "Reset master branch to here". Then choose hard from the next menu.

右键单击要返回的提交并选择“将主分支重置到此处”。然后从下一个菜单中选择 hard。

回答by huff

When you say the 'GUI Tool', I assume you're using Git For Windows.

当您说“GUI 工具”时,我假设您使用的是 Windows 版 Git。

IMPORTANT, I would highly recommend creating a new branch to do this on if you haven't already. That way your master can remain the same while you test out your changes.

重要的是,如果您还没有,我强烈建议您创建一个新分支来执行此操作。这样,您的 master 可以在您测试更改时保持不变。

With the GUI you need to 'roll back this commit' like you have with the history on the right of your view. Then you will notice you have all the unwanted files as changes to commit on the left. Now you need to right click on the grey title above all the uncommited files and select 'disregard changes'. This will set your files back to how they were in this version.

使用 GUI,您需要“回滚此提交”,就像您在视图右侧使用历史记录一样。然后您会注意到您将所有不需要的文件作为要在左侧提交的更改。现在您需要右键单击所有未提交文件上方的灰色标题并选择“忽略更改”。这会将您的文件设置回它们在此版本中的状态。