Home How-tos How To Reverse a Git Merge

How To Reverse a Git Merge


Accidentally doing the wrong thing is very common when working with Git, but luckily, it’s built to keep track of your repository’s version history. There’s always a way to reverse changes made, and if you want to undo a Git merge, it’s pretty easy.

Undoing a Git Merge

Basically, there are two ways to go about this, and it all depends on whether or not you’ve already pushed the changes to your source control. If you’ve already committed and pushed to a service like GitHub, it’s generally considered to be immutable, unless you want to force push, which is dangerous. You don’t want to ever “delete” commits, and git merges are added as “merge commits.” You’ll want to use git revert to safely undo the changes, and commit that undoing as a new commit.

However, if you just messed something up locally, cursed at your keyboard, and Googled for a solution, you can still safely delete the merge commit and keep your Git history clean with a simple git reset.

Resetting Locally

It’s easiest—and safest—to handle Git resets with a GUI Git client, where you can easily see the branch history. It’s not necessary of course, but the visual helps when you have complex branches.

In this example, we want to undo the merging of the feature branch into the main branch. Since the feature branch hasn’t actually been touched, we want to reset the “main” branch label to point to the last commit.

To do this, we want to git checkout the main branch, then reset to the previous commit. The shorthand for this is HEAD^, and we can perform a hard reset to the last commit. If you’re not comfortable with a “destructive” reset, you can do a soft reset, and then manually discard the merge commit changes.

git reset --hard HEAD^

As you can see, this completely gets rid of the merge commit, and leaves the feature branch untouched.

If the commit you need to reset to isn’t the last one before the current HEAD of the branch, you can simply copy the SHA1 ID for it, and reset to that specific commit.

Reverting a Commit That’s Already Pushed

Unfortunately, if you’ve already pushed, there’s no way to remove the merge commit without overwriting your repository history. You could do the same steps shown above, and push to the upstream repository (like GitHub) with the --force flag. But this is generally considered destructive, and unsafe when you have multiple collaborators, as they will all need to update their local repos.

The other way to undo commits in Git is to revert them. Reverting a commit will apply the opposite changes to the repository—for every line added, that line is removed. For everything deleted, it’s added back, and so on. This effectively reverses the commit, with the downside being that this process will clutter your version history a bit. Generally though, this is good practice for reversing mistakes.

Reverting a commit is pretty simple. First, you’ll need to run git log to get a list of commits and their ID numbers.

Then, you can copy the SHA1 hash, and revert the commit with git revert:

git revert 62ff517cc7c358eaf0bffdebbbe1b38dea92ba0f

You will need to push this new “revert commit” to GitHub to fix the issue.





Source link