PDF download Download Article
Learn these two easy methods to roll back a merge with Git
PDF download Download Article

If you are working with Git, you may be wondering how to undo a merge. Perhaps you accidentally merged branches before you were ready, or you discovered an issue with the latest commit and want to go back to a previous version. Undoing a merge with Git is simple, but how you do it depends on whether you're working locally or pushing your merge to a remote repository.

Things You Should Know

  • Use "git reset" to roll back to a previous commit and erase all changes after that commit.
  • Use "git revert" to make a new commit out of a previous commit with any changes removed.
  • "Git revert" is the safest way to undo a merge to avoid errors or losing any repo history.
Method 1
Method 1 of 2:

Undoing a Local Merge with Git Reset

PDF download Download Article
  1. The git reset command will roll the branch back to a previous commit, essentially erasing part of your repository's history. If you're working with Git locally, this may not be a big deal to you. [1]
    • If you don't want to alter your repository's history, consider using the git revert method listed below.
    • When using git reset , you don't need to remember the commit hash, which can save some time.
  2. Alternatively, you can use HEAD~1 to reference the commit before the current head or ORIG_HEAD to reference the commit before the last merge.
    Advertisement
  3. If your branch has any uncommitted changes, --merge will keep them. --hard will remove all changes after the merge, including any uncommitted changes.
    • If you are certain that there have been no additional uncommitted changes after merging, you can use the --hard flag. However, --merge is usually the safer option.
    • Alternatively, if you want to use the --hard flag but want to save any uncommitted changes, you can use the git stash command.
    • Use git stash to stash away your uncommitted changes, then use git stash pop or git stash apply to reapply your stashed changes.
    • "Pop" will remove the changes from your stash and apply them to your working copy, and "apply" will keep the changes in your stash and apply them to your working copy. [2]
  4. Replace [flag] with --merge or --hard (as determined in step 3) and replace [commit before merge] with the commit hash, HEAD~1 , or ORIG_HEAD (as determined in step 2).
    • The branch will be rolled back to the commit hash specified, and any history after that commit will be discarded.
  5. Advertisement
Method 2
Method 2 of 2:

Undoing a Pushed Merge with Git Revert

PDF download Download Article
  1. If you've already pushed a merge to a remote repository , resetting the commit may create issues if a colleague has already pulled the change. [3]
    • Using git revert is a better option for pushed merges because it won't erase the merge history and will make a new commit that undoes the changes.
    • You can use git revert with a local merge as well, but git reset may be beneficial because you don't necessarily need to know the commit hash.
  2. Unlike the previous method, make sure you grab the commit hash for the merge that you want to undo, not the hash before the merge.
    • Use git reflog for more readability, but either command will work.
  3. Running this command will create a new commit that undoes the changes in the merge. In this command, -m 1 signifies that Git should keep the side of the branch you merged into (such as main or another branch name).
    • If you want to keep the side of the branch you merged, change -m 1 to -m 2 .
  4. Advertisement

Expert Q&A

Search
Add New Question
  • Question
    Are there any other methods to undo a merge in Git?
    Théo Dufort
    Software Engineer and Full-Stack Web Developer
    Théo Dufort is a Software Engineer and Full-Stack Web Developer based in Quebec, Canada. With nearly 6 years of experience, Théo is a full-stack developer specializing in web development. At just 16, he launched his own consulting business to advise on all things web design and development. Most recently he created MyBookQuest, designed to be an all-in-one platform for book lovers to track, organize, and fill their personal library. MyBookQuest aims to fill the gaps of the popular Goodreads app owned by Amazon by rewarding its users with points to give them different perks like discount coupons as an incentive to read, review, and rate their latest book.
    Software Engineer and Full-Stack Web Developer
    Expert Answer
    You can also use git rebase or git checkout to undo a merge in Git. With git rebase, you can roll back to a specific commit by using its SHA (which is the unique identifier for the commit). This essentially moves your project’s history to a point before the merge, allowing you to rebuild from there. Another option is git checkout, where you can check out an earlier commit and create a new branch from that point, which bypasses the merge altogether.
Ask a Question
      Advertisement

      Tips

      Submit a Tip
      All tip submissions are carefully reviewed before being published
      Name
      Please provide your name and last initial
      Thanks for submitting a tip for review!

      Expert Interview

      Thanks for reading our article! If you’d like to learn more about software testing, check out our in-depth interview with Théo Dufort .

      About This Article

      Thanks to all authors for creating a page that has been read 5,279 times.

      Is this article up to date?

      Advertisement