如何恢复`git fetch upstream;

How do I revert `git fetch upstream; git merge upstream/master`?

本问题已经有最佳答案,请猛点这里访问。

这个问题不同于其他问题,因为我希望保留一些提交,而恢复一些提交。

这是我的设置。

1
2
Upstream repo on github (public repo which I don't own)
Personal repo on my server(git clone --bare $upstream)

我的工作流程如下:

1
2
3
4
5
6
7
8
Need to customize my personal branch
   1. check out origin/master(Personal)
   2. make changes
   3. push to origin/master

Need to get new feature or bug fix from the original author(github)
   1. git fetch upstream
   2. git merge upstream/master

现在我发现上游/master有一个bug,想要恢复,在最后一次提取/合并上游之前,如何返回状态?

编辑。

假设

  • I merged upstream
  • I merged what a team member pushed to origin/master(Personal)
  • 现在我要撤消步骤1。

    git reset --hard commit_sha_of_one_prior_the_merge_1

    (Although finding the sha is not easy. git log shows a lot of commit
    sha's of upstream, so it's not easy to find
    commit-sha-of_one_prior_the_merge_1 which is not of upstream but of
    origin/master)

    如何保持步骤2合并?

    假设一个稍微复杂一点的场景

  • I merged upstream
  • I merged what another team member pushed to Personal
  • I also pushed my work to Personal
  • 现在我要撤消上游合并。我该怎么做?


    因为您有一个实际的合并提交——因为您已经引入了自己的更改——所以您应该能够做到:

    1
    git reset --hard HEAD~1

    假设上一次提交是有问题的合并提交。


    您可以将本地master分支重置为origin/master状态(如果您没有将合并推送到origin/master状态),方法如下:

    1
    git reset --hard origin/master

    如果不可能,可以使用reflog:

    1
    git reflog

    您可以看到撤消git-rebase了解更多详细信息