Friday, July 15, 2011

Git as SVN client: rollback to previous version

Git is a very power content based DCVS. I use it as a SVN client, since my company is still using SVN. I've been quite happy until yesterday: I made a mistake in a commit and need to roll it back. The problem is I already checked in to SVN and it broke some unit tests I did not cover on my local run, but the CI server did. I'm used to local reset to revert some bad changes, but this will only help when you have not pushed changes to remote sites:
git reset HEAD~
Above command will reset your master pointing to the previous commit. From Git's point of view, it's done. The current HEAD (master) now points to the desired copy of the codes you want to have. But the problem is once you do a synchronization with SVN, it's going to be reset back to the copy you want to get rid of. This is because Git is treating master tag as a pointer. When you do the above reset, it just move the pointer back without doing any real commit. So here what we really need is a "real" commit, which "git revert" provides.
git revert HEAD
After this, if you look at the history, you'll find a commit with the new result. Now synchronize with SVN and you'll find a new commit in it and the changes you made has been rolled back.

Note, git revert should only use when you have pushed your changes to remote sites. Then it gives you a chance to fix the problem. If your change is still local, git reset is recommended.

Maybe this is stupid, but it took me a while to figure this out, hope this helps some one out there who is also bothered by this.