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.
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:
======================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.
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
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:
Method 2, when there are other operations and changes after the merge, git also has a way to undo the merge, use git revert:
This will create a new commit to offset the corresponding merge operation, and later git merge [the branch represented by that number] will prompt:
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:
That’s it, you can merge normally, but there may be a lot of conflicts! !
======================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
Thebutton, 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.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?