Friday, September 12, 2014

Fixup Git

Looking back at my Git commit history, I realize it looks quite ugly. I frequently make very small commits - like fixing a typo, then fixing another typo (since the first one was not actually a typo), fixing it again etc etc. It really looks ugly.

I am finally learning the power of rebase and squash (fixup) in history management. While I have not applied it to my real projects yet (I've only experimented on a test project), I am really looking forward to using it in the future.

Basically, the rebase command allows you consolidate however many commits you want into one. To do that, one needs to call
git rebase -i HEAD~N, where N is the number of commits to consolidate, counting from the last one. For example, 4.

You then see a screen, which lists the commits to be rebased, for example:

pick aaaaa 'commit message1'
pick bbbb 'commit message2'
pick ccccc 'commit message3'
pick dddd 'commit message4'


There are also several options: pick, edit, squash and fixup. I tried squash and fixup - they are both the same, except that squash shows the commit logs and fixup doesn't. So I guess if it is necessary to hide some particularly bad commit logs, fixup is the choice.

Assuming we want to consolidate three commits with the first one, the command console will look like that:

pick aaaaa 'commit message1'
fixup bbbb 'commit message2'
fixup ccccc 'commit message3'
fixup dddd 'commit message4'


Once the changes have been applied, the project will only show one commit: that is, commit message1. And now the history looks much cleaner.

Now, I still need to learn how to fix any issues when the rebase command was applied incorrectly. So far, Git has been winning over in these situations (though not for long, I bet).
 
 

No comments :

Post a Comment