One of git’s more powerful features is the ability to rewrite history with the rebase command. It looks a bit scary at first, but if you read the documentation carefully and know what you want to do before you start, it’s incredibly useful.
Case in point: I recently updated a large Python class to use the logging module instead of print statements (I wrote it many years ago when I was new to Python). I committed the changes for each method as I finished with it, as I didn’t want to worry about having a huge pile of changes in my working directory all at once. I had to do a fix in the master branch for a configuration change on the Perforce server in the middle of all this, so I was pretty happy with my strategy.
However, this left me with two dozen commits that all had nearly identical commit messages, because they were basically doing the same thing for each method. Using git’s interactive rebase (git rebase -i) I was able to quickly and easily turn all of those individual method commits into a single commit with a reworded commit message. That way I get a clean, easy to follow history without having to have all of those “216 lines added, 117 lines deleted” hanging over my head in my working directory.
Leave a Reply