Nishaglobal Education logo

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.

Lesson 10 of 1140 minQuiz: 4 questions

🎯 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
  1. What you'll learn and why it matters
  2. The GitHub flow
  3. Push your branch
  4. Open a pull request
  5. Reviewing and updating a pull request
  6. Merge and tidy up
  7. Forks and issues: contributing to other projects
  8. Practice
  9. Common mistakes
  10. Recap
  11. Quiz
  12. Mark lesson complete

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

1. Create a branch2. Commit your work3. Push and open a pull request4. Review and discuss5. Merge, then pull mainThen start the next branch.
Five steps in a loop: create a branch, commit your work, push the branch and open a pull request, review and discuss, then merge into main and pull the updated main. Then start the next branch.
StepWhereCommand or action
1. Start from the latest mainTerminalgit switch main then git pull
2. Create a branchTerminalgit switch -c contact-page
3. Commit your workTerminalgit add … and git commit -m …
4. Push the branchTerminalgit push -u origin contact-page
5. Open a pull requestGitHubCompare & pull request button
6. Review and fixGitHub + terminalComments; push more commits to the same branch
7. MergeGitHubMerge pull request
8. Update and tidyTerminalgit 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:

Terminal
git push -u origin contact-page

Output

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

  1. 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-page to merge into main).
  2. Write a clear title, such as *Add contact page*.
  3. 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.*
  4. On the right, you can request Reviewers (teammates) and link an Issue it fixes.
  5. 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.

Terminal
printf '<p>Phone: 020 1234 5678</p>\n' >> contact.html
git commit -q -am "Add phone number as requested in review"
git push

Output

To https://github.com/asha-kumar/my-website.git
   4970bdd..c9360b0  contact-page -> contact-page
Good review commentUnhelpful 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:

Terminal
git switch main
git pull
git branch -d contact-page
git log --oneline --graph -4

Output

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

FeatureWhat it isUsed for
IssueA numbered to-do, bug report or idea on a repositoryPlanning work; a PR can close it by writing *Fixes #12* in its description
ForkYour own copy of someone else's repository on GitHubProposing 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.

  1. Make sure you are on the branch (git branch).
  2. git push -u origin projects-section
  3. git branch -r lists the branches Git knows on origin.
Show the solution
Solution
git push -q -u origin projects-section
git branch -r

Output

  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
Solution
git switch -q main
git pull -q
git branch -d contact-page
git fetch --prune
git branch -a

Output

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. 1. A reviewer asks for a change to your open pull request. What do you do?

  2. 2. Where on a pull request do you see the line-by-line changes?

  3. 3. When would you fork a repository?

  4. 4. What should you do after your pull request is merged on GitHub?

Answer all 4 questions to check.

Mark this lesson complete

Finished this lesson?

When you have worked through the examples and exercises, mark it complete to save your progress.