【问题标题】:How to revert Git soft reset?如何恢复 Git 软重置?
【发布时间】:2017-01-09 03:57:11
【问题描述】:

我在一个分支上工作,偶然我完成了git reset HEAD~1,因为我认为我正在重置我的最后一次提交。

问题是我什至没有提交我的更改,所以我已经完成了重置以提交,这是由其他人完成的。该提交中的许多更改是在我也在处理的文件上完成的,所以我没有注意到并继续我的工作。在我提交并推送我的更改后,我注意到缺少提交。

develop branch: commitA -> commitB -> commitC

my branch: commitA -> commitB -> myCommit

有什么方法可以还原这些更改并在我的提交之前插入commitC

【问题讨论】:

  • git commit -a -s -c ORIG_HEAD,但它不会包含添加的文件,您必须事先运行git add ...<list of files>...

标签: git git-revert


【解决方案1】:

您可以通过git reflog 查看您的工作树。首先,回到commitC 并选择myCommit 堆栈顶部的git log。然后只需更新遥控器。

$ git reflog
# copy the commit-hash of 'commitC'

$ git checkout <commitC-hash>
$ git reflog
# copy the 'myCommit-hash'

$ git cherry-pick  <myCommit-hash>         # take the 'commitC' top
$ git checkout -b 'new-my-branch'          # create 'new-my-branch' from current stage

# Replace 'my-branch' with 'new-my-branch'
$ git branch -D my-branch                  # delete 'my-branch'
$ git checkout -b my-branch                # create new 'my-branch' from 'new-y-branch'
$ git push -f origin HEAD                  # replace remote/my-branch by local/my-branch 

【讨论】:

  • 这与您提供的简单解决方案相去甚远。
【解决方案2】:

您不需要还原您的提交,因为您可以在远程分支上rebase您的本地分支,并在重新应用您的提交之前干净地引入commitC。 p>

我不知道你在本地做了什么来最终得到你展示给我们的分支图,但我认为你可以简单地将你的分支重新设置在删除 develop 上以拉入 commitC,然后在上面重播你的myCommit

git fetch origin           # bring origin/develop up to date
git checkout develop       # checkout local develop branch
git rebase origin/develop  # rebase your local branch on the remote one

现在您应该能够简单地通过以下方式快进远程分支:

git push origin develop

【讨论】:

  • 太复杂了。 git rebase --onto origin/develop HEAD~1
  • @0andriy 你说我的答案太复杂了,因为收到赞成票的人要复杂得多。另外,我认为这里不需要rebase --onto
猜你喜欢
  • 2019-10-05
  • 2022-11-30
  • 2018-06-22
  • 2013-11-22
  • 2013-04-15
  • 2012-11-11
  • 2018-11-10
  • 2019-04-11
  • 1970-01-01
相关资源
最近更新 更多