CINXE.COM
tbaggery - A Note About Git Commit Messages
<!DOCTYPE html> <html> <head> <meta http-equiv="Content-type" content="text/html; charset=utf-8" /> <link rel="stylesheet" type="text/css" href="/stylesheets/screen.css" media="screen, projection" charset="utf-8" /> <link rel="stylesheet" type="text/css" href="/stylesheets/syntax.css" media="screen, projection" charset="utf-8" /> <link rel="stylesheet" type="text/css" href="/stylesheets/iphone.css" media="only screen and (max-device-width: 320px)" charset="utf-8" /> <link rel="shortcut icon" type="image/x-icon" href="/favicon.ico" /> <link href="http://feeds.feedburner.com/tpope" rel="alternate" title="tbaggery" type="application/atom+xml" /> <title> tbaggery - A Note About Git Commit Messages </title> </head> <body> <div id="header"> <h1><a href="/">tbaggery</a></h1> <p> <a href="http://tpo.pe/" title="Tim Pope" rel="author">tpope</a> blogs here, when he blogs. </p> </div> <div id="content"> <div class="post"> <h1>A Note About Git Commit Messages</h1> <p class="date">19 Apr 2008</p> <p>I want to take a moment to elaborate on what makes a well formed commit message. I think the best practices for commit message formatting is one of the little details that makes Git great. Understandably, some of the first commits to rails.git have messages of the really-long-line variety, and I want to expand on why this is a poor practice.</p> <p>Here’s a model Git commit message:</p> <div class="highlighter-rouge"><div class="highlight"><pre class="highlight"><code>Capitalized, short (50 chars or less) summary More detailed explanatory text, if necessary. Wrap it to about 72 characters or so. In some contexts, the first line is treated as the subject of an email and the rest of the text as the body. The blank line separating the summary from the body is critical (unless you omit the body entirely); tools like rebase can get confused if you run the two together. Write your commit message in the imperative: "Fix bug" and not "Fixed bug" or "Fixes bug." This convention matches up with commit messages generated by commands like git merge and git revert. Further paragraphs come after blank lines. - Bullet points are okay, too - Typically a hyphen or asterisk is used for the bullet, followed by a single space, with blank lines in between, but conventions vary here - Use a hanging indent </code></pre></div></div> <p>Let’s start with a few of the reasons why wrapping your commit messages to 72 columns is a good thing.</p> <ul> <li><code class="highlighter-rouge">git log</code> doesn’t do any special special wrapping of the commit messages. With the default pager of <code class="highlighter-rouge">less -S</code>, this means your paragraphs flow far off the edge of the screen, making them difficult to read. On an 80 column terminal, if we subtract 4 columns for the indent on the left and 4 more for symmetry on the right, we’re left with 72 columns.</li> <li><code class="highlighter-rouge">git format-patch --stdout</code> converts a series of commits to a series of emails, using the messages for the message body. Good email netiquette dictates we wrap our plain text emails such that there’s room for a few levels of nested reply indicators without overflow in an 80 column terminal. (The current rails.git workflow doesn’t include email, but who knows what the future will bring.)</li> </ul> <p>Vim users can meet this requirement by installing my <a href="http://github.com/tpope/vim-git">vim-git runtime files</a>, or by simply setting the following option in your git commit message file:</p> <div class="highlighter-rouge"><div class="highlight"><pre class="highlight"><code>:set textwidth=72 </code></pre></div></div> <p>For Textmate, you can adjust the “Wrap Column” option under the view menu, then use <code class="highlighter-rouge">^Q</code> to rewrap paragraphs (be sure there’s a blank line afterwards to avoid mixing in the comments). Here’s a shell command to add 72 to the menu so you don’t have to drag to select each time:</p> <div class="highlighter-rouge"><div class="highlight"><pre class="highlight"><code>$ defaults write com.macromates.textmate OakWrapColumns '( 40, 72, 78 )' </code></pre></div></div> <p>More important than the mechanics of formatting the body is the practice of having a subject line. As the example indicates, you should shoot for about 50 characters (though this isn’t a hard maximum) and always, always follow it with a blank line. This first line should be a concise summary of the changes introduced by the commit; if there are any technical details that cannot be expressed in these strict size constraints, put them in the body instead. The subject line is used all over Git, oftentimes in truncated form if too long of a message was used. The following are just a handful of examples of where it ends up:</p> <ul> <li><code class="highlighter-rouge">git log --pretty=oneline</code> shows a terse history mapping containing the commit id and the summary</li> <li><code class="highlighter-rouge">git rebase --interactive</code> provides the summary for each commit in the editor it invokes</li> <li>if the config option <code class="highlighter-rouge">merge.summary</code> is set, the summaries from all merged commits will make their way into the merge commit message</li> <li><code class="highlighter-rouge">git shortlog</code> uses summary lines in the changelog-like output it produces</li> <li><code class="highlighter-rouge">git format-patch</code>, <code class="highlighter-rouge">git send-email</code>, and related tools use it as the subject for emails</li> <li>reflogs, a local history accessible with <code class="highlighter-rouge">git reflog</code> intended to help you recover from stupid mistakes, get a copy of the summary</li> <li><code class="highlighter-rouge">gitk</code> has a column for the summary</li> <li>GitHub uses the summary in various places in their user interface</li> </ul> <p>The subject/body distinction may seem unimportant but it’s one of many subtle factors that makes Git history so much more pleasant to work with than Subversion.</p> </div> <div class='social'> <g:plusone href=""></g:plusone> <p class="comments_disabled"> Comments disabled. <a id="email" href="https://tpo.pe">Email me</a> instead. <script type="text/javascript"> var link = document.getElementById('email'); link.href = link.href.replace(/NOSPAM/,''); </script> </p> </div> </div> <div id="footer"> <address> © <a href="http://tpo.pe/">Tim Pope</a> </address> <a href="http://tpo.pe/feed" rel="alternate" type="application/atom+xml"> <img src="/images/feed.png" width="14" height="14" alt="Atom Feed" title="Atom Feed" /> </a> </div> <script type="text/javascript"> var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www."); document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E")); </script> <script type="text/javascript"> try { var pageTracker = _gat._getTracker("UA-13264116-1"); pageTracker._trackPageview(); } catch(err) {} </script> <script type="text/javascript"> (function() { var po = document.createElement('script'); po.type = 'text/javascript'; po.async = true; po.src = 'https://apis.google.com/js/plusone.js'; var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(po, s); })(); </script> </body> </html>