Git怎样撤销一次分支的合并Merge
伊谢尔伦
伊谢尔伦 2017-04-21 11:16:49
0
6
861

合并分支后,发现合并错了分支。
master上还没有提交任何东西,想取消这次合并,让分支回到位,肿么办?

好在现在还没commit另外的内容,如果不小心commit了,那又该怎么办啊?

伊谢尔伦
伊谢尔伦

小伙看你根骨奇佳,潜力无限,来学PHP伐。

reply all(6)
黄舟

What a weird management tool git is. There can be so many explanations for a problem, and you can’t tell which one is right at a glance.
http://git-scm.com/blog/2010/03/02/undoing-merges.html This is the official answer. After I finish reading it, I will translate it here.

====================== Simple translation starts====================

Because too many people asked how to undo a merge, git officially issued this tutorial to show how to achieve the goal of undoing a merge under the current ideological system of git.

Method 1, reset to the version before merge, and then redo the next operation. Each collaborator is required to know how to roll back the local HEAD:

$ git checkout 【行merge操作时所在的分支】
$ git reset --hard 【merge前的版本号】

Method 2, when there are other operations and changes after the merge, git also has a way to undo the merge, use git revert:

$ git revert -m 【要撤销的那条merge线的编号,从1开始计算(怎么看哪条线是几啊?)】 【merge前的版本号】
Finished one revert.
[master 88edd6d] Revert "Merge branch 'jk/post-checkout'"
 1 files changed, 0 insertions(+), 2 deletions(-)

This will create a new commit to offset the corresponding merge operation, and later git merge [the branch represented by that number] will prompt:

Already up-to-date.

Because using method 2 will make git mistakenly think that the things in this branch are things we don’t want.

Method three, how to cancel method two:

$ git revert 【方法二撤销merge时提交的commit的版本号,这里是88edd6d】
Finished one revert.
[master 268e243] Revert "Revert "Merge branch 'jk/post-checkout'""
 1 files changed, 2 insertions(+), 0 deletions(-)

That’s it, you can merge normally, but there may be a lot of conflicts! !

$ git merge jk/post-checkout
Auto-merging test.txt
Merge made by recursive.
 test.txt |    1 +
 1 files changed, 1 insertions(+), 0 deletions(-)

======================End of simple translation====================

In the end, I still feel that the top ones are all too troublesome. For those who use WebStorm to write code on the front end every day, take a look: WebStorm Right-click the project file or folder, and there is this:

Local History -> Show History

After clicking it, a window will appear where you can see all local changes. Find the right one and press the one in the upper left corner:

Revert

The

button, which is a small purple curved arrow, goes back. It is very useful when the file size is not large and is highly recommended.

伊谢尔伦

If you are sure to abandon the commit of this merge, if you merged the wrong branch to master, first create a new branch reference for the current commit through git reflog 或者 gitg、gitk、qgit 等工具确定你 merge 之前 master 所在的 commit,然后在 master 分支上使用 git reset --hard <commit> 重置头指针。一般来说,在 master 上直接执行 git reset --hard HEAD~ 也可以回到合并之前的提交,但 git reset --hard 命令还是使用确定的 commit 为好。注意,git reset --hard 命令有风险,除非十分确定要放弃当前提交,否则最好先 git branch and then continue. Delete it after confirming that it is correct.

If there are new commits after the wrong merge, you can pass the git rebase --onto <错误的合并提交> <正确的合并提交> <新提交所在分支> 来在正确的合并提交上重建新的提交。git rebase --onto 命令所重建的提交序列最好是线性的,否则非线性的提交会变成线性的。若需要保存非线性的提交历史,可以考虑使用 --preserve-merges parameter after completing the correct merge mentioned above, but the result is very unreliable, depending on the degree of non-linearity of the commit.

PHPzhong

B-R-A-N-C-H
/ --Merge
M-A-S-T-E-R----M

You can use git reset --hard R’s hash value and return to R. Return from M after Merge to R.

小葫芦

I think git merge --abort is needed here

伊谢尔伦

If you encounter a conflict during merging and want to cancel the operation and restore the index, use git merge --abort
git reset --hard can roll back to a certain commit
git revert can undo a commit, and undoing will generate a new commit

I haven’t studied git rebase carefully yet.

巴扎黑

http://opensource.apple.com/source/Git/Git-26/src/git-htmldocs/howto/revert-a-faulty-merge.txt

Who has time to translate it?

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template