Lately I've been spending some of my free time giving back to the Python community, sending in patches and doing patch reviews for the core Python project. The Python project uses Mercurial for its source code management. So I've been able to get a little more familiar with Mercurial as of late. This led me to learn about the rollback command, which I've found to be helpful.

A while ago I was lucky enough to watch the live stream of the “Fringes of Git” video by O'Rielly (which you can view here). In the video I learned about Git's reflog and how it keeps commits relative to the HEAD (or TIP for you hg users). So I had the bright idea of trying to implement Mercuial's rollback command by using an alias in Git.

For starters I put this alias into my .gitconfig file (avoid adding the alias block if you already have one):

[alias]

rollback = reset –hard HEAD@{1}

There... now you have the “git rollback” command.

Here was the process I used to test how similar the git version of rollback is to the mercurial one:
created two directories: test_hg and test_git
ran hg init in test_hg and git init in test_git:q
then just created a file called test.txt, added “test 1” into both directories, then committed appropriately.
Repeated the process for “line 2” and “line 3” and committing the additions.
This created a hg log like this:

changeset: 2:fddfdadb02cc

tag: tip

user: Bryce Verdier <bryce@scrollingtext.org>

date: Tue May 1011:06:302011-0700

summary: test3

changeset: 1:02ab8efc62ce

user: Bryce Verdier <bryce@scrollingtext.org>

date: Tue May 1011:06:222011-0700

summary: test2

changeset: 0:ca1341dfc464

user: Bryce Verdier <bryce@scrollingtext.org>

date: Tue May 1011:06:142011-0700

summary: test1

and a git log like this:

commit 0357aa15e585692abda6e0c36213fce9f80523a4

Author: Bryce Verdier <bryce@scrollingtext.org>

Date: Tue May 1011:07:312011-0700

test3

commit 7e2d21450b0e085ca57ac8e15b7f5b46314c1264

Author: Bryce Verdier <bryce@scrollingtext.org>

Date: Tue May 1011:07:242011-0700

test2

commit d165a9825e96d40608b1da4319321c272424e019

Author: Bryce Verdier <bryce@scrollingtext.org>

Date: Tue May 1011:07:152011-0700

test1

Next I ran “git rollback” and got a log like so:

commit 7e2d21450b0e085ca57ac8e15b7f5b46314c1264

Author: Bryce Verdier <bryce@scrollingtext.org>

Date: Tue May 1011:07:242011-0700

test2

commit d165a9825e96d40608b1da4319321c272424e019

Author: Bryce Verdier <bryce@scrollingtext.org>

Date: Tue May 1011:07:152011-0700

test1

And when I did the same for mercurial, I got a similar result:

changeset: 1:02ab8efc62ce

tag: tip

user: Bryce Verdier <bryce@scrollingtext.org>

date: Tue May 1011:06:222011-0700

summary: test2

changeset: 0:ca1341dfc464

user: Bryce Verdier <bryce@scrollingtext.org>

date: Tue May 1011:06:142011-0700

summary: test1

A difference showed up though when I ran the command a second time. When I ran “git rollback” this was my log result:

commit 0357aa15e585692abda6e0c36213fce9f80523a4

Author: Bryce Verdier <bryce@scrollingtext.org>

Date: Tue May 1011:07:312011-0700

test3

commit 7e2d21450b0e085ca57ac8e15b7f5b46314c1264

Author: Bryce Verdier <bryce@scrollingtext.org>

Date: Tue May 1011:07:242011-0700

test2

commit d165a9825e96d40608b1da4319321c272424e019

Author: Bryce Verdier <bryce@scrollingtext.org>

Date: Tue May 1011:07:152011-0700

test1

And when I ran “hg rollback” a second time I received an error:“no rollback information available”

At this moment I'm not very knowledgeable in how Mercurial deals with its logs. But at this cursory glance it appears as if the rollback command completely removes all traces of the last commit. (Someone please correct me if I'm wrong.) Also, the rollback does not go back any further than one commit. I honestly think that this might be a good thing because it could keep you from blindly shooting yourself in the foot, if you need to go further back than one commit there is always the revert command.

While I don't know much about Git's internals either. However I can at least make sense as to why the rollback alias functions this way. By using the reflog to revert to the last commit, I end up undoing my undue. Which can be seen when we review the reflog after running the rollback command twice:

0357aa1 HEAD@{0}: HEAD@{1}: updating HEAD

7e2d214 HEAD@{1}: HEAD@{1}: updating HEAD

0357aa1 HEAD@{2}: commit: test3

7e2d214 HEAD@{3}: commit: test2

d165a98 HEAD@{4}: commit (initial): test1

This makes sense to me if I focus on two things in particular in the log above. 1) Git's SHA1 hashes in the first column. And 2) looking at the log in reverse. Starting with the last line, we see the first commit. And going up to the second to last line we have the second commit. If you notice the hash values for “commit: test 2” and “commit: test 3”, those are the same hashes used for both of the git rollback commands, the second and first lines of the log. So instead of removing an entry from the log, git is just moving the head to the last commit.

While my alias isn't a complete copy of Mercurial's rollback command. I do feel that the abstract idea behind it is still correct. And hopefully I've done something to help make Git a little easier to use for someone out there besides myself.

Thank you for your patience as you all (I'm sure) anxiously await my next posting.

Not only has it been a really long time since I posted something technical, it's also been a really long time since I posted a Project Euler solution. On that note, let's go over the challenge:
The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17.
Find the sum of all the primes below two million.

After about a month of fighting with ISP's and registrars the blog has returned. I haven't stopped writing during this time and I plan on posting a new post next week. Once all the DNS servers are done propigating my server's IP around the interwebs of course.

I hope everyone has been well and I look forward to your comments as I continue to expand the content of this blog.

I'm sorry for the delay in writing. Unfortunately I am not updating the site with another blog post. I am giving everyone a quick heads up that I am changing web hosts and the site might be having issues over the next few days. You have been warned.