• Modern UX

    Edit and navigate faster in the terminal with Warp's IDE-like input editor.

  • Warp AI

    AI suggests what commands to run and learns from your documentation.

  • Agent Mode

    Delegate tasks to AI and use natural language on the command line.

  • Warp Drive

    Save and share interactive notebooks, workflows, and environment variables.

  • All Features

Undo a Git Merge

Thumbnail for Philip WilkinsonPhilip 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:

$ git revert -m 1 <commit_hash>

Run in Warp

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:

$ git log --oneline
42d4df9 (HEAD -> master) add server post endpoint
b575a79 create server
$ git revert -m 1 42d4df9
[master c21acf3] Revert "add server post endpoint"
 1 file changed, 5 deletions(-)
$ git log --oneline
c21acf3 (HEAD -> master) Revert "add server post endpoint"
42d4df9 add server post endpoint
b575a79 create server

Run in Warp

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:

Thumbnail for

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:

$ git reset --hard <commit_hash>

Run in Warp

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

For example:

$ git log --oneline
42d4df9 (HEAD -> master) add server post endpoint
22293de update server port
b575a79 create server
459ad3a add package manifest
$ git reset --hard b575a79
HEAD is now at b575a79 create server
$ git log --oneline
b575a79 (HEAD -> master) create server
459ad3a add package manifest

Run in Warp

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:

$ git reset --merge <commit_hash>

Run in Warp

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

For example:

$ git reflog
644bbfe (HEAD -> master, development) HEAD@{0}: merge development: Fast-forward
ab1cbd2 HEAD@{1}: checkout: moving from development to master
644bbfe (HEAD -> master, development) HEAD@{2}: commit: add put endpoint
ab1cbd2 HEAD@{3}: checkout: moving from master to development
42d4df9 HEAD@{4}: commit: add server post endpoint
22293de HEAD@{5}: commit: update server port to 8000
b575a79 HEAD@{6}: commit: create server in index.js
edb35eb HEAD@{7}: commit: add package manifest
c00168e HEAD@{8}: commit: add log in index.js
459ad3a HEAD@{9}: commit (initial): add file index.js
$ git reset --merge 42d4df9

Run in Warp

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

Thumbnail for Philip WilkinsonPhilip 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
Thumbnail for Glory KimGlory 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
Thumbnail for Gabriel ManricksGabriel 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
Thumbnail for Utsav PoudelUtsav 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
Thumbnail for Philip WilkinsonPhilip Wilkinson

Undo a git push

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

Git
Thumbnail for Philip WilkinsonPhilip 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
Thumbnail for Glory KimGlory Kim

Undo a Git Rebase

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

Git
Thumbnail for Philip WilkinsonPhilip Wilkinson

Git Push Origin

A breakdown of git push origin

Git
Thumbnail for Amanda KhooAmanda 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
Thumbnail for Razvan LudosanuRazvan Ludosanu

Git Push Tags

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

Git
Thumbnail for Philip WilkinsonPhilip 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
Thumbnail for Philip WilkinsonPhilip 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
Thumbnail for Philip WilkinsonPhilip Wilkinson

Trusted by hundreds of thousands of professional developers

Download Warp to get started

Download for Mac
Thumbnail for null