working-with-git.txt 11 KB

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