How to commit the right way in Git

Most people write commit messages explaining in short what is in their commit, which in the long run tells other people NOTHING about the commit without having to go through the diff of the commit. But there is a better way to do this. When committing and writing a message, like this:

git commit -m "Some very long commit message that ruins everything when exceeding 50 characters"

You’re exceeding 50 character limit Git has for commit messages. The rest goes to the commit message details.

You should always get the commit message dialog by writing git commit. Then write a 50 character (or less) description explaining what is in your commit. Then after 50 characters, you go into more details why you made this commit.

A little example:

commit1

Here I am just going into the commit message dialog.

commit2

Here I am in the commit message dialog.

Press the keyboard key i to get into INSERT mode of vim. Then you write some message, like so:

commit3

Notice how the word everybody is grayed out. That’s because it is beyond the 50 character limit. So everything after 50 characters becomes “details” in the commit message.

So, after writing a short message in the yellow field explaining the WHAT in your commit message, press enter twice to get into the details (gray) field, and write more details, explaining the WHY of your commit.

Something like this:

commit4

  • http://blog.kylemanna.com/ Kyle

    The format of the vim highlighting stems from the Linux kernel and is referred to as 50/72 formatting. More details @ http://stackoverflow.com/questions/2290016/git-commit-messages-50-72-formatting

    Generally the subject line is in a format of “subsystem: Subject line” so that people reviewing commits in large projects can quickly hone on stuff that matters to what they are working on.

    Figured I’d add this for others wondering. Coherent formatting of course is just a bonus, since most people (as you pointed out) write terrible commit log messages to begin with.