GitHub - 如何将更改恢复到之前的状态
我使用 GitHub 作为我的远程存储库。
我已经将 5 次提交推送到服务器,并希望恢复到这些提交之前的状态。
如果提交哈希为 3425661dba2aadccdbab
,如何将整个本地/远程恢复到该提交?我尝试过
$ reset --hard 3425661dba2aadccdbab
,但这只是将我的工作头重置到该分支,并要求我再次执行 git pull 。我尝试结账,但这导致我降落在“独立头”分支。
I am using GitHub as my remote repository.
I have already pushed 5 commits to the server and want to revert back to the state before the those commits.
If the commit hash is 3425661dba2aadccdbab
, how do I revert the entire local/remote back to that commit? I tried
$ reset --hard 3425661dba2aadccdbab
but that only resetted my working head to that branch and requires me to do a git pull
again. I tried checkout, but this caused me to land in a "detached head" branch.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
基本上,您有两个选项可以恢复更改:
第一个选项可以通过使用 git revert 来实现
一个例子是 git revert -n HEAD~5..HEAD 。此命令创建 5 个新提交,每个提交都会撤消当前签出分支的最后 5 个提交之一。
第二个选项是实际删除提交。请注意,这会更改存储库中的历史记录。因此,任何已经进行更改的人可能都会感到相当惊讶,事情很快就会变得混乱。也就是说,您可以执行
第一个命令将擦除当前工作副本中任何未提交的更改。并将本地存储库重置为当前 HEAD - 5 提交的状态。第二个命令将强制推送到默认远程(即 GitHub),在那里,任何与当前本地存储库不同的更改都会被覆盖。
再次警告:如果您真的不知道自己在做什么,请不要使用此选项,因为如果操作不当,可能会导致您或其他人的数据丢失。使用第一个选项,因为它将透明地删除更改,但不会产生历史重写的令人讨厌的副作用。
You basically have two options to revert changes:
The first option can be achieved by using
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.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
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.
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.
执行 git push -f 。如果有其他人使用相同的存储库,这不是一个好主意。
Do a
git push -f
. Not a good idea if there are other people using the same repo.您可以对在所需状态之后所做的所有提交执行 git revert操作。 (按照相反的顺序以避免任何冲突。)
如果有其他人共享存储库,这是一种干净的方式,但有点费力。 (不过你可以自动化......?)
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.)This is a clean way if there are other people sharing the repo, but a little effortsome. (You may automate though...?)
进行 git checkout,然后将其提交到您想要的分支。这将使用旧代码进行新的提交(因此您将有 6 次提交)。
git checkout HEAD~3
,其中 3 是您要恢复到的提交数。更好的是,您可以将单个文件检出到当前 HEAD 中:
git checkout 3425661dba2aadccdbab:path/to/file/from/base
这将减少让其他人因您拉扯地毯而生气的可能性从他们的脚下出来。
编辑:
这里有一个类似的问题:
签出旧提交和使其成为一个新的提交
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 HEAD~3
, where 3 is the number of commits back you want to revert to.Better yet, you can checkout a single file into the present HEAD:
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