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

本文翻译自:How do you roll back (reset) a Git repository to a particular commit? [duplicate]

This question already has an answer here: 这个问题在这里已有答案:

I cloned a Git repository and then tried to roll it back to a particular commit early on in the development process. 我克隆了一个Git存储库,然后尝试在开发过程的早期将其回滚到特定的提交。 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. 在该点之后添加到存储库的所有内容对我来说都不重要,因此我想省略本地源代码中的所有后续更改。

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? 从项目历史中的特定提交中获取存储库的源代码的正确方法是什么,并省略所有后续更新?


#1楼

参考:https://stackoom.com/question/6mdx/如何将Git存储库回滚-重置-到特定的提交-重复


#2楼

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". 右键单击要返回的提交,然后选择“将master branch重置为here”。 Then choose hard from the next menu. 然后从下一个菜单中选择硬盘。


#3楼

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

Notes: 笔记:

  • git reset without the --hard option resets the commit history, but not the files. 没有--hard选项的git reset重置提交历史记录,但不会重置文件。 With the --hard option the files in working tree are also reset. 使用--hard选项,工作树中的文件也会重置。 ( 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 push <reponame> -f贷记用户


#4楼

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 c2e7af2b51 with 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. 当您开始使用历史记录修改git命令(重置,rebase)以在运行它们之前创建备份分支时,总是很好。 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 reflog Git keeps commits around for quite a while even if there are no branches or tags pointing to them. 即使没有指向它们的分支或标签,Git也会保持一段时间的提交。

Original answer: 原始答案:

A slightly less scary way to do this than the git reset --hard method is to create a new branch. git reset --hard方法更难以实现的方法是创建一个新分支。 Let's assume that you're on the master branch and the commit you want to go back to is c2e7af2b51 . 假设您在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

#5楼

When you say the 'GUI Tool', I assume you're using Git For Windows. 当你说'GUI工具'时,我假设你正在使用Git For Windows。

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. 这样,在测试更改时,您的主人可以保持不变。

With the GUI you need to 'roll back this commit' like you have with the history on the right of your view. 使用GUI,您需要“回滚此提交”,就像您拥有视图右侧的历史记录一样。 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. 这会将您的文件设置回这个版本中的文件。

发布了0 篇原创文章 · 获赞 51 · 访问量 34万+

猜你喜欢

转载自blog.csdn.net/CHCH998/article/details/105505346