Pull requests and working as a team
Work the way professional teams do: push a feature branch, open a pull request on GitHub, review and discuss changes, merge, then update your local copy. Also learn forks and issues.
🎯 By the end of this lesson you will be able to:
- Push a branch and open a pull request on GitHub
- Review, discuss and merge a pull request
- Update and tidy your local repository after a merge
Outline
What you'll learn and why it matters
In a team, nobody pushes straight to main. Instead, each piece of work goes on a branch, and a pull request (PR) asks: *please review my changes and merge them into main*. Teammates read the changes, comment and approve, and automated checks run. This is the daily workflow in almost every software company, and the skill interviewers ask about most.
The GitHub flow
| Step | Where | Command or action |
|---|---|---|
| 1. Start from the latest main | Terminal | git switch main then git pull |
| 2. Create a branch | Terminal | git switch -c contact-page |
| 3. Commit your work | Terminal | git add … and git commit -m … |
| 4. Push the branch | Terminal | git push -u origin contact-page |
| 5. Open a pull request | GitHub | Compare & pull request button |
| 6. Review and fix | GitHub + terminal | Comments; push more commits to the same branch |
| 7. Merge | GitHub | Merge pull request |
| 8. Update and tidy | Terminal | git switch main, git pull, git branch -d contact-page |
Push your branch
Asha built her contact page on a branch, as in lesson 6. Push the branch to GitHub:
git push -u origin contact-pageOutput
To https://github.com/asha-kumar/my-website.git * [new branch] contact-page -> contact-page branch 'contact-page' set up to track 'origin/contact-page'.
On the real GitHub, the push output also includes a link: *Create a pull request for 'contact-page' on GitHub by visiting…*. You can open that link, or use the yellow banner GitHub shows on the repository page.
Open a pull request
- On the repository page on GitHub, click Compare & pull request in the banner (or go to the Pull requests tab and click New pull request, choosing
contact-pageto merge intomain). - Write a clear title, such as *Add contact page*.
- In the description, say what you changed, why, and how to check it. For example: *Adds contact.html with my email. Check by opening the page and clicking the link in the menu.*
- On the right, you can request Reviewers (teammates) and link an Issue it fixes.
- Click Create pull request.
The pull request page has tabs: Conversation for discussion, Commits for the branch's commits, and Files changed, which shows the diff, like git diff in colour. Reviewers can click any line to comment on it.
Reviewing and updating a pull request
If a reviewer asks for a change, you do not open a new pull request. Make the change on the same branch, commit, and push: the pull request updates automatically.
printf '<p>Phone: 020 1234 5678</p>\n' >> contact.html
git commit -q -am "Add phone number as requested in review"
git pushOutput
To https://github.com/asha-kumar/my-website.git 4970bdd..c9360b0 contact-page -> contact-page
| Good review comment | Unhelpful comment |
|---|---|
| *Could this link say "Email Asha" instead of "Click here"? It helps screen-reader users.* | *Bad link.* |
| *Nice! Small thing: the heading should be h1, not h3.* | *Wrong.* |
Merge and tidy up
When the pull request is approved, click Merge pull request and Confirm merge on GitHub, then Delete branch (it is safe: the work is now in main). Back in the terminal, update your main and delete the local branch:
git switch main
git pull
git branch -d contact-page
git log --oneline --graph -4Output
Switched to branch 'main' Your branch is up to date with 'origin/main'. From https://github.com/asha-kumar/my-website 1ce7ed1..61ecf98 main -> origin/main Updating 1ce7ed1..61ecf98 Fast-forward contact.html | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 contact.html Deleted branch contact-page (was 4970bdd). * 61ecf98 (HEAD -> main, origin/main, origin/HEAD) Merge pull request #1 from asha-kumar/contact-page |\ | * 4970bdd (origin/contact-page) Add contact page |/ * 1ce7ed1 Add intro sentence * ec605b9 Add stylesheet
GitHub created a merge commit, *Merge pull request #1…*, just as git merge does locally. Your main now matches GitHub, and you are ready for the next branch.
Forks and issues: contributing to other projects
| Feature | What it is | Used for |
|---|---|---|
| Issue | A numbered to-do, bug report or idea on a repository | Planning work; a PR can close it by writing *Fixes #12* in its description |
| Fork | Your own copy of someone else's repository on GitHub | Proposing changes to a project you cannot push to: fork, clone your fork, branch, push, then open a pull request to the original |
Open-source projects (including the ones that power much of the web) accept contributions this way. Look for issues labelled good first issue.
Practice
Guided exercise
Push a feature branch
Asha has a new branch projects-section with one commit. Push it to GitHub so she can open a pull request, then list the remote branches with git branch -r.
- Make sure you are on the branch (
git branch). git push -u origin projects-sectiongit branch -rlists the branches Git knows on origin.
Show the solution
git push -q -u origin projects-section
git branch -rOutput
origin/main origin/projects-section
How to get there: After the push, GitHub has the branch, and origin/projects-section appears in git branch -r. On GitHub you would now click Compare & pull request.
Your turn
After the merge
Your contact-page pull request was merged on GitHub and the branch deleted there. Update main, delete the local branch, and clean up Git's list of remote branches with git fetch --prune.
Show the solution
git switch -q main
git pull -q
git branch -d contact-page
git fetch --prune
git branch -aOutput
Deleted branch contact-page (was 4970bdd). From https://github.com/asha-kumar/my-website - [deleted] (none) -> origin/contact-page * main remotes/origin/HEAD -> origin/main remotes/origin/main
How to get there: git fetch --prune removes origin/contact-page from Git's memory because the branch no longer exists on GitHub. git branch -a lists local and remote branches: only main is left.
Common mistakes
Committing straight to main on a team project
You see: Changes skip review, or your push is rejected by branch protection
Fix: Always create a branch first.
Opening a new pull request for every fix
You see: Several PRs for the same feature
Fix: Push new commits to the same branch; the PR updates itself.
Huge pull requests
You see: Slow, shallow reviews
Fix: Keep each PR to one focused change; small PRs get merged faster.
Forgetting to pull main after merging
You see: Your next branch starts from old code
Fix: git switch main and git pull before creating the next branch.
Recap
- Work on a branch, push it, and open a pull request to merge it into main.
- Reviewers comment on Files changed; push more commits to update the PR.
- Merge on GitHub, then switch to main, pull and delete the local branch.
- Issues track work; forks let you propose changes to projects you cannot push to.
Key terms
- Pull request
- A request on GitHub to review and merge one branch into another.
- Code review
- Teammates reading and commenting on changes before they are merged.
- Fork
- Your own GitHub copy of someone else's repository.
- Issue
- A numbered task, bug report or idea tracked on a GitHub repository.
Check your understanding
Answer all 4 questions, then check your answers to see an explanation for each one.
1. A reviewer asks for a change to your open pull request. What do you do?
2. Where on a pull request do you see the line-by-line changes?
3. When would you fork a repository?
4. What should you do after your pull request is merged on GitHub?
Mark this lesson complete
Finished this lesson?
When you have worked through the examples and exercises, mark it complete to save your progress.