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 commitresolved.
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~1This indicates reverting to the previous commit (i.e.,mergethe previous state).- Using this
--hardwill 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 1This indicates that changes to the main branch are retained (i.e., changes to the merged branch are undone).- This creates a new commit, undoes
mergethe 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
mergethe 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
| Scene | method | Applicable situations |
|---|---|---|
Not submitted merge | git merge --abort | Merge conflict not submitted |
| Submitted but not yet pushed | git reset --hard HEAD~1 | Local rollback |
| Pushed | git revert -m 1 <commit-id> | Keep history |
| Force rollback | git reset --hard + git push --force | Completely delete merged records |
| Recovering from Misoperation | git reflog + git reset | Recover lost commits |
Precautions :
- Before using
--forcepush notifications, ensure that other team members are aware of the policy to avoid code conflicts. git revertIt is suitable for scenarios where history needs to be preserved, and alsogit resetsuitable for complete rollback.