我不小心在本地主分支上运行了git merge some_other_branch
。我还没有将更改推送到origin主分支。如何撤消合并?
合并后,git status
显示:
# On branch master
# Your branch is ahead of 'origin/master' by 5 commits.
如何撤销所有这些提交?
我不小心在本地主分支上运行了git merge some_other_branch
。我还没有将更改推送到origin主分支。如何撤消合并?
合并后,git status
显示:
# On branch master
# Your branch is ahead of 'origin/master' by 5 commits.
如何撤销所有这些提交?
最简单的答案是odinho-Velmont给出的答案。
首先执行git reset --merge ORIG_HEAD
对于那些想要在推送更改后重置的人,请执行以下操作 (因为这是任何git reset合并问题的第一篇帖子)
git push origin HEAD --force
这将以一种方式重置,以便在拉取后不会再次获得合并的更改。
仅供参考,我一直遵循在此处描述的分支模型:http://nvie.com/posts/a-successful-git-branching-model/,因此通常会使用--no-ff
(不快进)合并。
我刚刚阅读了这个页面,因为我不小心将测试分支与主发行版的master分支合并,以进行部署(网站上的master即为现行版本)。 测试分支已合并另外两个分支,总共有约六个提交。
因此,要还原整个提交,我只需要使用git reset --hard HEAD^
,它就会还原整个合并操作。由于合并不是快进式合并,所以该合并是一个块,退回一步即是“未合并的分支”。
git merge --abort
git checkout develop
git branch -D master
git branch -t master origin/master
看,主分支和原来的状态一样了,你的错误合并已经被消除了。
策略:从一切正常的地方创建一个新分支。
原因:回滚合并很困难。有太多的解决方案,取决于许多因素,例如您是否提交或推送了合并,或者是否有新的提交自您合并以来。此外,您仍需要相对深入地了解git将这些解决方案适应于您的情况。如果您盲目地按照某些说明操作,则可能会出现“空合并”,其中没有任何内容将被合并,并且进一步的合并尝试将使Git告诉您“已经是最新的”。
解决方案:
假设您想将dev
合并到feature-1
中。
Find the revision that you want to receive the merge:
git log --oneline feature-1
a1b2c3d4 Merge branch 'dev' into 'feature-1' <-- the merge you want to undo
e5f6g7h8 Fix NPE in the Zero Point Module <-- the one before the merge, you probably want this one
Check it out (go back in time):
git checkout e5f6g7h8
Create a new branch from there and check it out:
git checkout -b feature-1
合并:git merge dev
解决您的合并冲突。
提交:git commit
当您对结果满意时,删除旧分支:git branch --delete feature-1
只需创建新分支,然后将所需提交(cherry-pick)到该分支即可。
这比上面许多答案中描述的重置(resets)更安全简单。
git rebase -i [hash] [branch_name]
,其中[hash]
是您想要倒回的标识哈希值加一(或者您想要回退的提交数量),然后在编辑器中删除您不再需要的提交行。保存文件。退出。祈祷。这样就应该回退了。您可能还需要执行git reset --hard
,但此时应该已经完成了。您也可以使用此方法从堆栈中提取特定的提交,如果您不想将它们保留在历史记录中,但这可能会使您的存储库处于您可能不想要的状态。First, make sure that you've committed everything.
Then reset your repository to the previous working state:
$ git reset f836e4c1fa51524658b9f026eb5efa24afaf3a36
or using --hard
(this will remove all local, not committed changes!):
$ git reset f836e4c1fa51524658b9f026eb5efa24afaf3a36 --hard
Use the hash which was there before your wrongly merged commit.
Check which commits you'd like to re-commit on the top of the previous correct version by:
$ git log 4c3e23f529b581c3cbe95350e84e66e3cb05704f
commit 4c3e23f529b581c3cbe95350e84e66e3cb05704f
...
commit 16b373a96b0a353f7454b141f7aa6f548c979d0a
...
Apply your right commits on the top of the right version of your repository by:
By using cherry-pick (the changes introduced by some existing commits)
git cherry-pick ec59ab844cf504e462f011c8cc7e5667ebb2e9c7
Or by cherry-picking the range of commits by:
First checking the right changes before merging them:
git diff 5216b24822ea1c48069f648449997879bb49c070..4c3e23f529b581c3cbe95350e84e66e3cb05704f
First checking the right changes before merging them:
git cherry-pick 5216b24822ea1c48069f648449997879bb49c070..4c3e23f529b581c3cbe95350e84e66e3cb05704f
where this is the range of the correct commits which you've committed (excluding wrongly committed merge).
git stash
git branch -d the_local_branch
git checkout -t <name of remote>
git stash apply
这对我有用..!!