A Git Recipe For Going Back In Time

The one thing I REALLY love about distributed version control is the ease of branching. There is nothing like it. If you are currently on SVN you know what a pain it is to check out another branch and switch your workspace over, or (gag) try to roll back your local. Not so wtih Git or Hg. Another thing I love is the simplicity to roll back to a previous pointin the code. Super easy, almost zero time.

QA found abug in our code that was assigned to me with these conditions:

  • Bug in QA deployment (released 2 weeks ago)
  • Bug in DEV deployment (questionable release date)
  • No bug in my development machine code.

I couldn’t pinpoint any relevant fixes in the code with a ton of developers checking in on the same files and it looked like DEV code hadn’t been released since QA although the build engineer couldn’t answer that for me.  I dug through the build numbers and it made me more suspicious.

Perfect scenario to branch, rollback, and test. So here were the steps I took from the command line:

1. Made a branch to work in:

git checkout -b oldCodeBranch

2. Check the status to make sure I’m good, and on the branch:

git status

3. Listed the commits so I could grab a commit hash to roll back to:

git log –pretty=format:”%H %cd %an %s”

4. Roll the branch back to the hash, discarding any future changes:

git reset –hard f8b720b2a3

5. Build

mvn clean install

6. Debug

A variation on steps 1-3 if you know the hash (maybe from Tortoise Git; still trying to figure out if Gitg is useful):

git checkout -b oldCodeBranch f8b720b2a3

So this solved my problem:  the bug was fixed but not yet deployed.

I found the error right away; and that the DEV environment build hadn’t been released since the QA release (grrrr what’s the point of DEV then!!!). Then, switched back to master in the blink of an eye and deleted the branch. The speed at which I could accomplish this in Git with such little pain is awesome. Plus, for this particular project I am using git-svn to front the SVN repository, but once I get the code it is *all* in git-land until I push it.

I want to stress something here: the speed an ease that I accomplished this task, compared to something like SVN.  Until a developer goes through these mundane daily tasks they can’t appreciate it and I do, now.  I really encourage using something that lets you branch and change around in your system with ease be it Git, Hg, or anything else.

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>