working-with-git.txt 10 KB

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