Pull requests & the GitHub CLI
Propose changes with a pull request
Lesson 16 of 20 · 12 min · Module 5: Teamwork and fixing mistakes
On a team, changes reach main through review. You push a branch and open a pull request: a page where others read your changes, comment, and approve them before they are merged.
Free. No installation: you write and run the code in your browser, and it is checked automatically.
By the end, you’ll be able to…
- Push a feature branch
- Open and inspect a pull request
- Merge it and update main
The idea
After you push a branch, GitHub offers a Compare & pull request button. From the terminal, the GitHub CLI gh does the same: gh pr create --title "…" --body "…" opens a pull request, gh pr list and gh pr view show them, and gh pr merge --merge --delete-branch merges one, deletes the branch, and updates your local main. Many teams require an approval before a pull request can be merged.
git push -u origin add-faq
gh pr create --title "Add a FAQ page" --body "A first FAQ."
gh pr merge --merge --delete-branch
01Push a branch, not main
Push your feature branch. GitHub replies with a link to open a pull request for it.
git switch -c add-faq
printf '# FAQ\n\nAsk me anything about this site.\n' > faq.md
git add faq.md
git commit -m "Add a FAQ page"
git push -u origin add-faqSwitched to a new branch 'add-faq'
[add-faq 5cd6971] Add a FAQ page
1 file changed, 3 insertions(+)
create mode 100644 faq.md
Enumerating objects: 4, done.
Counting objects: 100% (4/4), done.
Delta compression using up to 8 threads
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 301 bytes | 301.00 KiB/s, done.
Total 3 (delta 1), reused 0 (delta 0), pack-reused 0 (from 0)
remote: Resolving deltas: 100% (1/1), completed with 1 local object.
remote:
remote: Create a pull request for 'add-faq' on GitHub by visiting:
remote: https://github.com/learner/website/pull/new/add-faq
remote:
To https://github.com/learner/website.git
* [new branch] add-faq -> add-faq
branch 'add-faq' set up to track 'origin/add-faq'.02Open the pull request
The pull request shows your commits and changes. Teammates comment on lines, ask for changes, and approve. Every new push to the branch updates the pull request.
gh pr create --title "Add a FAQ page" --body "A first FAQ page with one question."
gh pr view
Creating pull request for add-faq into main in learner/website
https://github.com/learner/website/pull/1
Add a FAQ page #1
Open • learner wants to merge 1 commit into main from add-faq • less than a minute ago
+3 -0 • No checks
A first FAQ page with one question.
View this pull request on GitHub: https://github.com/learner/website/pull/103Merge and update main
gh pr merge merges the pull request on GitHub. With --delete-branch it also deletes the branch here and on GitHub, switches you to main, and pulls the merge. origin/add-faq is only your computer's memory of the deleted branch; git fetch --prune clears such leftovers.
gh pr merge --merge --delete-branch
git log --oneline -3✓ Merged pull request learner/website#1 (Add a FAQ page)
✓ Deleted local branch add-faq and switched to branch main
✓ Deleted remote branch add-faq
4809b0a (HEAD -> main, origin/main, origin/HEAD) Merge pull request #1 from learner/add-faq
5cd6971 (origin/add-faq) Add a FAQ page
4abfd2a Say what the site is aboutYour turn
add-faq, add faq.md starting with # FAQ, commit it as Add a FAQ page, and push the branch with -u. Open a pull request titled Add a FAQ page with a short description, merge it with gh pr merge --merge --delete-branch, and check that your local main has the merge with git log --oneline.What the checks look for
- Pull request #1 is merged
- The pull request has a description
- main on GitHub has faq.md
- Your local main has the merge
- add-faq is deleted here and on GitHub
Take this with you
Push a branch, open a pull request, merge after review, then pull main.
Go a little further
gh pr merge --squash turns all commits of a pull request into one. In a fresh run, compare the history it produces with --merge.