The interactive rebase feature of Git is one of my favorites. I love being able to squash multiple commits into one commit or re-order my commits for clarity. My one complaint is the Git interactive rebase order is wrong (at least for the way my brain works).
When viewing commits with
git log the newest commits appear at the top. This makes sense as it matches my mental model of a programming stack.
In this example, I’m using
git log --pretty=oneline --abbrev-commit for brevity.
$ git log --pretty=oneline --abbrev-commit 782e25f (HEAD -> master) Fourth 4255067 Third 3351a8e Second 851d968 First ca58991 Initial commit with README.md
If you’re interested in an easy way to get a better git log like this, see my post on how to improve git log.
Git Interactive Rebase
When I use Git interactive rebase (like in the following command), the commits appear in the reverse order (with the newest commit at the bottom instead of the top)
$ git rebase --interactive HEAD~4
pick 851d968 First pick 3351a8e Second pick 4255067 Third pick 782e25f Fourth # Rebase ca58991..782e25f onto ca58991 (4 commands) # # Commands: # p, pick = use commit # r, reword = use commit, but edit the commit message # e, edit = use commit, but stop for amending # s, squash = use commit, but meld into previous commit # f, fixup = like "squash", but discard this commit's log message # x, exec = run command (the rest of the line) using shell # d, drop = remove commit # # These lines can be re-ordered; they are executed from top to bottom. # # If you remove a line here THAT COMMIT WILL BE LOST. # # However, if you remove everything, the rebase will be aborted. # # Note that empty commits are commented out
While I understand when something is appended to a file it goes on the end of the file and this mental model matches how commits are displayed with Git interactive rebase, that knowledge does not help change the fact that I need to perform a mental shift when I switch between git log and git rebase –interactive
Not the Only One
I’m not the only one who feels it would be more intuitive if the order of commits in git interactive rebase were reversed, as evidenced by this post from May 2011.
Since I use the Vim text editor when working with Git, I wrote a Vim plugin to reverse the order of the commits in an interactive git rebase.