Undo a Git Merge That Hasn’t Been Pushed Yet?

Better Stack Team
Updated on June 24, 2024

To undo a Git merge that hasn't been pushed yet, you can use the git reset command to move the branch pointer back to its state before the merge occurred. Here's how you can do it:

Step 1: Identify the Commit Before the Merge

First, you need to identify the commit before the merge. You can use git log to view the commit history and find the commit before the merge commit:

 
git log

Look for the commit hash or commit message before the merge commit.

Step 2: Reset the Branch

Once you've identified the commit before the merge, use git reset to move the branch pointer back to that commit:

 
git reset --hard <commit-before-merge>

Replace <commit-before-merge> with the commit hash or branch name before the merge.

Step 3: Clean up Unstaged Changes (Optional)

After resetting the branch, you may have unstaged changes in your working directory. If you don't want to keep these changes, you can use git reset --hard HEAD to discard them:

 
git reset --hard HEAD

Note:

  • Be cautious when using git reset --hard, as it will discard any changes in your working directory and staging area.
  • This method works if the merge hasn't been pushed yet. If the merge has already been pushed, you may need to use additional commands to revert the changes and push them back to the remote repository.
Got an article suggestion? Let us know
Explore more
Git
Licensed under CC-BY-NC-SA

This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.

Make your mark

Join the writer's program

Are you a developer and love writing and sharing your knowledge with the world? Join our guest writing program and get paid for writing amazing technical guides. We'll get them to the right readers that will appreciate them.

Write for us
Writer of the month
Marin Bezhanov
Marin is a software engineer and architect with a broad range of experience working...
Build on top of Better Stack

Write a script, app or project on top of Better Stack and share it with the world. Make a public repository and share it with us at our email.

community@betterstack.com

or submit a pull request and help us build better products for everyone.

See the full list of amazing projects on github