Selective Revert in Git

Uh oh, a mistake was made in your Git repository, and it needs to be undone. I use SourceTree for the majority of my Git-in’, and reverting changes is really easy. Just right-click a commit, choose “Reverse Commit,” answer “Yes” in the confirmation box, and the deed is one. On one hand, it’s nice because it’s so simple. On the other, it doesn’t give you much control.

I recently had to deal with a bad merge that contained many files. Some of the merge was legit, but part of it was also undoing valid changes. I wanted to partially revert the commit and pick which files/changes to keep. Unfortunately, I couldn’t find a way to do this in SourceTree, but it’s pretty simple to do from the command line.

git revert can be run with -n or –no-commit to revert a commit but not automatically commit the result.

git revert --no-commit <sha-1>

My commit was a merge, and undoing merges can be more complicated. For my purpose, simply adding -m 1 was sufficient.

git revert --no-commit -m 1 <sha-1>
Advertisements

Leave a comment

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s