Making a Pull Request on a GitHub Repo
Guide for contributing to another code base by making a pull request
Public Repo
- Identify a project you want to contribute to on GitHub
- Fork that project
- Clone it to your local machine*
note : Do not clone in another directory that was already cloned from another repo. For example, when I cloned pytalentsolution on my local machine, I initially cloned it inside Saku directory, which was itself a clone of an already-existing-repo. The correct way was to create a new directory for pytalentsolution.
- Make a new branch
- 4a:
cdinto directory of the cloned repo - 4b: use
git branchto confirm you’re on the*masterbranch - 4c: use
git checkout -b name_of_new_branch - 4d: use
git branchto check that you’re onname_of_new_branch
-
Make changes
-
Push it back to your repo
note : You’ll get a message git push remote -- use this instead of the traditional git push
- (Go back to the forked repo in GitHub) Click the Compare & Pull Request button.
- add a description to changes made in the pull request
- Click Create pull request to open a new pull request
Private Repo
1-3. You cannot fork a private repo
-
Clone directly to local machine
-
git branch -ato see all branches in the project -
git branchto confirm you’re on master branch -
[important]
git statusto check if ahead or behind (if behind, usegit pullto fetch and download content from a remote repository) -
[from git master] create a new branch use
git checkout -b name_of_new_branch -
Use
git branchto make sure you’re on new branch -
Make changes, then normal: git add, git commit -m “message”, git push
note : You’ll get a message git push remote -- use this instead of the traditional git push
- (Go back to the forked repo in GitHub) Click the Compare & Pull Request button.
- Add good PR description, see here
- Click Create pull request to open a new pull request
Production vs Development
Ongoing projects with several contributors will generally separate the main from develop branch. Making a PR in this context is slighty different:
-
In the command line, switch to development branch
git checkout develop(even if you don’t see thedevelopbranch locally, you may just seemainor a new branch you creatednew_branch). -
Now that you’re starting on
developbranch, dogit pull origin developto make sure that any prior changes from thedevelopbranch is pulled in locally, and you’re up-to-date. -
Then from
develop, create a newfeaturebranch like so:git checkout feature/new-branch. (note:feature/new-branchis a naming convention that explicitly says you’re creating a new feature branch). -
Then make your changes or add new code.
-
Then,
git push origin(in this case, origin will be develop) -
Once you go back to github, if you see
compare and pull request, make sure it is being merged intodevelopand notmain.
When Feature branch goes out of sync with Development
Sometimes you’ve already pushed a pull request, but the development branch goes ahead of your proposed changes. Here’s how to handle:
- Switch to a specific feature branch (no need to go back to develop):
git checkout branch_name - Run
git pull origin develop - Then
git push origin
3a. If there’s an Index Error, run
git reset --hard*git clean -df
git resetis to take the current branch and reset it to point somewhere else, also bringing the index and working tree along. Here’s a more visual explanation ( source)
If your main branch is C and you want to point your current branch somewhere else:
- A - B - C (HEAD, main branch)
and you want to point to B, not C, then you use git reset B to move it there:
- A - B (HEAD, main branch) # - C is still here, but there's no branch pointing to it anymore
VIM
If for whatever reason you find yourself on VIM, you can escape by:
- Press
esc(escape) - Press
:(colon) - Press
wq(write and quit)
Or one-line command to get out of VIM
- Press
:wq!(colon, write and quit)