Movatterモバイル変換


[0]ホーム

URL:


Skip to main content
Django

The web framework for perfectionists with deadlines.

Documentation

  • Language:en

Working with Git and GitHub

This section explains how the community can contribute code to Django via pullrequests. If you’re interested in howmergers handlethem, seeCommitting code.

Below, we are going to show how to create a GitHub pull request containing thechanges for Trac ticket #xxxxx. By creating a fully-ready pull request, youwill make the reviewer’s job easier, meaning that your work is more likely tobe merged into Django.

You could also upload a traditional patch to Trac, but it’s less practical forreviews.

Installing Git

Django usesGit for its source control. You candownload Git, but it’s often easier to install withyour operating system’s package manager.

Django’sGit repository is hosted onGitHub, and it is recommendedthat you also work using GitHub.

After installing Git, the first thing you should do is set up your name andemail:

$gitconfig--globaluser.name"Your Real Name"$gitconfig--globaluser.email"you@email.com"

Note thatuser.name should be your real name, not your GitHub nick. GitHubshould know the email you use in theuser.email field, as this will beused to associate your commits with your GitHub account.

Setting up local repository

When you have created your GitHub account, with the nick “GitHub_nick”, andforked Django’s repository,create a local copy of your fork:

gitclonehttps://github.com/GitHub_nick/django.git

This will create a new directory “django”, containing a clone of your GitHubrepository. The rest of the git commands on this page need to be run within thecloned directory, so switch to it now:

cddjango

Your GitHub repository will be called “origin” in Git.

You should also set updjango/django as an “upstream” remote (that is, tellgit that the reference Django repository was the source of your fork of it):

gitremoteaddupstreamhttps://github.com/django/django.gitgitfetchupstream

You can add other remotes similarly, for example:

gitremoteaddakaariaihttps://github.com/akaariai/django.git

Working on a ticket

When working on a ticket, create a new branch for the work, and base that workonupstream/main:

gitcheckout-bticket_xxxxxupstream/main

The -b flag creates a new branch for you locally. Don’t hesitate to create newbranches 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:

gitcheckout-bticket_xxxxx_1_4upstream/stable/1.4.x

Assume the work is carried on the ticket_xxxxx branch. Make some changes andcommit them:

gitcommit

When writing the commit message, follow thecommit messageguidelines to ease the work of the merger. If you’reuncomfortable with English, try at least to describe precisely what the commitdoes.

If you need to do additional work on your branch, commit as often asnecessary:

gitcommit-m'Added two more tests for edge cases'

Publishing work

You can publish your work on GitHub by running:

gitpushoriginticket_xxxxx

When you go to your GitHub page, you will notice a new branch has been created.

If you are working on a Trac ticket, you should mention in the ticket thatyour work is available from branch ticket_xxxxx of your GitHub repo. Include alink to your branch.

Note that the above branch is called a “topic branch” in Git parlance. You arefree to rewrite the history of this branch, by usinggitrebase forexample. Other people shouldn’t base their work on such a branch, becausetheir clone would become corrupt when you edit commits.

There are also “public branches”. These are branches other people are supposedto fork, so the history of these branches should never change. Good examplesof public branches are themain andstable/A.B.x branches in thedjango/django repository.

When you think your work is ready to be pulled into Django, you should createa pull request at GitHub. A good pull request means:

  • commits with one logical change in each, following thecoding style,

  • well-formed messages for each commit: a summary line and then paragraphswrapped at 72 characters thereafter – see thecommitting guidelines for more details,

  • documentation and tests, if needed – actually tests are always needed,except for documentation changes.

The test suite must pass and the documentation must build without warnings.

Once you have created your pull request, you should add a comment in therelated Trac ticket explaining what you’ve done. In particular, you should notethe environment in which you ran the tests, for instance: “all tests passunder SQLite and MySQL”.

