33. Git Bootcamp and Cheat Sheet

Note

This section provides instructions on common tasks in CPython’s workflow. It’s designed to assist new contributors who have some familiarity with git and GitHub.

If you are new to git and GitHub, please become comfortable with these instructions before submitting a pull request. As there are several ways to accomplish these tasks using git and GitHub, this section reflects one method suitable for new contributors. Experienced contributors may desire a different approach.

In this section, we will go over some commonly used Git commands that are relevant to CPython’s workflow.

Note

Setting up git aliases for common tasks can be useful to you. You can get more information about that in git documentation

Contents

33.1. Forking CPython GitHub Repository

You will only need to do this once.

  1. Go to https://github.com/python/cpython.

  2. Press Fork on the top right.

  3. When asked where to fork the repository, choose to fork it to your username.

  4. Your forked CPython repository will be created at https://github.com/<username>/cpython.

33.2. Cloning a Forked CPython Repository

You will only need to do this once. From your command line:

  1. git clone git@github.com:<username>/cpython.git

It is also recommended to configure an upstream remote repository:

  1. cd cpython
  2. git remote add upstream git@github.com:python/cpython.git

You can also use SSH-based or HTTPS-based URLs.

33.3. Listing the Remote Repositories

To list the remote repositories that are configured, along with their URLs:

  1. git remote -v

You should have two remote repositories: origin pointing to your forked CPython repository, and upstream pointing to the official CPython repository:

  1. origin git@github.com:<username>/cpython.git (fetch)
  2. origin git@github.com:<username>/cpython.git (push)
  3. upstream git@github.com:python/cpython.git (fetch)
  4. upstream git@github.com:python/cpython.git (push)

33.4. Setting Up Your Name and Email Address

  1. git config --global user.name "Your Name"
  2. git config --global user.email your.email@example.com

The --global flag sets these parameters globally while the --local flag sets them only for the current project.

33.5. Enabling autocrlf on Windows

The autocrlf option will fix automatically any Windows-specific line endings. This should be enabled on Windows, since the public repository has a hook which will reject all changesets having the wrong line endings:

  1. git config --global core.autocrlf input

33.6. Creating and Switching Branches

Important

Never commit directly to the main branch.

Create a new branch and switch to it:

  1. # creates a new branch off main and switch to it
  2. git checkout -b <branch-name> main

This is equivalent to:

  1. # create a new branch from main, without checking it out
  2. git branch <branch-name> main
  3. # check out the branch
  4. git checkout <branch-name>

To find the branch you are currently on:

  1. git branch

The current branch will have an asterisk next to the branch name. Note, this will only list all of your local branches.

To list all the branches, including the remote branches:

  1. git branch -a

To switch to a different branch:

  1. git checkout <another-branch-name>

Other releases are just branches in the repository. For example, to work on the 2.7 release from the upstream remote:

  1. git checkout -b 2.7 upstream/2.7

33.7. Deleting Branches

To delete a local branch that you no longer need:

  1. git checkout main
  2. git branch -D <branch-name>

To delete a remote branch:

  1. git push origin -d <branch-name>

You may specify more than one branch for deletion.

33.8. Renaming Branch

The CPython repository’s default branch was renamed from master to main after the Python 3.10b1 release.

If you have a fork on GitHub (as described in Forking CPython GitHub Repository) that was created before the rename, you should visit the GitHub page for your fork to rename the branch there. You only have to do this once. GitHub should provide you with a dialog for this. If it doesn’t (or the dialog was already dismissed), you can rename the branch in your fork manually by following these GitHub instructions

After renaming the branch in your fork, you need to update any local clones as well. This only has to be done once per clone:

  1. git branch -m master main
  2. git fetch origin
  3. git branch -u origin/main main
  4. git remote set-head origin -a

(GitHub also provides these instructions after you rename the branch.)

If you do not have a fork on GitHub, but rather a direct clone of the main repo created before the branch rename, you still have to update your local clones. This still only has to be done once per clone. In that case, you can rename your local branch as follows:

  1. git branch -m master main
  2. git fetch upstream
  3. git branch -u upstream/main main

33.9. Staging and Committing Files

  1. To show the current changes:

    1. git status
  2. To stage the files to be included in your commit:

    1. git add <filename1> <filename2>
  3. To commit the files that have been staged (done in step 2):

    1. git commit -m "bpo-XXXX: This is the commit message."

33.10. Reverting Changes

To revert changes to a file that has not been committed yet:

  1. git checkout <filename>

If the change has been committed, and now you want to reset it to whatever the origin is at:

  1. git reset --hard HEAD

33.11. Stashing Changes

To stash away changes that are not ready to be committed yet:

  1. git stash

To re-apply the last stashed change:

  1. git stash pop

33.12. Committing Changes

Add the files you want to commit:

  1. git add <filename>

Commit the files:

  1. git commit -m "<message>"

33.13. Pushing Changes

Once your changes are ready for a review or a pull request, you will need to push them to the remote repository.

  1. git checkout <branch-name>
  2. git push origin <branch-name>

33.14. Creating a Pull Request

  1. Go to https://github.com/python/cpython.

  2. Press the New pull request button.

  3. Click the compare across forks link.

  4. Select the base repository: python/cpython and base branch: main.

  5. Select the head repository: <username>/cpython and head branch: the branch containing your changes.

  6. Press the Create pull request button.

33.15. Updating your CPython Fork

Scenario:

  • You forked the CPython repository some time ago.

  • Time passes.

  • There have been new commits made in the upstream CPython repository.

  • Your forked CPython repository is no longer up to date.

  • You now want to update your forked CPython repository to be the same as the upstream CPython repository.

Please do not try to solve this by creating a pull request from python:main to <username>:main as the authors of the patches will get notified unnecessarily.

