working-with-git.txt 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. Working with Git and GitHub
  2. ===========================
  3. Django uses `Git`_ for its source control. You can `download
  4. <http://git-scm.com/download>`_ Git, but it's often easier to install with
  5. your operating system's package manager.
  6. Django's `Git repository`_ is hosted on `GitHub`_, and it is recommended
  7. that you also work using GitHub.
  8. After installing Git the first thing you should do is setup your name and
  9. email::
  10. $ git config --global user.name "Your Real Name"
  11. $ git config --global user.email "you@email.com"
  12. Note that ``user.name`` should be your real name, not your GitHub nick. GitHub
  13. should know the email you use in the ``user.email`` field, as this will be
  14. used to associate your commits with your GitHub account.
  15. Now we are going to show how to create a GitHub pull request containing the
  16. changes for Trac ticket #xxxxx. By creating a fully-ready pull request you
  17. will make the committers' job easier, meaning that your work is more likely to
  18. be merged into Django.
  19. You could also upload a traditional patch to Trac, but it's less practical for
  20. reviews.
  21. .. _Git: http://git-scm.com/
  22. .. _GitHub: https://github.com/
  23. .. _Git repository: https://github.com/django/django/
  24. Setting up local repository
  25. ---------------------------
  26. When you have created your GitHub account, with the nick "github_nick", and
  27. forked Django's repository, create a local copy of your fork::
  28. git clone git@github.com:github_nick/django.git
  29. This will create a new directory "django", containing a clone of your GitHub
  30. repository.
  31. Your GitHub repository will be called "origin" in Git.
  32. You should also setup django/django as an "upstream" remote (that is, tell git
  33. that the reference Django repository was the source of your fork of it)::
  34. git remote add upstream git@github.com:django/django.git
  35. git fetch upstream
  36. You can add other remotes similarly, for example::
  37. git remote add akaariai git@github.com:akaariai/django.git
  38. Working on a ticket
  39. -------------------
  40. When working on a ticket create a new branch for the work, and base that work
  41. on upstream/master::
  42. git checkout -b ticket_xxxxx upstream/master
  43. The -b flag creates a new branch for you locally. Don't hesitate to create new
  44. branches even for the smallest things - that's what they are there for.
  45. If instead you were working for a fix on the 1.4 branch, you would do::
  46. git checkout -b ticket_xxxxx_1_4 upstream/stable/1.4.x
  47. Assume the work is carried on ticket_xxxxx branch. Make some changes and
  48. commit them::
  49. git commit
  50. When writing the commit message, follow the :ref:`commit message
  51. guidelines <committing-guidlines>` to ease the work of the committer. If
  52. you're uncomfortable with English, try at least to describe precisely what the
  53. commit does.
  54. If you need to do additional work on your branch, commit as often as
  55. necessary::
  56. git commit -m 'Added two more tests for edge cases'
  57. Publishing work
  58. ~~~~~~~~~~~~~~~
  59. You can publish your work on GitHub just by doing::
  60. git push origin ticket_xxxxx
  61. When you go to your GitHub page you will notice a new branch has been created.
  62. If you are working on a Trac ticket, you should mention in the ticket that
  63. your work is available from branch ticket_xxxxx of your github repo. Include a
  64. link to your branch.
  65. Note that the above branch is called a "topic branch" in Git parlance. You are
  66. free to rewrite the history of this branch, by using ``git rebase`` for
  67. example. Other people shouldn't base their work on such a branch, because
  68. their clone would become corrupt when you edit commits.
  69. There are also "public branches". These are branches other people are supposed
  70. to fork, so the history of these branches should never change. Good examples
  71. of public branches are the ``master`` and ``stable/A.B.x`` branches in the
  72. django/django repository.
  73. When you think your work is ready to be pulled into Django, you should create
  74. a pull request at GitHub. A good pull request means:
  75. * commits with one logical change in each, following the
  76. :doc:`coding style <coding-style>`,
  77. * well-formed messages for each commit: a summary line and then paragraphs
  78. wrapped at 72 characters thereafter -- see the :ref:`committing guidelines
  79. <committing-guidlines>` for more details,
  80. * documentation and tests, if needed -- actually tests are always needed,
  81. except for documentation changes.
  82. The test suite must pass and the documentation must build without warnings.
  83. Once you have created your pull request, you should add a comment in the
  84. related Trac ticket explaining what you've done. In particular you should note
  85. the environment in which you ran the tests, for instance: "all tests pass
  86. under SQLite and MySQL".
  87. Pull requests at GitHub have only two states: open and closed. The committer
  88. who will deal with your pull request has only two options: merge it or close
  89. it. For this reason, it isn't useful to make a pull request until the code is
  90. ready for merging -- or sufficiently close that a committer will finish it
  91. himself.
  92. Rebasing branches
  93. ~~~~~~~~~~~~~~~~~
  94. In the example above you created two commits, the "Fixed ticket_xxxxx" commit
  95. and "Added two more tests" commit.
  96. We do not want to have the entire history of your working process in your
  97. repository. Your commit "Added two more tests" would be unhelpful noise.
  98. Instead, we would rather only have one commit containing all your work.
  99. To rework the history of your branch you can squash the commits into one by
  100. using interactive rebase::
  101. git rebase -i HEAD~2
  102. The HEAD~2 above is shorthand for two latest commits. The above command
  103. will open an editor showing the two commits, prefixed with the word "pick".
  104. Change the second line to "squash" instead. This will keep the
  105. first commit, and squash the second commit into the first one. Save and quit
  106. the editor. A second editor window should open, so you can reword the
  107. commit message for the commit now that it includes both your steps.
  108. You can also use the "edit" option in rebase. This way you can change a single
  109. commit, for example to fix a typo in a docstring::
  110. git rebase -i HEAD~3
  111. # Choose edit, pick, pick for the commits
  112. # Now you are able to rework the commit (use git add normally to add changes)
  113. # When finished, commit work with "--amend" and continue
  114. git commit --amend
  115. # reword the commit message if needed
  116. git rebase --continue
  117. # The second and third commits should be applied.
  118. If your topic branch is already published at GitHub, for example if you're
  119. making minor changes to take into account a review, you will need to force-
  120. push the changes::
  121. git push -f origin ticket_xxxxx
  122. Note that this will rewrite history of ticket_xxxxx - if you check the commit
  123. hashes before and after the operation at GitHub you will notice that the
  124. commit hashes do not match any more. This is acceptable, as the branch is merely
  125. a topic branch, and nobody should be basing their work on it.
  126. After upstream has changed
  127. ~~~~~~~~~~~~~~~~~~~~~~~~~~
  128. When upstream (django/django) has changed, you should rebase your work. To
  129. do this, use::
  130. git fetch upstream
  131. git rebase
  132. The work is automatically rebased using the branch you forked on, in the
  133. example case using upstream/master.
  134. The rebase command removes all your local commits temporarily, applies the
  135. upstream commits, and then applies your local commits again on the work.
  136. If there are merge conflicts you will need to resolve them and then use ``git
  137. rebase --continue``. At any point you can use ``git rebase --abort`` to return
  138. to the original state.
  139. Note that you want to *rebase* on upstream, not *merge* the upstream.
  140. The reason for this is that by rebasing, your commits will always be *on
  141. top of* the upstream's work, not *mixed in with* the changes in the upstream.
  142. This way your branch will contain only commits related to its topic, which
  143. makes squashing easier.
  144. After review
  145. ------------
  146. It is unusual to get any non-trivial amount of code into core without changes
  147. requested by reviewers. In this case, it is often a good idea to add the
  148. changes as one incremental commit to your work. This allows the reviewer to
  149. easily check what changes you have done.
  150. In this case, do the changes required by the reviewer. Commit as often as
  151. necessary. Before publishing the changes, rebase your work. If you added two
  152. commits, you would run::
  153. git rebase -i HEAD~2
  154. Squash the second commit into the first. Write a commit message along the lines of::
  155. Made changes asked in review by <reviewer>
  156. - Fixed whitespace errors in foobar
  157. - Reworded the docstring of bar()
  158. Finally push your work back to your GitHub repository. Since you didn't touch
  159. the public commits during the rebase, you should not need to force-push::
  160. git push origin ticket_xxxxx
  161. Your pull request should now contain the new commit too.
  162. Note that the committer is likely to squash the review commit into the previous commit
  163. when committing the code.
  164. Summary
  165. -------
  166. * Work on GitHub if you can.
  167. * Announce your work on the Trac ticket by linking to your GitHub branch.
  168. * When you have something ready, make a pull request.
  169. * Make your pull requests as good as you can.
  170. * When doing fixes to your work, use ``git rebase -i`` to squash the commits.
  171. * When upstream has changed, do ``git fetch upstream; git rebase``.