Pull requests at GitHub have only two states: open and closed. The merger whowill 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 readyfor merging – or sufficiently close that a merger will finish it themselves.

Rebasing branches

In the example above, you created two commits, the “Fixed ticket_xxxxx” commitand “Added two more tests” commit.

We do not want to have the entire history of your working process in yourrepository. 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 byusing interactive rebase:

gitrebase-iHEAD~2

The HEAD~2 above is shorthand for two latest commits. The above commandwill open an editor showing the two commits, prefixed with the word “pick”.

Change “pick” on the second line to “squash” instead. This will keep thefirst commit, and squash the second commit into the first one. Save and quitthe editor. A second editor window should open, so you can reword thecommit message for the commit now that it includes both your steps.

You can also use the “edit” option in rebase. This way you can change a singlecommit, for example to fix a typo in a docstring:

gitrebase-iHEAD~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 continuegitcommit--amend# Reword the commit message if neededgitrebase--continue# The second and third commits should be applied.

If your topic branch is already published at GitHub, for example if you’remaking minor changes to take into account a review, you will need to force-pushthe changes:

gitpush-foriginticket_xxxxx

Note that this will rewrite history of ticket_xxxxx - if you check the commithashes before and after the operation at GitHub you will notice that the commithashes do not match anymore. This is acceptable, as the branch is a topicbranch, and nobody should be basing their work on it.

After upstream has changed

When upstream (django/django) has changed, you should rebase your work. Todo this, use:

gitfetchupstreamgitrebaseupstream/main

The work is automatically rebased using the branch you forked on, in theexample case usingupstream/main.

The rebase command removes all your local commits temporarily, applies theupstream commits, and then applies your local commits again on the work.

If there are merge conflicts, you will need to resolve them and then usegitrebase--continue. At any point you can usegitrebase--abort to returnto the original state.

Note that you want torebase on upstream, notmerge the upstream.

The reason for this is that by rebasing, your commits will always beontop of the upstream’s work, notmixed in with the changes in the upstream.This way your branch will contain only commits related to its topic, whichmakes squashing easier.

After review

It is unusual to get any non-trivial amount of code into core without changesrequested by reviewers. In this case, it is often a good idea to add thechanges as one incremental commit to your work. This allows the reviewer toeasily check what changes you have done.

In this case, do the changes required by the reviewer. Commit as often asnecessary. Before publishing the changes, rebase your work. If you added twocommits, you would run:

gitrebase-iHEAD~2

Squash the second commit into the first. Write a commit message along the linesof:

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 touchthe public commits during the rebase, you should not need to force-push:

gitpushoriginticket_xxxxx

Your pull request should now contain the new commit too.

Note that the merger is likely to squash the review commit into the previouscommit when committing the code.

Working on a patch

One of the ways that developers can contribute to Django is by reviewingpatches. Those patches will typically exist as pull requests on GitHub andcan be easily integrated into your local repository:

gitcheckout-bpull_xxxxxupstream/maincurl-Lhttps://github.com/django/django/pull/xxxxx.patch|gitam

This will create a new branch and then apply the changes from the pull requestto it. At this point you can run the tests or do anything else you need todo to investigate the quality of the patch.

For more detail on working with pull requests see theguidelines for mergers.

Summary

  • Work on GitHub if you can.

  • 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, usegitrebase-i to squash the commits.

  • When upstream has changed, dogitfetchupstream;gitrebase.

Back to Top

Additional Information

Support Django!

Support Django!

Contents

Getting help

FAQ
Try the FAQ — it's got answers to many common questions.
Index,Module Index, orTable of Contents
Handy when looking for specific information.
Django Discord Server
Join the Django Discord Community.
Official Django Forum
Join the community on the Django Forum.
Ticket tracker
Report bugs with Django or Django documentation in our ticket tracker.

Download:

Offline (development version):HTML |PDF |ePub
Provided byRead the Docs.


[8]ページ先頭

©2009-2025 Movatter.jp