【问题标题】:Accidentally pulled a remote branch to my feature branch, resolved conflicts and committed again. Can this be reverted?不小心将一个远程分支拉到我的功能分支,解决了冲突并再次提交。这可以恢复吗?
【发布时间】:2018-08-26 07:18:30
【问题描述】:

我不小心将一个远程分支拉到了我的本地分支中,解决了合并的冲突,并再次对功能分支提交了一些不同的更改。我可以只从远程分支恢复拉取,而不会丢失我此后所做的提交,或者还有什么可以解决这种情况?工作流程类似于:

git checkout my_feature
git pull origin wrong_branch

git commit -am "Resolving merge conflict"
git push

git commit -m "Some other commit to my_feature"
git commit -m "Another commit to my_feature"
git push

【问题讨论】:

  • 你应该可以在git reflog找到之前的状态。

标签: git git-merge git-pull git-revert


【解决方案1】:

有多种方法可以解决这个问题;最简单的方法可能是重置为合并之前的状态,然后挑选您在此基础上所做的提交。

git reset --hard abc123^  # where abc123 is the hash of the merge
                          # commit. The ^ refers to the first parent of
                          # that commit, i.e. the commit before the
                          # merge happened.

git cherry-pick cde234    # where cde234 is the hash of a commit that
                          # you made after the merge

另一种方法是使用带有--onto 标志的git rebase;请参阅man git-rebase 了解更多信息。

一定要备份以防万一。

请注意,这意味着您正在改变历史。要对遥控器进行这些更改,您需要强制推送; man git-push 寻找 --force--force-with-lease。请三思而后行,并注意如果在同一分支上工作的其他人已将这些更改拉入,他们将需要通过git resetgit rebase 采取行动来更正他们的历史记录。另请注意,如果其他人在您上次获取后推送了更改,您可能会无意中删除这些更改。使用--force-with-lease 代替--force 将帮助您避免这种情况。在许多情况下,改变历史并不值得。

【讨论】:

  • 感谢@jsageryd 的回答。由于某种原因,我无法获得 mege 提交的哈希值。但是我在合并之前有提交的哈希值(比如:test123)。在这种情况下是否应该在合并之前将分支的状态带到提交? 'git reset --hard test123^' 'git cherry-pick test123' git push
  • 如果你在合并之前有提交的提交哈希,你可以使用它,是的。在这种情况下,不要包含抑扬符 (^) 字符。
  • 更多关于如何指定修订(和^语法)的信息可以在man gitrevisionsgitirc.eu/gitrevisions.html中找到
猜你喜欢
  • 2018-11-01
  • 2018-11-02
  • 2023-01-11
  • 1970-01-01
  • 1970-01-01
  • 2020-06-24
  • 2019-08-11
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多