DEV Community

Simon Plenderleith
Simon Plenderleith

Posted on • Originally published at simonplend.com on

How to restore changes which you’ve reverted from your main git branch

Reverting code changes and restoring them with git is one of those things that feels like it’s going to be straightforward, but often ends up turning your git history into a hot mess. A messy history is one thing, but not even being able to restore the code changes you reverted? That’s a nightmare!

When you start looking at the documentation for git you’ll probably end up despairing at how complicated it seems to do something which you know should be simple. Wouldn’t it be great if you could revert and restore changes whenever you need to with git? The good news is that it’s not difficult to do when you know how, but first we need to understand why your code changes aren’t being restored in the way that you expect them to be.

Why aren’t my code changes being restored?!

If you’ve ever reverted code changes from your main branch and then later tried to restore them, this git workflow is probably familiar to you:

Diagram showing the git workflow of merging a feature branch into the main branch, finding a bug, reverting the changes which were merged in, then adding a bug fix to the feature branch, merging it into the main branch again and finding that the code changes made in the feature branch before the bug fix are not showing in the main branch.

The reason that the code changes made before the bug fix are "missing" from main when you try to merge feature-branch in again is because the commits containing those changes were already merged in to main earlier.

To bring the changes back in to main which you reverted, as well as any bug fix, you need to create a fresh branch and revert the commit which reverted the changes. Huh?! Let’s head to the command line and see what that looks like.

Put ya revert down flip it and revert it

Ok, now for the good stuff: the git commands which will restore order to the universe and let you get on with your life. The following commands assume you’re in a situation where your main branch has had feature-branch merged in, and that the code changes introduced by that merge have then been reverted.

# Make sure your local main branch is up-to-date.
git checkout main
git pull

# Create a new branch off of main.
# This is where you're going to bring back the changes which were reverted.
git checkout -b restore-feature-x

# You need to look at your git log and copy the SHA hash for the commit that
# you created when you reverted the changes e.g. c4780a2bf0e22a93ece0b42cf5e42a9a21cfcbca
# You'll use this commit SHA hash which you've copied in the next command.
git log

# Revert the commit where you reverted the changes.
git revert COMMIT_SHA_HASH_YOU_JUST_COPIED

# This branch will now contain the changes which you originally reverted,
# and you can proceed to make any bug fixes or further changes that are required.
#
# You can diff this branch against the main branch to confirm that when you merge
# this branch in, main will contain all the changes that you want it to.
git diff main..
Enter fullscreen mode Exit fullscreen mode

I recommend that you follow this one simple rule for restoring changes that you’ve reverted with git: revert the revert, every time. If you don’t, you’re very likely to end up fighting git, which nobody wants to do, not even on a good day.

Top comments (0)