In Git, there are methods to undo (roll back) a merge operation

In Git, there are several ways to undo (rollback) merge an operation, depending on whether a commit has been made, whether a push has been made, and whether a history needs to be retained. Here are some common undo  merge methods:


1. Not submitted  merge (not submitted  commit)

If  merge you haven’t submitted it yet, you can use the following methods to undo it:

git merge --abort
  • This will terminate the current merge operation and restore the system to its state before the merge.
  • This applies to situations where conflicts arise during the merger process, but have not yet been  git commit resolved.

2. Submitted merge, but not yet pushed.

If  merge you have committed but not yet pushed to the remote repository, you can use  git reset a rollback:

git reset --hard HEAD~1
  • HEAD~1 This indicates reverting to the previous commit (i.e.,  merge the previous state).
  • Using this  --hard will discard all uncommitted changes; use with caution.

3. Already pushed out merge

If  merge you have already pushed to the remote repository, you can use the  following method git revert to generate a reverse commit:

git revert -m 1 <merge-commit-id>
  • -m 1 This indicates that changes to the main branch are retained (i.e., changes to the merged branch are undone).
  • This creates a new commit, undoes  merge the changes, but preserves the history.

4. Use  git reflog the history retrieval function

If a commit is lost due to an accidental operation, you can  git reflog view the operation history and restore it:

git reflog  # View operation logs
git reset --hard <commit-id>  # Restore to specified submission
  • This is suitable for recovering  merge the state before an accidental operation.

5. Force rollback of remote branches (use with caution)

If  merge the push has already been made, and you need to completely delete the merged record (which will modify the history):

git reset --hard <commit-before-merge>
git push --force origin <branch-name>
  • Note : This will overwrite remote branches, which may result in the loss of code from other collaborators. Use with caution.

6. Cancel revert(Re-merge)

If you previously used  git revert the undo function  merge, but later need to merge again:

git revert <revert-commit-id>  # Revoke the previous revocation
git merge <branch-name>      # remerge
  • This applies to merge operations that need to be reversed.

Summarize

ScenemethodApplicable situations
Not submitted mergegit merge --abortMerge conflict not submitted
Submitted but not yet pushedgit reset --hard HEAD~1Local rollback
Pushedgit revert -m 1 <commit-id>Keep history
Force rollbackgit reset --hard + git push --forceCompletely delete merged records
Recovering from Misoperationgit reflog + git resetRecover lost commits

Precautions :

  • Before using  --force push notifications, ensure that other team members are aware of the policy to avoid code conflicts.
  • git revert It is suitable for scenarios where history needs to be preserved, and also  git reset suitable for complete rollback.