A place to cache linked articles (think custom and personal wayback machine)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

4 年之前
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. title: Telling stories with your Git history
  2. url: https://about.futurelearn.com/blog/telling-stories-with-your-git-history
  3. hash_url: 3298d68a0ad3d267742d80811213d628
  4. <p><b>Seb Jacobs, a Developer at FutureLearn, discusses how we maintain a </b><b>useful Git history and outlines five principles we live by when making code commits.</b></p>
  5. <p>Development started on the FutureLearn platform on 2 April 2013 and since that time, our codebase has gone through rapid changes, covering over 10,000 commits.</p>
  6. <p>With a rapidly changing codebase and a growing development team, being able to communicate how and why your code evolves over time is crucial.</p>
  7. <p>In order to manage the complex process of changing coding, we not only believe in having well factored code and tests, but also a simple Git history, which allows us to make informed decisions about future code changes.</p>
  8. <h2>Telling stories with your Git history</h2>
  9. <p><a href="https://github.com/Futurelearn">Our Git history</a> is a living, ever-changing, searchable record that tells the story of how and why our code is the way it is. The ability to document code effectively using Git (or another version control system) is just as important as being able to ship a feature, write clean code or readable tests.</p>
  10. <p>Although your code should be self-documenting, it doesn&#8217;t tell the story of why the code is the way it is or how it came to be.</p>
  11. <p>You may be used to more conventional methods of code documentation such as a Readme or a Wiki, however these forms of documentation can often become out of date.</p>
  12. <p>The other method of code documentation that comes to mind is code comments. These can be noisy and are not always relevant. I believe you should apply the same rules you apply to code design, in terms of separating your concerns, using Git for the documentation and the code for the code. These comments also have a tendency of becoming out of date.</p>
  13. <p>Here are five principles we live by at FutureLearn when it comes to Git:</p>
  14. <h2><b>1. Atomic commits</b></h2>
  15. <p>Large commits can be difficult to read, especially when they contain changes which are unrelated. I&#8217;ll admit that it is often difficult to think about how you might split up a large commit, but I find it helps to think about the purpose of each commit.</p>
  16. <p>Think of atomic commits as the smallest amount of code changed which delivers value &#8211; whether it’s tidying up existing code or introducing a new (small) feature.</p>
  17. <pre>commit: [REDACTED]
  18. Date: [REDACTED]
  19. Allow educators to invite users onto courses.
  20. 61 files changed, 937 insertions(+), 81 deletions(-)</pre>
  21. <p>With this example there are a lot of changes in this one commit. On the surface it might look like all these changes are related. However, if you were to break down the changes, you would have a better sense of what&#8217;s going on, for example:</p>
  22. <pre>a6455f8 Record when enrolment is created via an invitation.
  23. b529f6d Allow invited users to enrol on courses.
  24. b5bb6e4 Allow invited users to see the course description page.
  25. c829cbc Send enrolment invitation emails in batches of 1000.
  26. 5feaccf Allow educators to invite users onto courses.</pre>
  27. <p>Now that this example has been split up, it starts to become more useful; not only is each commit smaller, it also gives you a better story of what has happened.</p>
  28. <h2><b>2. Useful commit messages</b></h2>
  29. <p>Writing commit messages can be difficult, but it helps to have a purpose in mind. We’re already breaking the changes down into atomic commits, meaning we should have a good idea of the value of each commit.</p>
  30. <p>If there&#8217;s one thing to remember, it is to explain why you&#8217;ve made the change in the first place. This is the perfect opportunity to reflect on what you&#8217;re doing and to provide context &#8211; whether it&#8217;s to satisfy a user requirement, to fix a bug or to make another change easier to make in the future.</p>
  31. <p>I find it helps to look at the commit from the perspective of another developer. What questions might they be asking when looking at your code changes? What might not be immediately obvious?</p>
  32. <p>In terms of good practice, the following template is a good start. However, bear in mind that every commit is different.</p>
  33. <pre>Short one line title.
  34. An explanation of the problem, providing context (this may be as simple
  35. as a reference to the user story).
  36. Longer description of what the change does.
  37. An explanation of why the change is being made.
  38. Perhaps a discussion of alternatives that were considered.</pre>
  39. <p>The first line should be used to explain the value of the changes, rather than focussing on the implementation details. By keeping this concise, we allow the reader to easily scan over the commit and find the code changes they are interested in.</p>
  40. <p>The rest of the Git commit message depends on the change, but it’s always useful to explain what you’ve changed and why you’ve changed it.</p>
  41. <p>I find it also helps in some cases, to provide further context such as explaining alternative solutions you&#8217;ve ruled out or providing external references.</p>
  42. <pre>Correct the colour of FAQ link in course notice footer
  43. PT: https://www.pivotaltracker.com/story/show/84753832
  44. In some email clients the colour of the FAQ link in the course notice
  45. footer was being displayed as blue instead of white. The examples given
  46. in PT are all different versions of Outlook. Outlook won't implement
  47. CSS changes that include `!important` inline [1].
  48. Therefore, since we were using it to define the colour of that link,
  49. Outlook wasn't applying that style and thus simply set its default
  50. style(blue, like in most browsers).
  51. Removing that `!important` should fix the problem.
  52. [1] https://www.campaignmonitor.com/blog/post/3143/outlook-2007-and-
  53. the-inline-important-declaration/</pre>
  54. <p>This example has a clear headline, it outlines the problem, the developer’s intent and also provides context around the change.</p>
  55. <h2><b>3. Revise history before sharing</b></h2>
  56. <p>When developing your code, you are bound to change direction or even make mistakes (most commonly introducing typos or bugs).</p>
  57. <pre>324d079 Fix typo in enrolment flash message
  58. 3a85f77 Only display enrol button for users who can enrol
  59. 4cc4778 Allow users to enrol on courses</pre>
  60. <p>In this example, the developer has introduced a typo in their first commit, which they have fixed in a later commit.</p>
  61. <p>Before you share your commit history, it&#8217;s important to think about what is useful information for someone else to read. You shouldn’t think of your Git history as a &#8220;truthful&#8221; log of what you worked on step-by-step. Just as we refactor code, we should refactor our commits before sharing them with others.</p>
  62. <p>The power of Git makes it simple to re-order, reword and refactor your commits until they tell the clearest story possible.</p>
  63. <p>Git’s interactive rebasing functionality allows us to tell a clearer story:</p>
  64. <pre>$ git rebase --interactive
  65. 3a85f77 Only display enrol button for users who can enrol
  66. 773e345 Allow users to enrol on courses</pre>
  67. <p>By reducing the amount of noise in your commit history, you can save you and your team time.</p>
  68. <p>If you&#8217;re struggling with this process, I find that using Pull Requests is a great way to collaborate on shaping the commit history of your feature branch.</p>
  69. <h2><b>4. Single purpose branches</b></h2>
  70. <p>Long-living feature branches can often be difficult to keep up to date with master. I find that it is important to think about the purpose and scope of your feature branch.</p>
  71. <p>Although you might be working on a single feature (user story), this doesn&#8217;t necessarily mean that you can&#8217;t deliver value in stages.</p>
  72. <pre>5ce95fb Notify educators when an invitation has been accepted.
  73. 5ce95fb Refactor specs around enrolment invitations.
  74. ee95245 Extend enrolment invitation to educators.
  75. cfb2fb4 Tidy up whitespace in enrolment invitations spec.</pre>
  76. <p>Like with this example, you may find that you are having to change/refactor key areas of the codebase in order to implement the feature you are developing. These changes can often provide a clear benefit that isn’t directly related to the feature branch, and can be landed on master separately (and earlier).</p>
  77. <pre>$ git cherry-pick cfb2fb4 5ce95fb
  78. * 0564508 Merge branch 'educator-enrolment-invitations'
  79. |\
  80. | _ 5ce95fb Notify educators when an invitation has been accepted.
  81. | _ ee95245 Extend enrolment invitation to educators.
  82. | |
  83. |/
  84. * 5ce95fb Refactor specs around enrolment invitations.
  85. * cfb2fb4 Tidy up whitespace in enrolment invitations spec.</pre>
  86. <p>By splitting up your feature branches, you not only reduce the pain of merging each branch, you also deliver value sooner and make your Git history more readable.</p>
  87. <h2><b>5. Keep your history linear</b></h2>
  88. <p>Often, merging changes into master can result in your history becoming tangled and difficult to read.</p>
  89. <pre>* ce91a05 Merge branch 'reprint-statements'
  90. |\
  91. | * ae43ad0 Disable reprint link for refunded purchases.
  92. | * 0b1abb0 Allow admins to flag purchases for re-printing.
  93. * | 35d0357 Put dates formats in the pattern library
  94. * | 275206c Merge branch 'fulfilment-attempt'
  95. |\ \
  96. | _ | 7aae45b Populate `fulfilled?` for existing purchases.
  97. | _ | 8e461b1 Display purchase fulfilment attempts to admins.
  98. * | | 1adc0a9 Reduce padding around the course run date
  99. | |/
  100. |/|</pre>
  101. <p>This becomes even more of an issue when you have several feature branches being developed in parallel.</p>
  102. <pre>* ce91a05 Merge branch 'reprint-statements'
  103. |\
  104. | * ae43ad0 Disable reprint link for refunded purchases.
  105. | * 0b1abb0 Allow admins to flag purchases for re-printing.
  106. |/
  107. * 35d0357 Put dates formats in the pattern library
  108. * 275206c Merge branch 'fulfilment-attempt'
  109. |\
  110. | _ 7aae45b Populate `fulfilled?` for existing purchases.
  111. | _ 8e461b1 Display purchase fulfilment attempts to admins.
  112. | \* 44cbfd0 Introduce Fulfilment attempts
  113. |/
  114. * 1adc0a9 Reduce padding around the course run date</pre>
  115. <p>When it comes to merging, we try to preserve our merges commits and to rebase our feature branches before merging.</p>
  116. <pre>$ git checkout reprint-statements
  117. $ git rebase master
  118. $ git checkout master
  119. $ git merge --no-ff reprint-statements</pre>
  120. <p>This allows us to group related commits together while keeping our merges clean, making it a lot easier to identify when a particular change was introduced.</p>
  121. <p>We also find it often helps to commit smaller changes directly onto master.</p>
  122. <h2><b>Summary</b></h2>
  123. <p>If you spend as much time ensuring your commits are well factored as you do refactoring your code and tests, it will save you and your team time and pain in the future.</p>
  124. <p><strong>How do you use Git? What sort of problems do you think it solves? Let us know in the comments below.</strong></p>
  125. <p><strong>Want to know more about how we use Git? <a href="https://skillsmatter.com/skillscasts/6128-telling-stories-through-your-commits">Watch a talk from our CTO</a>, Joel Chippindale.</strong></p>
  126. <p><strong>You might also find the following links useful: <a href="http://pcottle.github.io/learnGitBranching">LearnGitBranching</a>; <a href="http://tbaggery.com/2008/04/19/a-note-about-git-commit-messages.html">A Note About Git Commit Messages</a>; <a href="http://think-like-a-git.net/">Think Like a Git</a>; and <a href="https://github.com/gazler/githug">GitHug</a>.</strong></p>