-
-
Notifications
You must be signed in to change notification settings - Fork 872
Add Git Bootcamp page #144
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 5 commits
142f1df
4321772
2104dea
efee4f7
2993cbc
6284566
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,268 @@ | ||
.. _gitbootcamp: | ||
|
||
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 | ||
--------------------------------- | ||
|
||
You'll only need to do this once. | ||
|
||
1. Go to https://github.com/python/cpython. | ||
|
||
2. Press ``Fork``. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. "Press the |
||
|
||
3. When asked where to fork the repository, choose to fork it to your username. | ||
|
||
4. A fork will be created at https://github.com/<username>/cpython. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Your fork? |
||
|
||
|
||
Cloning The Forked CPython Repository | ||
------------------------------------- | ||
|
||
From your command line:: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In this section you could also mention that this is only necessary once. |
||
|
||
$ git clone [email protected]:<username>/cpython.git | ||
$ cd cpython | ||
$ git remote add upstream [email protected]:python/cpython.git | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Using There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It is possible to set the default syntax highlight on a page-by-page basis, and specify that this page should use shell highlight. I'm not sure what the default is for the devguide. You should be able to set it by adding There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Perhaps explain briefly (even with an inline comment), why adding the remote is necessary/useful. |
||
|
||
|
||
Listing the Remote Repositories | ||
------------------------------- | ||
|
||
:: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A sentence to explain why this is useful would be nice. |
||
|
||
$ git remote -v | ||
|
||
|
||
Creating and Switching Branches | ||
------------------------------- | ||
|
||
.. note:: | ||
Never commit directly to the ``master`` branch. | ||
|
||
Create a new branch:: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. "Create a new branch |
||
|
||
$ git checkout -b some-branch master # creates a new branch off master | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use a double space in front of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe use |
||
|
||
which is equal to:: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Might want to mention it's equivalent if you are starting from the master branch, otherwise explicitly add the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks, I made the change in line 56. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. "This is equivalent to:" |
||
|
||
$ git branch some-branch master # create 'some-branch' off 'master', without checking it out | ||
$ git checkout some-branch # check out 'some-branch' | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As above, either double space before the comment or comments on separate lines. |
||
|
||
To find out which branch you are in now:: | ||
|
||
$ git branch | ||
|
||
The current branch will have an asterisk next to the branch name. Note, this | ||
will list all of your local branches. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Lately I've been using |
||
|
||
To list all the branches, including the remote branches:: | ||
|
||
$ git branch -a | ||
|
||
To switch to a different branch:: | ||
|
||
$ git checkout another-branch-name | ||
|
||
|
||
Delete Local Branch | ||
------------------- | ||
|
||
To delete branch that you no longer need:: | ||
|
||
$ git branch -D branch-to-delete | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Shouldn't this be
As far as I understand:
Please correct me if I'm wrong. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The capital |
||
|
||
|
||
Staging and Committing Files | ||
---------------------------- | ||
|
||
1. To show the current changes:: | ||
|
||
$ git status | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe mention |
||
|
||
2. To stage the files to be included in your commit:: | ||
|
||
$ git add /path/to/file1 path/to/file2 path/to/file3 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Extra There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Will remove. |
||
|
||
3. To commit the files that have been staged (done in step 2):: | ||
|
||
$ git commit -m "This is the commit message. Prefix it with bpo-XXXX." | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe use |
||
|
||
|
||
Reverting Changes | ||
----------------- | ||
|
||
To revert changes to a file that has not been committed yet:: | ||
|
||
$ git checkout path/to/file | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This line has a 4-spaces indent, instead of 3-spaces. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed in the next commit. |
||
|
||
If the change has been committed, and now you want to reset it to whatever | ||
the origin is at:: | ||
|
||
$ git reset --hard HEAD | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What is the difference between this and There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not sure... I don't normally use the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I find this section a bit confusing/misleading. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There are two more scenarios where I used
The second case works differently since we are using PRs. With HG I could apply a patch and do |
||
|
||
|
||
Stashing Changes | ||
---------------- | ||
|
||
To stash away changes that are not ready to be committed yet:: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Coming from HG, the concept of staging and stashing are foreign. While I am now somewhat familiar with them, I'd prefer a goal-oriented approach, i.e.:
(Feel free to change the wording if it doesn't accurately reflect what |
||
|
||
$ git stash | ||
|
||
To re-apply last stashed change:: | ||
|
||
$ git stash pop | ||
|
||
|
||
Pushing Changes | ||
--------------- | ||
|
||
Once your changes are ready for a review or a pull request, you'll need to push | ||
them to the remote repository. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. "...push them to your GitHub fork." |
||
|
||
:: | ||
|
||
$ git checkout some-branch | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If I omit this, will the next command fail? |
||
$ git push origin some-branch | ||
|
||
|
||
Creating a Pull Request | ||
----------------------- | ||
|
||
1. Go to https://github.com/python/cpython. | ||
|
||
2. Click ``compare across forks`` link. | ||
|
||
3. Select the base fork: ``python/cpython`` and base branch: ``master`` | ||
|
||
4. Select the head fork: ``<username>/cpython`` and base branch: the branch | ||
containing your changes. | ||
|
||
5. Press ``Create Pull Request`` button. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Isn't it easier to just go on my fork, and press the "Compare & pull request" button? I'm not sure when that button is available, but so far I've always found it after pushing a new branch. Edit: after reading below I'm not sure if this works for other branches, but maybe it does? |
||
|
||
|
||
Syncing With Upstream | ||
--------------------- | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it would be easier to configure the remote to |
||
|
||
Scenario: | ||
|
||
- You forked cpython repository some time ago. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Either CPython or |
||
- Time passes. | ||
- There have been new commits made in upstream cpython repository. | ||
- Your forked cpython repository is no longer up to date. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So far I've used "(GH) fork" to refer to the GitHub fork at github.com/myusername/cpython, and "(local) clone" to refer to the local copy on my pc. |
||
- You now want to update your forked cpython repository to be the same as | ||
upstream. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. While I like the goal-oriented approach, I somewhat dislike the format. I think this whole bullet list can be replaced with a single sentence: "If you want to update your repo with the latest commits from the upstream CPython repo, use::" There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is there a reason to want to update |
||
|
||
Solution:: | ||
|
||
$ git checkout master | ||
$ git pull --rebase upstream master | ||
$ git push origin master | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. FWIW I've been using There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Inline comments would be useful:
|
||
|
||
The `--rebase` is only needed if you have local changes to the branch. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Above you mentioned that you should never commit to master, so I would assume |
||
|
||
Another scenario: | ||
|
||
- You created ``some-branch`` some 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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I recently came across this scenario, and the solution I used was:
This has the advantage of also updating the local master. I think this also assumes that the first command is done in the Perhaps this should also mention two other things:
|
||
|
||
|
||
Backporting Merged Changes | ||
-------------------------- | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Who is the target here? Are contributors supposed to do this or is it only for core devs? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think core dev who merged the PR is expected to do the backport, mainly because it involves removing For now I will just state here that the core dev is expected to do the backport. Once the cherry-picking bot is in place, this section will just be obsolete. |
||
|
||
When a pull request has been merged to master, and it needs to be backported | ||
into one of the maintenance branches. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This sentence seems incomplete. |
||
|
||
First, obtain the commit sha1 from the merged pull request: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Below you use "hash", so perhaps s/sha1/hash/ |
||
|
||
1. Go to the merged pull request page, for example:: | ||
|
||
https://github.com/python/cpython/pull/PR-ID | ||
|
||
2. Scroll down and find the activity that says something like:: | ||
|
||
CoreDeveloper merged commit <hash> into python:master ... | ||
|
||
3. Follow the link to <hash>. | ||
|
||
4. Copy the complete hash value. | ||
|
||
The commit hash will be used below. | ||
|
||
To backport the commit to 3.6:: | ||
|
||
$ git fetch upstream | ||
$ git checkout -b backport-someissue-3.6 upstream/3.6 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this should be done while in the Would this work too?
Coming from HG, my thinking is more or less:
Whereas by reading this commands it seems to me that a local upstream exists and needs to be updated (even though upstream itself is remote), and by updating the local upstream, master doesn't get updated (and I will need to remember to update it next time I create a branch from it). I therefore find my approach (update master from upstream, branch off master) simpler, but I'm not sure if it's "idiomatic". |
||
$ git cherry-pick -x hashvalue | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. s/hashvalue//? |
||
$ git push origin backport-someissue-3.6 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Inline comments might be useful. |
||
|
||
Go to https://github.com/python/cpython to create the pull request. Select | ||
``3.6`` as the base branch, and ``backport-someissue-3.6`` as the head branch. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I haven't tried tried backporting a changeset yet, but I think the "Compare & create PR" button on my fork should work for this too. Hopefully it's smart enough to compare with 3.6 (but that might depend on how the branch got created?), but if not, I believe it's possible to manually select the branch after pressing the button. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Compare & create PR by default will select the With cherry_picker.py, the correct maintenance branch will be pre-selected :) |
||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You forgot to mention your script. 😄 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Once it's moved to core-workflow, I'll update this :) |
||
|
||
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}" -' | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is the third variant of what I believe is the same thing. The other two I saw are:
The first one seem equivalent to the alias, except that it needs to be done manually every time. The second one doesn't require to create an additional alias but it needs to be added to each There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This alias was suggested by @zware. I don't actually know about the other variants. So I'll just leave this as is. |
||
|
||
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> | ||
|
||
For example, to fetch and checkout pull request #777:: | ||
|
||
$ git pr 777 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this is the only example in the page -- elsewhere you only used . There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'll remove the example to make it consistent. |
||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Another scenario that is not covered here is: how can I update a PR that I pulled from a contributor (e.g. to add Misc/NEWS)?
It was not clear how the author is determined if multiple users (contributor and core-dev) worked on the same PR: author of the first commit? owner of the repo the PR comes from? author with most commits/loc in the PR? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I haven't had to do this myself, so I don't have the answer ... |
||
|
||
Accepting and Merging A Pull Request | ||
------------------------------------ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should be marked as core-devs only, since contributors can't accept/merge. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Mentioned in the next line. |
||
|
||
Pull requests can be accepted and merged by a Python Core Developer. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A question about "merging etiquette": if another core-dev submitted a PR, and it looks good to me, should I go ahead and merge it, or should I just leave a positive review and let the original core-dev merge it? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Perhaps this is to be asked at core-workflow or python-committers? :) |
||
|
||
1. At the bottom of the pull request page, click the ``Squash and merge`` | ||
button. | ||
|
||
2. 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 | ||
|
||
3. Press the ``Confirm squash and merge`` button. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just a nit: I think this should either be "Git" or
:cmd:`git`
.