Course contents · Lesson 10 of 17

Branches and Pull Requests: How Teams Change Code

Git and Collaboration · Lesson 10 of 17 · 11 min Engineer

You are halfway through a new contact form for your team's shop website when a teammate must ship an urgent price fix today. If your unfinished form were already in main, the shared code the team releases from, the fix would ship it too. Teams avoid this with branches and pull requests: each change grows on its own line of work, is checked by tests and a teammate, and only then joins main.

Branches: a cheap, separate line of work

As What Is Git? showed, a branch is a movable name that points to a commit, and HEAD marks the branch you are on. Creating a branch only adds a new name at your current commit, so it is instant and copies no files.

To create a branch and move onto it in one step, run git switch -c with a name. -c means create, and the new branch starts at the commit you are on. Name it after the work; this lesson uses feature-x:

git switch -c feature-x

Git confirms on one line:

Switched to a new branch 'feature-x'

If the name is taken, Git stops with fatal: A branch named 'feature-x' already exists. To move onto an existing branch, leave out the -c. Older guides write git checkout -b feature-x, which does the same.

Each new commit now moves feature-x forward while main stays put. In the diagram, X and Y exist only on the branch, while C, a teammate's fix, lands on main. M is a merge commit, a commit with two parents, which joins the lines when the pull request is merged.

Commits drawn as circles. The main line runs A, B, C and M. At B, the command git switch -c feature-x starts a branch that drops to a lower line with two commits, X, add form, and Y, fix review note, with the feature-x name tag on Y. Meanwhile main gets C, a teammate's fix. The branch rejoins main at M, a merge commit whose parents are C and Y, and the main name tag sits on M. A note says X and Y stay off main until the pull request is merged
Your commits stay off main until the merge commit M brings them in.

Pull requests: asking to merge your branch

First, share the branch. Push it with -u, which, as in Git Basics: Commit, Push and Pull, links your branch to its copy on origin so that later a plain git push knows where to go:

git push -u origin feature-x

Among the progress lines, [new branch] shows the branch arriving on origin, and GitHub adds a link for opening a pull request (trimmed, example address):

remote: Create a pull request for 'feature-x' on GitHub by visiting:
remote:      https://github.com/asha/shop/pull/new/feature-x
To https://github.com/asha/shop.git
 * [new branch]      feature-x -> feature-x

If Git answers src refspec feature-x does not match any, the name is misspelt or that branch does not exist on your laptop.

A pull request, or PR, is a page on GitHub that asks to merge one branch, the head, into another, the base, usually main. GitLab calls its version a merge request. On GitHub, click Compare & pull request in the banner after your push, check that base is main, add a title and description, and click Create pull request.

The PR page's tabs include Conversation for the description and discussion, Commits, Checks and Files changed, which shows the diff: the lines your branch would change in the base, removed lines in red and added lines in green.

Checks are programs the project runs automatically on each push to the PR, such as the tests and a linter, a tool that flags style problems and likely mistakes. Setting them up is the job of CI, explained in What Is CI/CD?. The mockup labels the five parts a reviewer looks at.

Simplified mockup of a pull request page in a browser at github.com/asha/shop/pull/7. Callout 1, the title: Add contact form, number 7, Open, asha wants to merge feature-x into main. Below it the tabs Conversation, Commits, Checks and Files changed. Callout 2, the description: customers asked for a way to message us, adds a form to the contact page, Closes #42. Callout 3, the diff of contact.html: one removed line in red and two added lines in green. Callout 4, checks and review: tests passed, lint passed, ravi approved these changes. Callout 5, a green Merge pull request button
One page holds the why, the exact lines changed and the test results, so review happens in one place.

What makes a good pull request

Keep it small: one change that a reviewer can read carefully in one sitting. A PR that mixes a new form, a tidy-up and a library upgrade is hard to review and hard to undo, so split it.

Give it a title that says what changes, such as "Add contact form", not "Updates". In the description, say why you made the change and how you tested it, so the reviewer checks your reasoning instead of guessing it.

Link the issue it solves. An issue is a numbered task or bug report kept in the same project, such as #42. On GitHub, a keyword such as Closes, Fixes or Resolves before the number links the two, and merging the PR closes the issue automatically. A description in this shape works:

Customers asked for a way to message us.
Adds a form to the contact page.

Tested: sent a message locally and saw it saved.

Closes #42

The keyword works only in PRs into the default branch, usually main.

Review, update and merge

Reviewers read the diff and comment on specific lines. Each then submits a review as Comment (general feedback), Approve (ready to merge) or Request changes (fix this first). Code Review Basics covers giving and receiving feedback.

To answer a review, you do not open a new PR. You change the files on the same branch, commit and push, and the PR shows the new commits while the checks run again:

git add contact.html
git commit -m "Check the email address before sending"
git push

Thanks to -u, the push goes to feature-x on origin and ends with a line like 9e1d0c4..b7c2e15 feature-x -> feature-x (example hashes). In the flowchart, the dashed loop is this step. If it is rejected because someone else pushed to your branch, pull first, then push: the rejected-push fix from Git Basics: Commit, Push and Pull, with its pull.rebase false setting. That pull adds a merge commit to feature-x; many teams rebase instead, as Git Merge vs Rebase shows.

Flowchart in seven steps. 1, create branch, git switch -c; 2, commit, on your branch; 3, push, git push; 4, open PR, title and description; 5, checks and review, tests and teammates. Then a decision, changes requested? Yes: a dashed arrow labelled fix, commit, push loops back to step 2. Approved: an arrow leads to 6, merge into main, then 7, delete branch, its work is in main. A banner says every new push updates the same pull request
Review is a loop on one branch: every fix is another push to the same pull request.

Once the PR is approved and its checks pass, someone allowed to merge clicks Merge pull request, then Confirm merge. That creates a merge commit like M; the menu's squash and rebase options are compared in Git Merge vs Rebase. Conflicts with main must be resolved first, as a later lesson shows.

Many teams add a branch protection rule, a repository setting that can require approving reviews and passing checks before anything merges into main.

Keep branches short-lived and delete them after merge

A branch that lives for weeks drifts away from main: other merges pile up, conflicts grow, and the final PR is too big to review well. Aim for branches that last days, not weeks.

Once merged, the branch's commits are part of main, so its name is clutter. Delete it from the merged PR page, or turn on Automatically delete head branches under the repository's Settings, General, Pull Requests.

Then tidy your laptop: switch to main, pull the merged work and delete your copy of the branch. -d is the safe delete, which refuses to remove unmerged work:

git switch main
git pull
git branch -d feature-x

The last line confirms, with the commit the name pointed to (example hash):

Deleted branch feature-x (was b7c2e15).

If Git refuses because the branch "is not fully merged", it holds commits Git cannot find in main or in the branch's upstream. Usually you skipped git pull, or your team squash-merges, adding each PR to main as one new commit. If the PR says Merged, git branch -D forces the delete.

Check yourself

You are halfway through feature-x when a teammate's urgent fix is merged and released. Does your unfinished work ship with it?

No. The fix reached main through its own pull request, while your commits sit only on feature-x, which main gets only when your PR is merged.

A reviewer requests changes. Why push to the same branch instead of opening a new PR?

The PR follows its branch, so a push updates it: the discussion stays together, the reviewer sees what changed, and the checks run again.

After the merge, you delete feature-x. Do its commits disappear from main?

No. A branch is only a name pointing at a commit, and the merge made those commits part of main's history, so deleting the name removes nothing main needs.