Wednesday, May 10, 2017

Undoing a merge in Sourcetree/Git

Merging some changes into your master branch, then realizing you need an undo function, is stressful. Fortunately, whether or not you've pushed your merge, you can still reverse what you did. There's no actual "undo" in Git, but you can either reset the master branch, or reverse your changes.

If you haven't pushed to the origin master branch yet, you can reset the master branch to the previous commit before the merge. You'll want to reset using "Hard" mode so that your working copy of master will have the changes removed.

Sourcetree: 
Right-click the previous commit on the master branch, select "Reset current branch to this commit" and select "Hard" mode.

Git Command Line:
git reset --hard XXXXXXX (insert the Commit ID)

If your merge to master has been pushed, then you will need to do a reverse commit in the feature branch, which reverts everything in that commit, merge to master, then push master again. Later on, if you want to re-merge what was in the feature branch to master once again, you will have to reverse commit the reversion itself in the feature branch. This will reinstate the changes you had removed.

No comments:

Post a Comment