不小心拉了一个远程分支到我的feature分支,解决了冲突又commit了。这可以恢复吗?
Accidentally pulled a remote branch to my feature branch, resolved conflicts and committed again. Can this be reverted?
我不小心将一个远程分支拉到了我的本地分支,解决了合并的冲突并再次提交了一些不同的更改到功能分支。我是否可以只从远程分支恢复拉取而不会丢失此后所做的提交,或者还有什么可以解决这种情况?工作流程类似于:
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
有多种方法可以解决这个问题;最简单的方法可能是重置为合并前的状态,然后 cherry-pick 您在此基础上所做的提交。
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 reset
或 git rebase
来更正其历史记录。另请注意,如果其他人在您上次获取后推送了更改,您可能会无意中删除这些更改。使用 --force-with-lease
代替 --force
将帮助您避免这种情况。在很多情况下,改变历史是不值得的。
我不小心将一个远程分支拉到了我的本地分支,解决了合并的冲突并再次提交了一些不同的更改到功能分支。我是否可以只从远程分支恢复拉取而不会丢失此后所做的提交,或者还有什么可以解决这种情况?工作流程类似于:
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
有多种方法可以解决这个问题;最简单的方法可能是重置为合并前的状态,然后 cherry-pick 您在此基础上所做的提交。
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 reset
或 git rebase
来更正其历史记录。另请注意,如果其他人在您上次获取后推送了更改,您可能会无意中删除这些更改。使用 --force-with-lease
代替 --force
将帮助您避免这种情况。在很多情况下,改变历史是不值得的。