Committing codeÂś
This section is addressed to the mergers and to anyone interested in knowing how code gets committed into Django. If youâre a community member who wants to contribute code to Django, look at Working with Git and GitHub instead.
Handling pull requestsÂś
Since Django is hosted on GitHub, patches are provided in the form of pull requests.
When committing a pull request, make sure each individual commit matches the commit guidelines described below. Contributors are expected to provide the best pull requests possible. In practice mergers - who will likely be more familiar with the commit guidelines - may decide to bring a commit up to standard themselves.
You may want to have Jenkins or GitHub actions test the pull request with one of the pull request builders that doesnât run automatically, such as Oracle or Selenium. See the CI wiki page for instructions.
If you find yourself checking out pull requests locally more often, this git alias will be helpful:
[alias]
pr = !sh -c \"git fetch upstream pull/${1}/head:pr/${1} && git checkout pr/${1}\"
Add it to your ~/.gitconfig, and set upstream to be django/django.
Then you can run git pr #### to checkout the corresponding pull request.
At this point, you can work on the code. Use git rebase -i and git
commit --amend to make sure the commits have the expected level of quality.
Once youâre ready:
$ # Pull in the latest changes from main.
$ git checkout main
$ git pull upstream main
$ # Rebase the pull request on main.
$ git checkout pr/####
$ git rebase main
$ git checkout main
$ # Merge the work as "fast-forward" to main to avoid a merge commit.
$ # (in practice, you can omit "--ff-only" since you just rebased)
$ git merge --ff-only pr/XXXX
$ # If you're not sure if you did things correctly, check that only the
$ # changes you expect will be pushed to upstream.
$ git push --dry-run upstream main
$ # Push!
$ git push upstream main
$ # Delete the pull request branch.
$ git branch -d pr/xxxx
...\> REM Pull in the latest changes from main.
...\> git checkout main
...\> git pull upstream main
...\> REM Rebase the pull request on main.
...\> git checkout pr/####
...\> git rebase main
...\> git checkout main
...\> REM Merge the work as "fast-forward" to main to avoid a merge commit.
...\> REM (in practice, you can omit "--ff-only" since you just rebased)
...\> git merge --ff-only pr/XXXX
...\> REM If you're not sure if you did things correctly, check that only the
...\> REM changes you expect will be pushed to upstream.
...\> git push --dry-run upstream main
...\> REM Push!
...\> git push upstream main
...\> REM Delete the pull request branch.
...\> git branch -d pr/xxxx
Force push to the branch after rebasing on main but before merging and pushing to upstream. This allows the commit hashes on main and the branch to match which automatically closes the pull request.
If a pull request doesnât need to be merged as multiple commits, you can use GitHubâs âSquash and mergeâ button on the website. Edit the commit message as needed to conform to the guidelines and remove the pull request number thatâs automatically appended to the messageâs first line.
When rewriting the commit history of a pull request, the goal is to make Djangoâs commit history as usable as possible:
If a patch contains back-and-forth commits, then rewrite those into one. For example, if a commit adds some code and a second commit fixes stylistic issues introduced in the first commit, those commits should be squashed before merging.
Separate changes to different commits by logical grouping: if you do a stylistic cleanup at the same time as you do other changes to a file, separating the changes into two different commits will make reviewing history easier.
Beware of merges of upstream branches in the pull requests.
Tests should pass and docs should build after each commit. Neither the tests nor the docs should emit warnings.
Trivial and small patches usually are best done in one commit. Medium to large work may be split into multiple commits if it makes sense.
Practicality beats purity, so it is up to each merger to decide how much history mangling to do for a pull request. The main points are engaging the community, getting work done, and having a usable commit history.
Committing guidelinesÂś
In addition, please follow the following guidelines when committing code to Djangoâs Git repository:
Never change the published history of
django/djangobranches by force pushing. If you absolutely must (for security reasons for example), first discuss the situation with the team.For any medium-to-big changes, where âmedium-to-bigâ is according to your judgment, please bring things up on the Django Forum or django-developers mailing list before making the change.
If you bring something up and nobody responds, please donât take that to mean your idea is great and should be implemented immediately because nobody contested it. Everyone doesnât always have a lot of time to read mailing list discussions immediately, so you may have to wait a couple of days before getting a response.
Write detailed commit messages in the past tense, not present tense.
Good: âFixed Unicode bug in RSS API.â
Bad: âFixes Unicode bug in RSS API.â
Bad: âFixing Unicode bug in RSS API.â
The commit message should be in lines of 72 chars maximum. There should be a subject line, separated by a blank line and then paragraphs of 72 char lines. The limits are soft. For the subject line, shorter is better. In the body of the commit message more detail is better than less:
Fixed #18307 -- Added git workflow guidelines. Refactored the Django's documentation to remove mentions of SVN specific tasks. Added guidelines of how to use Git, GitHub, and how to use pull request together with Trac instead.
Credit the contributors in the commit message: âThanks A for the report and B for review.â Use gitâs Co-Authored-By as appropriate.
For commits to a branch, prefix the commit message with the branch name. For example: â[1.4.x] Fixed #xxxxx â Added support for mind reading.â
Limit commits to the most granular change that makes sense. This means, use frequent small commits rather than infrequent large commits. For example, if implementing feature X requires a small change to library Y, first commit the change to library Y, then commit feature X in a separate commit. This goes a long way in helping everyone follow your changes.
Separate bug fixes from feature changes. Bugfixes may need to be backported to the stable branch, according to Supported versions.
If your commit closes a ticket in the Django ticket tracker, begin your commit message with the text âFixed #xxxxxâ, where âxxxxxâ is the number of the ticket your commit fixes. Example: âFixed #123 â Added whizbang feature.â. Weâve rigged Trac so that any commit message in that format will automatically close the referenced ticket and post a comment to it with the full commit message.
For the curious, weâre using a Trac plugin for this.
Note
Note that the Trac integration doesnât know anything about pull requests. So if you try to close a pull request with the phrase âcloses #400â in your commit message, GitHub will close the pull request, but the Trac plugin will not close the same numbered ticket in Trac.
If your commit references a ticket in the Django ticket tracker but does not close the ticket, include the phrase âRefs #xxxxxâ, where âxxxxxâ is the number of the ticket your commit references. This will automatically post a comment to the appropriate ticket.
Write commit messages for backports using this pattern:
[<Django version>] Fixed <ticket> -- <description> Backport of <revision> from <branch>.
For example:
[1.3.x] Fixed #17028 -- Changed diveintopython.org -> diveintopython.net. Backport of 80c0cbf1c97047daed2c5b41b296bbc56fe1d7e3 from main.
Thereâs a script on the wiki to automate this.
If the commit fixes a regression, include this in the commit message:
Regression in 6ecccad711b52f9273b1acb07a57d3f806e93928.
(use the commit hash where the regression was introduced).
Reverting commitsÂś
Nobodyâs perfect; mistakes will be committed.
But try very hard to ensure that mistakes donât happen. Just because we have a reversion policy doesnât relax your responsibility to aim for the highest quality possible. Really: double-check your work, or have it checked by another merger before you commit it in the first place!
When a mistaken commit is discovered, please follow these guidelines:
If possible, have the original author revert their own commit.
Donât revert another authorâs changes without permission from the original author.
Use git revert â this will make a reverse commit, but the original commit will still be part of the commit history.
If the original author canât be reached (within a reasonable amount of time â a day or so) and the problem is severe â crashing bug, major test failures, etc. â then ask for objections on the Django Forum or django-developers mailing list then revert if there are none.
If the problem is small (a feature commit after feature freeze, say), wait it out.
If thereâs a disagreement between the merger and the reverter-to-be then try to work it out on the Django Forum or django-developers mailing list. If an agreement canât be reached then it should be put to a vote.
If the commit introduced a confirmed, disclosed security vulnerability then the commit may be reverted immediately without permission from anyone.
The release branch maintainer may back out commits to the release branch without permission if the commit breaks the release branch.
If you mistakenly push a topic branch to
django/django, delete it. For instance, if you did:git push upstream feature_antigravity, do a reverse push:git push upstream :feature_antigravity.