git GitHub - 如何将更改恢复到以前的状态

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

GitHub - How to revert changes to previous state

gitgithubrepositorycommitrevert

提问by Zhen

I am using GitHub as my remote repository.

我使用 GitHub 作为我的远程存储库。

I have already pushed 5 commits to the server and want to revert back to the state before the those commits.

我已经向服务器推送了 5 次提交,并希望恢复到这些提交之前的状态。

If the commit hash is 3425661dba2aadccdbab, how do I revert the entire local/remote back to that commit? I tried

如果提交哈希是3425661dba2aadccdbab,我如何将整个本地/远程恢复到该提交?我试过

$ reset --hard 3425661dba2aadccdbab

but that only resetted my working head to that branch and requires me to do a git pullagain. I tried checkout, but this caused me to land in a "detached head" branch.

但这只会将我的工作头重新设置为该分支,并需要我再做git pull一次。我尝试结帐,但这导致我进入“独立头”分支。

回答by Holger Just

You basically have two options to revert changes:

您基本上有两个选项可以还原更改:

  1. create a new commit which applies reverse changes. This is the preferred option as it doesn't changes history on a public repository
  2. Remove the commits and force push them.
  1. 创建一个应用反向更改的新提交。这是首选选项,因为它不会更改公共存储库上的历史记录
  2. 删除提交并强制推送它们。

The first option can be achieved by using git revert

第一个选项可以通过使用来实现 git revert

git-revert - Revert some existing commits

Given one or more existing commits, revert the changes that the related patches introduce, and record some new commits that record them.

git-revert - 还原一些现有的提交

给定一个或多个现有提交,还原相关补丁引入的更改,并记录一些记录它们的新提交。

An example would be git revert -n HEAD~5..HEAD. This command creates 5 new commits, each of which undoes one of the last 5 commits of the currently checked out branch.

一个例子是git revert -n HEAD~5..HEAD。此命令创建 5 个新提交,每个提交都撤消当前检出分支的最后 5 个提交之一。

The second option would be to actually remove the commits. Note that this changes history in the repository. So anyone who has already pull the changes will probably be rather surprised and things can get messy quickly. That said, you can do

第二种选择是实际删除提交。请注意,这会更改存储库中的历史记录。因此,任何已经拉动更改的人可能会感到相当惊讶,事情很快就会变得混乱。也就是说,你可以做

git reset --hard HEAD~5
git push --force

The first command will wipe any uncommitted changes in your current working copy. and reset your local repository to the state of the current HEAD - 5 commits. The second command will force-push to the default remote (i.e. GitHub) There, any changes diverging from your current local repository are overwritten.

第一个命令将清除当前工作副本中所有未提交的更改。并将您的本地存储库重置为当前 HEAD 的状态 - 5 次提交。第二个命令将强制推送到默认远程(即 GitHub)那里,任何与当前本地存储库不同的更改都将被覆盖。

A note of warning again: If you don't really know what you are doing, don't use this option as it can lead to data loss for you or others if not done right.Use the first option instead as it will transparently remove changes but without the nasty side-effects of history-rewriting.

再次警告:如果您真的不知道自己在做什么,请不要使用此选项,因为如果操作不当,可能会导致您或其他人的数据丢失。改用第一个选项,因为它会透明地删除更改,但没有历史重写的讨厌的副作用。

回答by Chandra Sekar

Do a git push -f. Not a good idea if there are other people using the same repo.

做一个git push -f。如果还有其他人使用相同的 repo,这不是一个好主意。

回答by Sailesh

You can do git revert <commit>to all the commits that have been made after your required state. (In the reverse order to avoid any conflicts.)

您可以git revert <commit>对在所需状态之后进行的所有提交进行操作。(以相反的顺序以避免任何冲突。)

This is a clean way if there are other people sharing the repo, but a little effortsome. (You may automate though...?)

如果有其他人共享 repo,这是一种干净的方式,但有点费力。(虽然你可以自动化......?)

回答by beatgammit

Do a git checkout, then commit it to the branch you want. This will make a new commit with the old code (so you'll have 6 commits).

执行 git checkout,然后将其提交到您想要的分支。这将使用旧代码进行新提交(因此您将有 6 次提交)。

git checkout HEAD~3, where 3 is the number of commits back you want to revert to.

git checkout HEAD~3,其中 3 是您要恢复的提交次数。

Better yet, you can checkout a single file into the present HEAD:

更好的是,您可以将单个文件签出到当前的 HEAD 中:

git checkout 3425661dba2aadccdbab:path/to/file/from/base

git checkout 3425661dba2aadccdbab:path/to/file/from/base

This will reduce the likelihood of making other people angry with you pulling the proverbial rug out from under their feet.

这将减少让其他人因为你从他们脚下拉出众所周知的地毯而生气的可能性。

EDIT:

编辑:

There's a similar question here:

这里有一个类似的问题:

Checkout old commit and make it a new commit

签出旧提交并使其成为新提交