Git Bootcamp and Cheat Sheet
In this section, we'll go over some commonly used Git commands that are relevant to CPython's workflow.
Contents
- Forking CPython GitHub Repository
- Cloning The Forked CPython Repository
- Listing the Remote Repositories
- Creating and Switching Branches
- Deleting Local Branches
- Staging and Committing Files
- Reverting Changes
- Stashing Changes
- Pushing Changes
- Creating a Pull Request
- Syncing With Upstream
- Backporting Merged Changes
- Downloading Other's Patches
- Accepting and Merging A Pull Request
- Editing a Pull Request Prior to Merging
Forking CPython GitHub Repository
You'll only need to do this once.
- Go to https://github.com/python/cpython.
- Press
Forkon the top right. - When asked where to fork the repository, choose to fork it to your username.
- Your fork will be created at https://github.com/<username>/cpython.
Cloning The Forked CPython Repository
You'll only need to do this once. From your command line:
$ git clone git@github.com:<username>/cpython.git $ cd cpython $ git remote add upstream git@github.com:python/cpython.git
Listing the Remote Repositories
To list the remote repositories that are configured, along with their urls:
Creating and Switching Branches
Note
Never commit directly to the master branch.
Create a new branch and switch to it:
# creates a new branch off master and switch to it $ git checkout -b <branch-name> master
This is equivalent to:
# create a new branch off 'master', without checking it out $ git branch <branch-name> master # check out the branch $ git checkout <branch-name>
To find the branch you are currently on:
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:
To switch to a different branch:
$ git checkout <another-branch-name>Deleting Local Branches
To delete a branch that you no longer need:
$ git branch -D <branch-name>You may specify more than one branch for deletion.
Staging and Committing Files
To show the current changes:
To stage the files to be included in your commit:
$ git add path/to/file1 path/to/file2 path/to/file3To commit the files that have been staged (done in step 2):
$ git commit -m "bpo-XXXX: This is the commit message."
Reverting Changes
To revert changes to a file that has not been committed yet:
$ git checkout path/to/fileIf the change has been committed, and now you want to reset it to whatever the origin is at:
Stashing Changes
To stash away changes that are not ready to be committed yet:
To re-apply the last stashed change:
Pushing Changes
Once your changes are ready for a review or a pull request, you'll need to push them to the remote repository.
$ git checkout <branch-name> $ git push origin <branch-name>
Creating a Pull Request
- Go to https://github.com/python/cpython.
- Click
compare across forkslink. - Select the base fork:
python/cpythonand base branch:master. - Select the head fork:
<username>/cpythonand base branch: the branch containing your changes. - Press
Create Pull Requestbutton.
Syncing With Upstream
Scenario:
- You forked the CPython repository some time ago.
- Time passes.
- There have been new commits made in 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 upstream.
Solution:
$ git checkout master $ git pull --rebase upstream master $ git push origin master
The --rebase option is only needed if you have local changes to the
branch.
Another scenario:
- You created
some-branchsome time ago. - Time passes.
- You made some commits to
some-branch. - Meanwhile, there are recent changes from upstream CPython repository.
- You want to incorporate the recent changes from upstream into
some-branch.
Solution:
$ git checkout some-branch $ git fetch upstream $ git rebase upstream/master
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 master. 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 core developer who merged the pull request is expected to do the backport.
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.
Set up the following git alias:
$ 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:
Accepting and Merging A Pull Request
Pull requests can be accepted and merged by a Python Core Developer.
At the bottom of the pull request page, click the
Squash and mergebutton.Adjust and clean up the commit message. Replace the reference to GitHub PR #XXX into GH-XXX.
Example of good commit message:
bpo-12345: Improve the spam module (GH-777) * Add method A to the spam module * Update the documentation of the spam module
Example of bad commit message:
bpo-12345: Improve the spam module (#777) * Improve the spam module * merge from master * adjust code based on review comment * rebased
Press the
Confirm squash and mergebutton.
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 and Misc/NEWS.
To edit an open pull request that targets master:
In the pull request page, under the description, there is some information about the contributor's fork and branch name that will be useful later:
<contributor> wants to merge 1 commit into python:master from <contributor>:<branch_name>Fetch the pull request, using the :ref:`git pr <git_pr>` alias:
This will checkout the contributor's branch at
pr_XXX.Make and commit your changes on the branch. For example, merge in changes made to
mastersince the PR was submitted (any merge commits will be removed by the laterSquash and Mergewhen accepting the change):$ git merge origin/master $ git add <filename> $ git commit -m "<commit message>"
Push the changes back to the contributor's PR branch:
$ git push git@github.com:<contributor>/cpython <pr_XXX>:<branch_name>Optionally, delete the local PR branch:
$ git checkout master $ git branch -D <pr_XXX>