GitHub - 如何将更改恢复到以前的状态 [英] GitHub - How to revert changes to previous state

查看:842
本文介绍了GitHub - 如何将更改恢复到以前的状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用GitHub作为远程仓库。



我已经向服务器推送了5次提交,并希望在提交之前恢复到状态。 / p>

如果提交散列是 3425661dba2aadccdbab ,那么如何将整个本地/远程恢复为该提交?我试过

  $ reset --hard 3425661dba2aadccdbab 

但只重设我的工作头到那个分支,并要求我再次执行 git pull 。我尝试了结帐,但是这导致我在一个分离头分支登陆。 您基本上有两个选项可以恢复更改:


  1. 创建一个应用反向更改的新提交。这是首选,因为它不会更改公共存储库中的历史记录。

  2. 删除提交并强制推送它们。

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


git-revert - 恢复一些现有的提交



给定一个或多个现有提交,恢复相关修补程序引入的更改,记录一些新的提交记录。


一个例子是 git revert -n HEAD〜5 .. HEAD 。这个命令创建了5个新的提交,其中每个提交都撤销了当前签出分支的最后5个提交中的一个。



第二个选项是实际删除提交。请注意,这会更改存储库中的历史记录。所以任何已经做出改变的人都可能会感到惊讶,并且事情会很快变得混乱。也就是说,你可以这样做:
$ b $

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

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



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

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.

解决方案

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.

The first option can be achieved by using 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.

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

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.

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.

这篇关于GitHub - 如何将更改恢复到以前的状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