I personally have committed some of the worst sins possible with the Gits. I’ve been yelled at on team projects, have done things with the pull command which I’m sure Linus never intended, and have merged branches which should never, ever, be merged. But over time I’ve come to respect Git for what it is – a way to tell the story of your project source and how it has evolved over time.
So let’s jump into storyteller mode. At present I am working in a large team which has a Git repository with a number of contributors. For this reason it’s important to maintain a level of discipline when committing, as when the number of users increases, the tolerance for ambiguity in commits (both content and messages) changes in an inverse manner. Consider the following git history scenario:
"WIP" commit messages don’t tell the reader much of what has been going on in that repo. What would be much better would be to squash them into one meaningful commit, with a well worded commit message. Enter git’s interactive rebase (aka:
rebase -i). Interactive rebase allows us to ‘alter’ the history of a git repo by performing a number of changes, including removing commits, squashing commits and changing commit messages. In this case, to squash the 4
"WIP" commits into one meaningful commit requires just one simple command line command:
$ git rebase -i [sha of the commit prior to the first commit you wish to squash]
$ git rebase -i 3bea...
performing this will open your elected git text editor, presenting the following:
All we have to do now is follow Linus’ instructions, and replace the word
squash for all the commits except
fg97... and then save and close the document. Git will work its magic, will present you with a commit message editor again where you can now add a new “squashed” commit message and hey presto!, you now have a nice looking repo with a nice looking history:
Persevere with Git – it’s worth it! There are fantastic docs over at Git-SCM so you can try using the command line; with persistence you’ll find it’s 100x better than Sourcetree or GitHub’s Git UI.