Solution:

  1. git checkout main
  2. git pull upstream main
  3. git push origin main

Note

For the above commands to work, please follow the instructions found in the Get the source code section

Another scenario:

  • You created some-branch some time ago.

  • Time passes.

  • You made some commits to some-branch.

  • Meanwhile, there are recent changes from the upstream CPython repository.

  • You want to incorporate the recent changes from the upstream CPython repository into some-branch.

Solution:

  1. git checkout some-branch
  2. git fetch upstream
  3. git merge upstream/main
  4. git push origin some-branch

You may see error messages like “CONFLICT” and “Automatic merge failed;” when you run git merge upstream/main.

When it happens, you need to resolve conflict. See these articles about resolving conflicts:

33.16. Applying a Patch from Mercurial to Git

Scenario:

  • A Mercurial patch exists but there is no pull request for it.

Solution:

  1. Download the patch locally.

  2. Apply the patch:

    1. git apply /path/to/issueNNNN-git.patch

    If there are errors, update to a revision from when the patch was created and then try the git apply again:

    1. git checkout $(git rev-list -n 1 --before="yyyy-mm-dd hh:mm:ss" main)
    2. git apply /path/to/issueNNNN-git.patch

    If the patch still won’t apply, then a patch tool will not be able to apply the patch and it will need to be re-implemented manually.

  3. If the apply was successful, create a new branch and switch to it.

  4. Stage and commit the changes.

  5. If the patch was applied to an old revision, it needs to be updated and merge conflicts need to be resolved:

    1. git rebase main
    2. git mergetool
  6. Push the changes and open a pull request.

33.17. Downloading Other’s Patches

Scenario:

  • A contributor made a pull request to CPython.

  • Before merging it, you want to be able to test their changes locally.

On Unix and MacOS, set up the following git alias:

  1. $ git config --global alias.pr '!sh -c "git fetch upstream pull/${1}/head:pr_${1} && git checkout pr_${1}" -'

On Windows, reverse the single (') and double (") quotes:

  1. git config --global alias.pr "!sh -c 'git fetch upstream pull/${1}/head:pr_${1} && git checkout pr_${1}' -"

The alias only needs to be done once. After the alias is set up, you can get a local copy of a pull request as follows:

  1. git pr <pr_number>

Note

hub command line utility makes this workflow very easy. You can check out the branch by hub pr checkout <pr_number> [<branch_name>]. This command configures remote URL for the branch too. So you can git push if the pull request author checked “Allow edits from maintainers” when creating the pull request.

33.18. Accepting and Merging a Pull Request

Pull requests can be accepted and merged by a Python Core Developer.

  1. At the bottom of the pull request page, click the Squash and merge button.

  2. Replace the reference to GitHub pull request #NNNN with GH-NNNN. If the title is too long, the pull request number can be added to the message body.

  3. Adjust and clean up the commit message.

    Example of good commit message:

    1. bpo-12345: Improve the spam module (GH-777)
    2. * Add method A to the spam module
    3. * Update the documentation of the spam module

    Example of bad commit message:

    1. bpo-12345: Improve the spam module (#777)
    2. * Improve the spam module
    3. * merge from main
    4. * adjust code based on review comment
    5. * rebased

    Note

    How to Write a Git Commit Message is a nice article describing how to write a good commit message.

  4. Press the Confirm squash and merge button.

33.19. Backporting Merged Changes

A pull request may need to be backported into one of the maintenance branches after it has been accepted and merged into main. It is usually indicated by the label needs backport to X.Y on the pull request itself.

Use the utility script cherry_picker.py from the core-workflow repository to backport the commit.

The commit hash for backporting is the squashed commit that was merged to the main branch. On the merged pull request, scroll to the bottom of the page. Find the event that says something like:

  1. <core_developer> merged commit <commit_sha1> into python:main <sometime> ago.

By following the link to <commit_sha1>, you will get the full commit hash.

Alternatively, the commit hash can also be obtained by the following git commands:

  1. git fetch upstream
  2. git rev-parse ":/bpo-12345"

The above commands will print out the hash of the commit containing "bpo-12345" as part of the commit message.

When formatting the commit message for a backport commit: leave the original one as is and delete the number of the backport pull request.

Example of good backport commit message:

  1. bpo-12345: Improve the spam module (GH-777)
  2. * Add method A to the spam module
  3. * Update the documentation of the spam module
  4. (cherry picked from commit 62adc55)

Example of bad backport commit message:

  1. bpo-12345: Improve the spam module (GH-777) (#888)
  2. * Add method A to the spam module
  3. * Update the documentation of the spam module

33.20. Editing a Pull Request Prior to Merging

When a pull request submitter has enabled the Allow edits from maintainers option, Python Core Developers may decide to make any remaining edits needed prior to merging themselves, rather than asking the submitter to do them. This can be particularly appropriate when the remaining changes are bookkeeping items like updating Misc/ACKS.

To edit an open pull request that targets main:

  1. In the pull request page, under the description, there is some information about the contributor’s forked CPython repository and branch name that will be useful later:

    1. <contributor> wants to merge 1 commit into python:main from <contributor>:<branch_name>
  2. Fetch the pull request, using the git pr alias:

    1. git pr <pr_number>

    This will checkout the contributor’s branch at <pr_number>.

  3. Make and commit your changes on the branch. For example, merge in changes made to main since the PR was submitted (any merge commits will be removed by the later Squash and Merge when accepting the change):

    1. git fetch upstream
    2. git merge upstream/main
    3. git add <filename>
    4. git commit -m "<message>"
  4. Push the changes back to the contributor’s PR branch:

    1. git push git@github.com:<contributor>/cpython <pr_number>:<branch_name>
  5. Optionally, delete the PR branch.