2012-06-08 00:48:29 +08:00
|
|
|
Working with Git and GitHub
|
|
|
|
===========================
|
|
|
|
|
|
|
|
Django uses `Git`_ for its source control. You can `download
|
|
|
|
<http://git-scm.com/download>`_ Git, but it's often easier to install with
|
|
|
|
your operating system's package manager.
|
|
|
|
|
|
|
|
Django's `Git repository`_ is hosted on `GitHub`_, and it is recommended
|
|
|
|
that you also work using GitHub.
|
|
|
|
|
|
|
|
After installing Git the first thing you should do is setup your name and
|
|
|
|
email::
|
|
|
|
|
2012-06-08 17:26:22 +08:00
|
|
|
$ git config --global user.name "Your Real Name"
|
|
|
|
$ git config --global user.email "you@email.com"
|
2012-06-08 00:48:29 +08:00
|
|
|
|
|
|
|
Note that ``user.name`` should be your real name, not your GitHub nick. GitHub
|
|
|
|
should know the email you use in the ``user.email`` field, as this will be
|
|
|
|
used to associate your commits with your GitHub account.
|
|
|
|
|
|
|
|
Now we are going to show how to create a GitHub pull request containing the
|
2012-06-08 17:26:22 +08:00
|
|
|
changes for Trac ticket #xxxxx. By creating a fully-ready pull request you
|
|
|
|
will make the committers' job easier, meaning that your work is more likely to
|
|
|
|
be merged into Django.
|
|
|
|
|
|
|
|
You could also upload a traditional patch to Trac, but it's less practical for
|
|
|
|
reviews.
|
2012-06-08 00:48:29 +08:00
|
|
|
|
|
|
|
.. _Git: http://git-scm.com/
|
|
|
|
.. _GitHub: https://github.com/
|
|
|
|
.. _Git repository: https://github.com/django/django/
|
|
|
|
|
|
|
|
Setting up local repository
|
|
|
|
---------------------------
|
|
|
|
|
2012-06-08 17:26:22 +08:00
|
|
|
When you have created your GitHub account, with the nick "github_nick", and
|
|
|
|
forked Django's repository, create a local copy of your fork::
|
2012-06-08 00:48:29 +08:00
|
|
|
|
|
|
|
git clone git@github.com:github_nick/django.git
|
|
|
|
|
2012-06-08 17:26:22 +08:00
|
|
|
This will create a new directory "django", containing a clone of your GitHub
|
|
|
|
repository.
|
|
|
|
|
|
|
|
Your GitHub repository will be called "origin" in Git.
|
|
|
|
|
|
|
|
You should also setup django/django as an "upstream" remote (that is, tell git
|
|
|
|
that the reference Django repository was the source of your fork of it)::
|
2012-06-08 00:48:29 +08:00
|
|
|
|
|
|
|
git remote add upstream git@github.com:django/django.git
|
|
|
|
git fetch upstream
|
|
|
|
|
|
|
|
You can add other remotes similarly, for example::
|
|
|
|
|
|
|
|
git remote add akaariai git@github.com:akaariai/django.git
|
|
|
|
|
|
|
|
Working on a ticket
|
|
|
|
-------------------
|
|
|
|
|
2012-06-08 17:26:22 +08:00
|
|
|
When working on a ticket create a new branch for the work, and base that work
|
|
|
|
on upstream/master::
|
2012-06-08 00:48:29 +08:00
|
|
|
|
|
|
|
git checkout -b ticket_xxxxx upstream/master
|
|
|
|
|
2012-06-08 17:26:22 +08:00
|
|
|
The -b flag creates a new branch for you locally. Don't hesitate to create new
|
|
|
|
branches even for the smallest things - that's what they are there for.
|
|
|
|
|
|
|
|
If instead you were working for a fix on the 1.4 branch, you would do::
|
2012-06-08 00:48:29 +08:00
|
|
|
|
|
|
|
git checkout -b ticket_xxxxx_1_4 upstream/stable/1.4.x
|
|
|
|
|
|
|
|
Assume the work is carried on ticket_xxxxx branch. Make some changes and
|
|
|
|
commit them::
|
|
|
|
|
|
|
|
git commit
|
|
|
|
|
2012-06-08 17:26:22 +08:00
|
|
|
When writing the commit message, follow the :ref:`commit message
|
2012-06-08 00:48:29 +08:00
|
|
|
guidelines <committing-guidlines>` to ease the work of the committer. If
|
|
|
|
you're uncomfortable with English, try at least to describe precisely what the
|
|
|
|
commit does.
|
|
|
|
|
|
|
|
If you need to do additional work on your branch, commit as often as
|
|
|
|
necessary::
|
|
|
|
|
|
|
|
git commit -m 'Added two more tests for edge cases'
|
|
|
|
|
|
|
|
Publishing work
|
|
|
|
~~~~~~~~~~~~~~~
|
|
|
|
|
2012-06-08 17:26:22 +08:00
|
|
|
You can publish your work on GitHub just by doing::
|
2012-06-08 00:48:29 +08:00
|
|
|
|
|
|
|
git push origin ticket_xxxxx
|
|
|
|
|
|
|
|
When you go to your GitHub page you will notice a new branch has been created.
|
2012-06-08 17:26:22 +08:00
|
|
|
|
2012-06-08 00:48:29 +08:00
|
|
|
If you are working on a Trac ticket, you should mention in the ticket that
|
|
|
|
your work is available from branch ticket_xxxxx of your github repo. Include a
|
|
|
|
link to your branch.
|
|
|
|
|
2012-06-08 17:26:22 +08:00
|
|
|
Note that the above branch is called a "topic branch" in Git parlance. You are
|
|
|
|
free to rewrite the history of this branch, by using ``git rebase`` for
|
|
|
|
example. Other people shouldn't base their work on such a branch, because
|
|
|
|
their clone would become corrupt when you edit commits.
|
|
|
|
|
|
|
|
There are also "public branches". These are branches other people are supposed
|
|
|
|
to fork, so the history of these branches should never change. Good examples
|
|
|
|
of public branches are the ``master`` and ``stable/A.B.x`` branches in the
|
|
|
|
django/django repository.
|
2012-06-08 00:48:29 +08:00
|
|
|
|
|
|
|
When you think your work is ready to be pulled into Django, you should create
|
2012-06-08 17:26:22 +08:00
|
|
|
a pull request at GitHub. A good pull request means:
|
2012-06-08 00:48:29 +08:00
|
|
|
|
2012-06-08 17:26:22 +08:00
|
|
|
* commits with one logical change in each, following the
|
|
|
|
:doc:`coding style <coding-style>`,
|
2012-06-08 00:48:29 +08:00
|
|
|
|
2012-06-08 17:26:22 +08:00
|
|
|
* well-formed messages for each commit: a summary line and then paragraphs
|
|
|
|
wrapped at 72 characters thereafter -- see the :ref:`committing guidelines
|
|
|
|
<committing-guidlines>` for more details,
|
2012-06-08 00:48:29 +08:00
|
|
|
|
2012-06-08 17:26:22 +08:00
|
|
|
* documentation and tests, if needed -- actually tests are always needed,
|
|
|
|
except for documentation changes.
|
2012-06-08 00:48:29 +08:00
|
|
|
|
2012-06-08 17:26:22 +08:00
|
|
|
The test suite must pass and the documentation must build without warnings.
|
2012-06-08 00:48:29 +08:00
|
|
|
|
|
|
|
Once you have created your pull request, you should add a comment in the
|
2012-06-08 17:26:22 +08:00
|
|
|
related Trac ticket explaining what you've done. In particular you should note
|
|
|
|
the environment in which you ran the tests, for instance: "all tests pass
|
|
|
|
under SQLite and MySQL".
|
2012-06-08 00:48:29 +08:00
|
|
|
|
2012-06-08 17:26:22 +08:00
|
|
|
Pull requests at GitHub have only two states: open and closed. The committer
|
|
|
|
who will deal with your pull request has only two options: merge it or close
|
|
|
|
it. For this reason, it isn't useful to make a pull request until the code is
|
|
|
|
ready for merging -- or sufficiently close that a committer will finish it
|
|
|
|
himself.
|
2012-06-08 00:48:29 +08:00
|
|
|
|
|
|
|
Rebasing branches
|
|
|
|
~~~~~~~~~~~~~~~~~
|
|
|
|
|
|
|
|
In the example above you created two commits, the "Fixed ticket_xxxxx" commit
|
2012-06-08 17:26:22 +08:00
|
|
|
and "Added two more tests" commit.
|
|
|
|
|
|
|
|
We do not want to have the entire history of your working process in your
|
|
|
|
repository. Your commit "Added two more tests" would be unhelpful noise.
|
|
|
|
Instead, we would rather only have one commit containing all your work.
|
|
|
|
|
|
|
|
To rework the history of your branch you can squash the commits into one by
|
|
|
|
using interactive rebase::
|
2012-06-08 00:48:29 +08:00
|
|
|
|
|
|
|
git rebase -i HEAD~2
|
|
|
|
|
|
|
|
The HEAD~2 above is shorthand for two latest commits. The above command
|
|
|
|
will open an editor showing the two commits, prefixed with the word "pick".
|
2012-06-08 17:26:22 +08:00
|
|
|
|
|
|
|
Change the second line to "squash" instead. This will keep the
|
|
|
|
first commit, and squash the second commit into the first one. Save and quit
|
|
|
|
the editor. A second editor window should open, so you can reword the
|
|
|
|
commit message for the commit now that it includes both your steps.
|
2012-06-08 00:48:29 +08:00
|
|
|
|
|
|
|
You can also use the "edit" option in rebase. This way you can change a single
|
2012-06-08 17:26:22 +08:00
|
|
|
commit, for example to fix a typo in a docstring::
|
2012-06-08 00:48:29 +08:00
|
|
|
|
|
|
|
git rebase -i HEAD~3
|
|
|
|
# Choose edit, pick, pick for the commits
|
|
|
|
# Now you are able to rework the commit (use git add normally to add changes)
|
|
|
|
# When finished, commit work with "--amend" and continue
|
|
|
|
git commit --amend
|
|
|
|
# reword the commit message if needed
|
|
|
|
git rebase --continue
|
2012-06-08 17:26:22 +08:00
|
|
|
# The second and third commits should be applied.
|
2012-06-08 00:48:29 +08:00
|
|
|
|
2012-06-08 17:26:22 +08:00
|
|
|
If your topic branch is already published at GitHub, for example if you're
|
|
|
|
making minor changes to take into account a review, you will need to force-
|
|
|
|
push the changes::
|
2012-06-08 00:48:29 +08:00
|
|
|
|
|
|
|
git push -f origin ticket_xxxxx
|
|
|
|
|
|
|
|
Note that this will rewrite history of ticket_xxxxx - if you check the commit
|
|
|
|
hashes before and after the operation at GitHub you will notice that the
|
2012-06-08 17:26:22 +08:00
|
|
|
commit hashes do not match any more. This is acceptable, as the branch is merely
|
|
|
|
a topic branch, and nobody should be basing their work on it.
|
2012-06-08 00:48:29 +08:00
|
|
|
|
|
|
|
After upstream has changed
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
|
|
|
|
When upstream (django/django) has changed, you should rebase your work. To
|
|
|
|
do this, use::
|
|
|
|
|
|
|
|
git fetch upstream
|
|
|
|
git rebase
|
|
|
|
|
|
|
|
The work is automatically rebased using the branch you forked on, in the
|
|
|
|
example case using upstream/master.
|
|
|
|
|
|
|
|
The rebase command removes all your local commits temporarily, applies the
|
2012-06-08 17:26:22 +08:00
|
|
|
upstream commits, and then applies your local commits again on the work.
|
|
|
|
|
|
|
|
If there are merge conflicts you will need to resolve them and then use ``git
|
2012-06-08 00:48:29 +08:00
|
|
|
rebase --continue``. At any point you can use ``git rebase --abort`` to return
|
|
|
|
to the original state.
|
|
|
|
|
2012-06-08 17:26:22 +08:00
|
|
|
Note that you want to *rebase* on upstream, not *merge* the upstream.
|
|
|
|
|
|
|
|
The reason for this is that by rebasing, your commits will always be *on
|
|
|
|
top of* the upstream's work, not *mixed in with* the changes in the upstream.
|
|
|
|
This way your branch will contain only commits related to its topic, which
|
|
|
|
makes squashing easier.
|
2012-06-08 00:48:29 +08:00
|
|
|
|
|
|
|
After review
|
|
|
|
------------
|
|
|
|
|
|
|
|
It is unusual to get any non-trivial amount of code into core without changes
|
|
|
|
requested by reviewers. In this case, it is often a good idea to add the
|
|
|
|
changes as one incremental commit to your work. This allows the reviewer to
|
2012-06-08 17:26:22 +08:00
|
|
|
easily check what changes you have done.
|
2012-06-08 00:48:29 +08:00
|
|
|
|
2012-06-08 17:26:22 +08:00
|
|
|
In this case, do the changes required by the reviewer. Commit as often as
|
|
|
|
necessary. Before publishing the changes, rebase your work. If you added two
|
|
|
|
commits, you would run::
|
2012-06-08 00:48:29 +08:00
|
|
|
|
2012-06-08 17:26:22 +08:00
|
|
|
git rebase -i HEAD~2
|
2012-06-08 00:48:29 +08:00
|
|
|
|
2012-06-08 17:26:22 +08:00
|
|
|
Squash the second commit into the first. Write a commit message along the lines of::
|
|
|
|
|
|
|
|
Made changes asked in review by <reviewer>
|
|
|
|
|
|
|
|
- Fixed whitespace errors in foobar
|
|
|
|
- Reworded the docstring of bar()
|
|
|
|
|
|
|
|
Finally push your work back to your GitHub repository. Since you didn't touch
|
|
|
|
the public commits during the rebase, you should not need to force-push::
|
|
|
|
|
|
|
|
git push origin ticket_xxxxx
|
|
|
|
|
|
|
|
Your pull request should now contain the new commit too.
|
2012-06-08 00:48:29 +08:00
|
|
|
|
2012-06-08 17:26:22 +08:00
|
|
|
Note that the committer is likely to squash the review commit into the previous commit
|
2012-06-08 00:48:29 +08:00
|
|
|
when committing the code.
|
|
|
|
|
|
|
|
Summary
|
|
|
|
-------
|
|
|
|
|
2012-06-08 17:26:22 +08:00
|
|
|
* Work on GitHub if you can.
|
2012-06-08 00:48:29 +08:00
|
|
|
* Announce your work on the Trac ticket by linking to your GitHub branch.
|
|
|
|
* When you have something ready, make a pull request.
|
|
|
|
* Make your pull requests as good as you can.
|
|
|
|
* When doing fixes to your work, use ``git rebase -i`` to squash the commits.
|
|
|
|
* When upstream has changed, do ``git fetch upstream; git rebase``.
|