60 captures
25 Jun 2018 - 24 Mar 2025
Sep OCT Nov
28
2019 2020 2021
success
fail

About this capture

COLLECTED BY

Collection: Common Crawl

Web crawl data from Common Crawl.
TIMESTAMPS

The Wayback Machine - http://web.archive.org/web/20201028202347/https://devguide.python.org/gitbootcamp/
 

Navigation



index

next |

previous |


Python »
  Python Developer's Guide »  
|  







32. Git Bootcamp and Cheat Sheet



Note

This section provides instructions on common tasks in CPythons workflow. Its 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 CPythons 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


Git Bootcamp and Cheat Sheet

Forking CPython GitHub Repository

Cloning a Forked CPython Repository

Listing the Remote Repositories

Setting Up Your Name and Email Address

Enabling autocrlf on Windows

Creating and Switching Branches

Deleting Branches

Staging and Committing Files

Reverting Changes

Stashing Changes

Committing Changes

Pushing Changes

Creating a Pull Request

Syncing with Upstream

Applying a Patch from Mercurial to Git

Downloading Others Patches

Accepting and Merging a Pull Request

Backporting Merged Changes

Editing a Pull Request Prior to Merging





32.1. Forking CPython GitHub Repository


You will only need to do this once.


(一)Go to https://github.com/python/cpython.

(二)Press Fork on the top right.

(三)When asked where to fork the repository, choose to fork it to your username.

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



32.2. Cloning a Forked CPython Repository


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

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



It is also recommended to configure an upstream remote repository:

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



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


32.3. Listing the Remote Repositories


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

git remote -v



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

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




32.4. Setting Up Your Name and Email Address


git config --global user.name "Your Name"
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.


32.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:

git config --global core.autocrlf input




32.6. Creating and Switching Branches



Important

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 from 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:

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:

git branch -a



To switch to a different branch:

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:

git checkout -b 2.7 upstream/2.7




32.7. Deleting Branches


To delete a local branch that you no longer need:

git checkout master
git branch -D <branch-name>



To delete a remote branch:

git push origin -d <branch-name>



You may specify more than one branch for deletion.


32.8. Staging and Committing Files



To show the current changes:

git status




To stage the files to be included in your commit:

git add <filename1> <filename2>




To commit the files that have been staged (done in step 2):

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






32.9. Reverting Changes


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

git checkout <filename>



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

git reset --hard HEAD




32.10. Stashing Changes


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

git stash



To re-apply the last stashed change:

git stash pop




32.11. Committing Changes


Add the files you want to commit:

git add <filename>



Commit the files:

git commit -m "<message>"




32.12. Pushing Changes


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

git checkout <branch-name>
git push origin <branch-name>




32.13. Creating a Pull Request



(一)Go to https://github.com/python/cpython.

(二)Press the New pull request button.

(三)Click the compare across forks link.

(四)Select the base repository: python/cpython and base branch: master.

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

(六)Press the Create pull request button.



32.14. Syncing with Upstream


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.


Solution:

git checkout master
git pull upstream master
git push origin master



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:

git checkout some-branch
git fetch upstream
git merge upstream/master
git push origin some-branch



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

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


About merge conflicts

Resolving a merge conflict using the command line



32.15. Applying a Patch from Mercurial to Git


Scenario:


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


Solution:


Download the patch locally.


Apply the patch:

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:

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



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


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


Stage and commit the changes.


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

git rebase master
git mergetool




Push the changes and open a pull request.




32.16. Downloading Others 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:

$ 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:

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:

git pr <pr_number>




Note

hub command line utility makes this workflow very easy. You can check out the branch by hub prcheckout <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.



32.17. 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 merge button.


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.


Adjust and clean up the commit message.

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




Note

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



Press the Confirm squash and merge button.




32.18. 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 toX.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 master branch. On the merged pull request, scroll to the bottom of the page. Find the event that says something like:

