Download Article
Learn these two easy methods to roll back a merge with Git
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.
Steps
-
Determine if you should use git reset over git revert . 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] X Research source
- 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.
-
Run git log or git reflog to get the commit hash before the merge. 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
-
Determine if you should use the --merge flag or --hard flag. 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] X Research source
-
Run git reset [flag] [commit before merge] . 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.
Advertisement
-
Determine if you should use git revert over git reset . 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] X Research source
- 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.
-
Run git log or git reflog to get the commit hash. 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.
-
Run git revert -m 1 [merge commit hash] . 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 .
Advertisement
Expert Q&A
Search
-
QuestionAre there any other methods to undo a merge in Git?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.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!
References
About This Article
Thanks to all authors for creating a page that has been read 4,949 times.
Advertisement