Wednesday, June 6, 2018

Force Merge With Git

We use git through VSTS for our source control repository and CI/CD processes, and I love it. However, sometimes it can make my life a bit tricky. Recently, another developer accidentally merged code straight from the dev branch into the master branch. We caught the mistake and used the Revert feature of VSTS to roll the changes back. We thought we were all good then, but we were wrong.

When you use Revert in VSTS what you're actually doing is creating two new commits. VSTS takes your source code back to what it was before the commit you're reverting and then for some reason makes two additional commits. I wouldn't think that'd be a problem, but it was. When we completed our work and tried to merge up to master we got all sorts of merge conflicts that we were unable to resolve. (On a side note, there's a cool plug-in for VSTS from Microsoft Dev-Labs that allows you to resolve merge conflicts in the browser: https://marketplace.visualstudio.com/items?itemName=ms-devlabs.conflicts-tab).

What I ended up doing was temporarily allowing myself to override branch policies, reverting the master branch to the commit before the accidental merge, force pushing master into VSTS, then creating a new merge. It was pretty easy once I figured out what to do, but as usual I wanted to document it here for future reference.

Here are the git commands I used:
git checkout master
git pull
git reset --hard [commit id]
git push -f
Create new Pull Request in VSTS

No comments:

Post a Comment