我有一个master分支和一个development分支。我的生产环境使用master分支,而develop分支通常要提前几个提交。
我需要为生产添加快速补丁。我通常会做的是:
在这种情况下,我不小心忘记了第1步,这意味着当我创建my-hot-fix时,我在develop分支上。直到最后一步,我才意识到这一点,并将修补程序合并到master中。我没有得到一个零钱,而是从develop分支收到了许多先前的提交。
如何扭转这种情况?请注意,我尚未将更改推送到上游。
注意:关于意外合并还有其他SO问题。我的问题不只此而已-发生的原因是我要合并的分支是从错误的分支创建的。
答案 0 :(得分:2)
您最好的选择是简单地将master
和develop
上的更改重置为与遥控器同步。因此,假设您的遥控器称为origin
:
git checkout master && git reset --hard origin/master
git checkout develop && git reset --hard origin/develop
然后您可以这次从master创建一个新的修补程序分支,并git cherry-pick
这样创建:
查找提交哈希:
git log my-hot-fix
然后:
git cherry-pick MY_COMMIT_HASH
然后,您可以从第4步(开发git checkout)继续。
您很幸运,因为您还没有努力。如果您遇到困难并想还原本地更改,则可能还需要查看git reflog
命令。它真的很强大! https://git-scm.com/docs/git-reflog
答案 1 :(得分:1)
如果未推送更改,则可以使用git reset
命令取消提交
答案 2 :(得分:1)
要在正确的分支上提交:
# in the output of next command, spot the commit you know is good and store its hash ID, let's call it <commit_hash_OK>
git log -10 --oneline master
# in the output of next command, spot the NEW hotfix commit and store its hash ID, let's call it <commit_hash_NEW>
git log -10 --oneline my-hot-fix
# we need to reset master to its previous state
git checkout master
git reset --hard <commit_hash_OK>
# finally, put onto master only the commit you needed
git cherry-pick <commit_hash_NEW>
然后您将被设置为本地,只需像往常一样按下即可。