Reverted commit in master, how to pull master down without effects of revert?

407 views Asked by At

Several weeks ago I pushed something to master that ended up needing to be reverted.

Now I've made the changes and am ready to push them to master again. My normal workflow is to checkout master, pull the latest version from origin, checkout the feature branch, merge in the new changes from master, and then make a PR to master.

When I try to merge in the changes from master, the revert commit is deleting lots of stuff in my feature branch that I need to keep.

What is the best way to handle this?

1

There are 1 answers

1
tkruse On BEST ANSWER

So what you are saying is that on origin master has been reverted, but you need to add the reverted changes back in? Like

                     [branch feature/A-Fixed]
                   o commit A-fixed
[branch master]    |
commit revert A *  |
                | /
                |/
       commit A o
                |
                .

In this case merging master into your feature branch means the revert commit marked with * in the above graph will be added to your changes, so that's why stuff is removed from your branch.

So what you can do is to create a new branch starting at the revert commit, then revert the revert again (un-revert<), merge this branch to your feature branch, and then merge master to your branch.

git checkout <reverted> -b revert-branch
git revert <reverted>
git checkout feature/fixed-A
git merge revert-branch # rebase would also work
git branch -D revert-branch
git merge master
#...  continue with your normal workflow

That way, when merging master, the revert is already in your feature branch, but cancelled out by the unrevert commit, so the revert will not be added to your changes.

                        [branch feature/A-Fixed]
                      o merge revert-branch
                     /|
                    / | [previous state of branch feature/A-Fixed]
                   |  o commit A-fixed
                   |  |
                   o commit revert revert A
[branch master]    |  |
    new commit  *  |  |
                | /  /
                |/  /
commit revert A o  /
                | /
                |/
       commit A o
                |
                .

You can see that now the revert commit is already on your feature branch, so merging master may add some other commit marked with * above, but not the revert.

Generally in the situation that something has been reverted on master, and you want to work on it and push it again, you should start at the revert commit and unrevert first (git revert revert-commit).