<core_developer> merged commit <commit_sha1> into python:master <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:

git fetch upstream
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:

bpo-12345: Improve the spam module (GH-777)

* Add method A to the spam module
* Update the documentation of the spam module

(cherry picked from commit 62adc55)



Example of bad backport commit message:

bpo-12345: Improve the spam module (GH-777) (#888)

* Add method A to the spam module
* Update the documentation of the spam module




32.19. 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 master:


In the pull request page, under the description, there is some information about the contributors forked CPython repository 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 git pr alias:

git pr <pr_number>



This will checkout the contributors branch at <pr_number>.


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

git fetch upstream
git merge upstream/master
git add <filename>
git commit -m "<message>"




Push the changes back to the contributors PR branch:

git push git@github.com:<contributor>/cpython <pr_number>:<branch_name>




Optionally, delete the PR branch.




 




Table of Contents



32. Git Bootcamp and Cheat Sheet

32.1. Forking CPython GitHub Repository

32.2. Cloning a Forked CPython Repository

32.3. Listing the Remote Repositories

32.4. Setting Up Your Name and Email Address

32.5. Enabling autocrlf on Windows

32.6. Creating and Switching Branches

32.7. Deleting Branches

32.8. Staging and Committing Files

32.9. Reverting Changes

32.10. Stashing Changes

32.11. Committing Changes

32.12. Pushing Changes

32.13. Creating a Pull Request

32.14. Syncing with Upstream

32.15. Applying a Patch from Mercurial to Git

32.16. Downloading Others Patches

32.17. Accepting and Merging a Pull Request

32.18. Backporting Merged Changes

32.19. Editing a Pull Request Prior to Merging



Sections



1. Getting Started

2. Where to Get Help

3. Lifecycle of a Pull Request

4. Running & Writing Tests

5. Increase Test Coverage

6. Helping with Documentation

7. Documenting Python

8. Silence Warnings From the Test Suite

9. Fixing easy Issues (and Beyond)

10. Issue Tracking

11. Triaging an Issue

12. Following Pythons Development

13. Porting Python to a new platform

14. How to Become a Core Developer

15. Developer Log

16. Accepting Pull Requests

17. Development Cycle

18. Continuous Integration

19. Adding to the Stdlib

20. Changing the Python Language

21. Experts Index

22. gdb Support

23. Exploring CPythons Internals

24. Changing CPythons Grammar

25. Design of CPythons Compiler

26. Design of CPythons Garbage Collector

27. Updating standard library extension modules

28. Coverity Scan

29. Dynamic Analysis with Clang

30. Running a buildbot worker

31. Core Developer Motivations and Affiliations

32. Git Bootcamp and Cheat Sheet

32.1. Forking CPython GitHub Repository

32.2. Cloning a Forked CPython Repository

32.3. Listing the Remote Repositories

32.4. Setting Up Your Name and Email Address

32.5. Enabling autocrlf on Windows

32.6. Creating and Switching Branches

32.7. Deleting Branches

32.8. Staging and Committing Files

32.9. Reverting Changes

32.10. Stashing Changes

32.11. Committing Changes

32.12. Pushing Changes

32.13. Creating a Pull Request

32.14. Syncing with Upstream

32.15. Applying a Patch from Mercurial to Git

32.16. Downloading Others Patches

32.17. Accepting and Merging a Pull Request

32.18. Backporting Merged Changes

32.19. Editing a Pull Request Prior to Merging



33. Appendix: Topics

Previous topic


31. Core Developer Motivations and Affiliations

Next topic


33. Appendix: Topics

This Page



Show Source  







Navigation



index

next |

previous |


Python »
  Python Developer's Guide »  
|  



© Copyright 2011-2020, Python Software Foundation.  
The Python Software Foundation is a non-profit corporation. Please donate.
 
Last updated on Oct 26, 2020.  Found a bug?
 Created using Sphinx 1.8.5.