Every open source contributor at some point reviews pull requests. Pulling the contents of the PR locally is important to test the code. Many a times reviewers themselves want to fix stuff on the PR like merge conflicts, typos. GitHub has documentation to deal with this but I never found those simple enough so here's the way I suggest reviewing PRs:
Keep in mind upstream
refers to the original repository from which you forked and your own forked copy is called origin
.
Note: Things in UPPERCASE are placeholders for some GitHub specific values. You'll understand them by reading.
# Fetch PR
git fetch upstream refs/pull/PULLREQ_NUMBER/head:pr-PULLREQ_NUMBER
PULLREQ_NUMBER
is the pull request number / id you see on GitHub.
refs/pull/PULLREQ_NUMBER/head:pr-PULLREQ_NUMBER
says to pull all the commits of PR PULLREQ_NUMBER
and pipe all those to a new local branch pr-PULLREQ_NUMBER
.
# Checkout PR's branch
git checkout pr-PULLREQ_NUMBER
After checking out pr-PULLREQ_NUMBER
the PR's contents are availble for your local use.
If you want to push commit(s) to this PR you can do that too very easily. Continue reading.
You can push a commit to the PR only if you have write access (or push access) on the upstream repository. This is helpful when you want to work on a PR together or maybe you want to resolve a large conflict for first time contributors.
# Assuming you have fetched the PR & checked out to pr-PULLREQ_NUMBER by following above steps
# Make changes to files, resolve conflicts. Then add all the modified files
git add .
# Commit your changes
git commit
# Push to PR creator's branch
git push https://github.com/USERNAME_OF_PR_CREATOR/REPONAME.git pr-PULLREQ_NUMBER:PR_CREATOR_BRANCH_NAME
In above, git add
, git commit
are self explanatory.
git push https://github.com/USERNAME_OF_PR_CREATOR/REPONAME.git pr-PULLREQ_NUMBER:PR_BRANCH_NAME
looks confusing at first.
We are telling git to push to https://github.com/USERNAME_OF_PR_CREATOR/REPONAME.git
which is PR_CREATOR's fork. pr-PULLREQ_NUMBER:PR_CREATOR_BRANCH_NAME
means to push commits of your local pr-PULLREQ_NUMBER
branch to PR_CREATOR_BRANCH
.
I hope you found this helpful. To show your love please star this. I share only useful recourses on my Twitter, follow me there: https://twitter.com/vkwebdev.
You are a champion :)