git undo branch merge code

        If you have merged branches in Git, but later find that the merged code has problems, and you need to undo the merge and restore to the state before the merge, you can use the following command.

1. Method 1: [Use the revert command]

Submit the code on the sub-branch, and merge the sub-branch code into a main branch in the remote factory library. The operation process is as follows:

        Switch to the merged branch on the local computer, update the latest code, then perform git revert operation on the record submitted by the sub-branch just now, and then resubmit the code of the current main branch to cancel the merged code of the previous sub-branch .

Note : Using the revert command will result in one more submission record.

2. Method 2: [log]

First, use the git log command to view the commit history and find the commit ID before the merge.

Then, use the git reset command to roll back the current branch to the state before the merge. The command format is as follows:

git reset --hard <commit-id>

where <commit-id> is the commit ID before the merge.

Note: There is no need to use the commit command to submit and save the code!

Finally, use the git push command to push the modification of the local branch to the remote warehouse. The command format is as follows:

git push -f

        Among them, the -f parameter means forced push, because the use of the git reset command to roll back the branch will cause the branch history to be modified, and a forced push is required to update the remote branch.

It should be noted that after undoing the merge, the merged modifications will be lost. If you need to keep these modifications, you can use other methods to repair them.

Note : Using the above command method will not generate one more submission record, and there is no record of previous code merging.

Question: Which branch is the above operation performed on?

        This operation is performed on the merged branch, that is, if you merged the B branch on the A branch, but after merging, you find that there is a problem and you need to undo the merge, then this operation is performed on the A branch. Specifically, you need to use the git reset command on the A branch to roll back the A branch to the state before the merge.

Guess you like

Origin blog.csdn.net/weixin_44799217/article/details/131180619