Introducing Oz: the orchestration platform for cloud agents.

Learn More.

Undo a Git Merge

Philip Wilkinson

Philip Wilkinson

Software Engineer, Amazon

Updated: 6/27/2024

Published: 8/8/2023

About Terminus

The short answer

In Git, the easiest and safest way to undo a merge is to create a new commit that reverts the changes introduced by a specific commit using the git revert command as follows:

Adding the option -m 1 to the git revert command tells Git that you want to keep the parent side of the merge (the branch you merged into). If you want to keep the side of the branch merged, you change the 1 to a 2 instead.

For example:

Easily retrieve this command using Warp’s AI Command Suggestions

If you’re using Warp as your terminal, you can easily retrieve this command using the Warp AI Command Suggestions feature:

Entering git revert merge in the AI Command Suggestions will prompt a git command that can then be quickly inserted into your shell by doing CMD+ENTER.

Undoing a merge by discarding all changes

To completely discard all changes in the working directory and the staging area that have not been committed so that the state of the repository matches a specific commit exactly, you can use the git reset command with the --hard flag as follows:

Note that this command will effectively rewrite the commit history by removing all the commits after the specified commit.

For example:

Undoing a merge and preserving changes

To reset the repository to a specific commit and keep the local changes in the working directory and the staging area that are not conflicting with the changes introduced by the reset, you can use the git reset command with the --merge flag as follows:

Note that this command will automatically abort if conflicting changes are detected.

For example:

Undoing local vs remote merges

It is important to note that unlike git revert, git reset rewrites the history of the branch so you need to use this with caution, especially if the branch has already been pushed to a remote repository.

Undoing a local merge as opposed to one that has already been pushed to a remote repository differs in the implications for the history of the repository. To undo a local merge you can use any of the methods discussed above as long as you are okay with changing the history of your repository.

However, if the merge has already been pushed to a remote repository, you should be cautious about using git reset and git push --force as this can cause issues for collaborators who have already pulled the changes. In such cases it is generally recommended to use git revert to create a new commit that undoes the changes without altering the history.

Written by

Philip Wilkinson

Philip Wilkinson

Software Engineer, Amazon

Filed Under

Related Articles

Undo A Git Pull

How to effectively remove the commits introduced by a pull in Git using git-reset and preserve your local changes using git-stash. Also, how to cancel an unmerged pull request on GitHub.

Git
Glory Kim

Prompt Show Git Branch In Prompt

Enhance your terminal with a custom Git prompt. Learn different ways to integrate this contextual info, from custom shell functions to Warp context chips and toolkits like Starship and P10K.

Git
Gabriel Manricks

How To Remove Secrets From The Git History Remove Secrets From The Git History

Learn how to remove secrets from the Git history using the BFG and git-filter-repo command-line tools.

Git
Utsav Poudel

Adding a Submodule in Git

This post will show you how to simply add a submodule to a local repository, clone a repository with a submodule, and work within a repository that has a submodule.

Git
Philip Wilkinson

Undo a git push

This post will show you had to simply undo a git push three different ways.

Git
Philip Wilkinson

Undo Git Add

Learn how to effectively use 'git add' to stage files in Git for committing, and discover two powerful methods to undo accidental stagings.

Git
Glory Kim

Undo a Git Rebase

This post will show you how to undo a rebase using git reset, git rebase and git revert

Git
Philip Wilkinson

Git Push Origin

A breakdown of git push origin

Git
Amanda Khoo

Create Folder In GitHub Repository

Learn how to create and push one or more empty directories in a Git repository using `.placeholder` and `README.md` files using both the CLI and the GitHub interface.

Git
Razvan Ludosanu

Git Push Tags

This post will show you how to push a single tag, multiple tags, all tags, and tags with commits.

Git
Philip Wilkinson

Undoing Git Commits

Explore ways to undo a commit, including git reset, git checkout, and git revert with git while preserving commit history.

Git
Philip Wilkinson

Delete Local Git Branch

Learn how to delete local branches from your git repository, including ones with unmerged changes, as well as local remote-tracking branches.

Git
Philip Wilkinson

Trusted by hundreds of thousands of professional developers

Download Warp to get started

Download for Mac
.deb

Debian, Ubuntu

.rpm

Red Hat, Fedora, SUSE

Download for Windows

For Windows 11/10 x64

Download for Windows

For Windows 11/10 ARM64

On a mobile device? Send Warp to your work station.