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

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.[3]
    • 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.[4]
  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.[5]
    • 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
    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.
    Théo Dufort
    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
200 characters left
Include your email address to get a message when this question is answered.
Submit
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!

You Might Also Like

Git Remove Untracked FilesUsing the "git clean" Command to Delete Untracked Files
Undo in Google Colab3 Easy Ways to Undo Changes in Google Colab
Recover Overwritten Files3 Easy Ways to Recover Overwritten Files on a Computer
Clone a Repository on Github3 Quick & Easy Ways to Clone a GitHub Repository (With Pictures)
Import a Repository on Github Import a Repository on GitHub
Recover Deleted Files from Your Computer8 Ways to Restore Deleted Files on Windows & Mac
Restore Your ComputerRestore Your Computer
Restore Your iPhone Without Updating Reset Your iPhone Without Updating It
Reboot an iPhone Reboot & Reset Your iPhone
Unjailbreak an iPhoneA Complete Guide to Unjailbreaking an iPhone
How Do I Reset My Android Without Losing DataEasy Steps to Reset Your Android Without Deleting Data
Recover Word Documents6 Effective Ways to Recover Word Documents
Revert Computer to Factory SettingsThe Complete Guide to Factory Resetting Your Mac & PC
Restart a Mac6 Ways to Restart Your Mac: Apple Menu, Shortcuts, and More
Advertisement

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.

References

  1. Théo Dufort. Software Engineer and Full-Stack Web Developer. Expert Interview
  2. Théo Dufort. Software Engineer and Full-Stack Web Developer. Expert Interview
  3. https://git-scm.com/docs/git-reset
  4. https://git-scm.com/docs/git-stash
  5. https://git-scm.com/docs/git-revert

About This Article

Théo Dufort
Co-authored by:
Software Engineer and Full-Stack Web Developer
This article was co-authored by Théo Dufort and by wikiHow staff writer, Hannah Dillon. 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. This article has been viewed 6,818 times.
How helpful is this?
Co-authors: 4
Updated: November 7, 2024
Views: 6,818
Categories: GitHub | Software Testing
Thanks to all authors for creating a page that has been read 6,818 times.

Is this article up to date?

Advertisement