Pull requests, reviews and short-lived branches
Deliver a change to a protected `main` through a pull request: push a short-lived branch, pass review and required checks, merge, and tag the release.
Contentsand lesson path
On this page
- Why it matters
- Remotes: your copy and the shared copy
- The life of a pull request
- Protecting main
- Short-lived branches and trunk-based development
- Tagging a release
- Check your understanding
- Apply it
- Review prompt
Lessons of Git Workflow
- Git's model: snapshots, commits and pointers Concept · 15 min
- Commit hygiene: staging hunks and writing useful messages Worked example · 15 min
- Branching, merging and resolving a real conflict Lab · 25 min
- Pull requests, reviews and short-lived branches Concept · 20 min
- Recovering from mistakes without breaking teammates Lab · 20 min
- Check: repository-state challenges Knowledge check · 8 questions
What you will learn
- Deliver a change through a pull request on a protected main branch, including review, required checks and a tagged release.
01. Why it matters
On a team, main is the branch everyone builds on and, often, the one that gets deployed. If anyone can push anything to it, one untested change can block the whole team or break production. A pull request (PR; GitLab calls it a merge request) puts a gate in front of main: your branch is proposed, discussed, tested and approved before it is merged.
This lesson follows one small change from your laptop to a tagged release on GitHub. The ideas carry over to GitLab, Bitbucket or Azure DevOps.
02. Remotes: your copy and the shared copy
Every clone is a complete repository. The shared one, usually on GitHub, is just another repository that your clone knows as a remote, conventionally named origin. Git only talks to it when you ask:
git fetchdownloads new commits and updates remote-tracking branches such asorigin/main, your local record of where the remote's branches were at the last fetchgit pullrunsgit fetch, then brings the upstream branch into your current branch: a fast-forward when you have no commits of your own; otherwise Git asks you to choose between merging (--no-rebase) and rebasing (--rebase), or to setpull.rebaseonce in your configgit pushuploads your commits and moves the matching branch on the remote
In the sessions below, origin is a local practice repository standing in for GitHub, so its path is shortened to .... When you push a new branch to GitHub, the output also includes a remote: message with a link for opening a pull request.
$ git switch -c add-rate-limit
Switched to a new branch 'add-rate-limit'
$ echo 'limit: 100 requests per minute' > api.conf
$ git commit -qam 'Add a per-client rate limit'
$ git push -u origin add-rate-limit
To .../pr-demo/origin.git
* [new branch] add-rate-limit -> add-rate-limit
branch 'add-rate-limit' set up to track 'origin/add-rate-limit'.
$ git branch -vv
* add-rate-limit b9dc832 [origin/add-rate-limit] Add a per-client rate limit
main 5464a17 [origin/main] Add API config-u (short for --set-upstream) links your branch to origin/add-rate-limit. From then on a plain git push or git pull knows where to go, and git status can tell you whether you are ahead of or behind the remote. git branch -vv shows each branch's upstream in square brackets.
03. The life of a pull request
A typical flow on GitHub:
- Start from an up-to-date
main:git switch main,git pull, thengit switch -c <short-descriptive-name>. - Make small, focused commits and push the branch.
- Open the pull request against
main. The title says what the change does; the description says why, how you tested it and what is risky, and links the issue ("Closes #87" closes it when the PR is merged). Open it as a draft for early feedback. - Automated checks run. A CI workflow builds the code, runs tests and linters, and reports a status on the pull request.
- Review. Reviewers read the diff, leave comments on specific lines, and submit a review that comments, approves or requests changes.
- Address feedback by committing on the same branch and pushing again. A pull request follows its branch, so the new commits appear in it automatically and the checks run again. Reply to each comment, then re-request review.
- Merge once the approvals and checks are in place, and delete the branch. GitHub offers three methods: a merge commit, squash and merge (the PR's commits become one commit on
main) and rebase and merge (the commits are replayed ontomainwith no merge commit). Teams usually pick one and stick to it. - Update your clone: switch to
mainandgit pullto get the merged result.
$ echo '# Raise only after load testing' >> api.conf
$ git commit -qam 'Explain when the limit may be raised'
$ git push
To .../pr-demo/origin.git
b9dc832..a056cf6 add-rate-limit -> add-rate-limit
$ git switch main
Switched to branch 'main'
Your branch is up to date with 'origin/main'.
$ git pull
From .../pr-demo/origin
5464a17..2cba632 main -> origin/main
Updating 5464a17..2cba632
Fast-forward
api.conf | 3 ++-
1 file changed, 2 insertions(+), 1 deletion(-)
$ git branch -d add-rate-limit
Deleted branch add-rate-limit (was a056cf6).Notice that git switch main reported the branch as up to date even though the pull request had already been merged. That message compares main with origin/main as of your last fetch; only git pull, which fetches first, discovered the merge. When in doubt, fetch.
Review works best as a conversation about the change rather than a hurdle to get past:
- As the author, keep the pull request small enough to review in one sitting, read your own diff on GitHub before asking anyone else, and say in the description what kind of feedback you want.
- As a reviewer, check behaviour first (does it do what the description says, is it tested, what could break), then readability. Be specific, explain your reasoning, and label optional suggestions as such; many teams prefix them with "nit:".
- For both, answer every comment, even with a short "done", and take long debates to a call.
04. Protecting main
The flow above is only a convention until the hosting platform enforces it. On GitHub you protect main with a branch protection rule or a ruleset in the repository settings. The settings teams reach for first:
- Require a pull request before merging, with a number of required approvals (one is a common start). Direct pushes to
mainare then rejected, and because authors can never approve their own pull requests, the approval has to come from someone else. - Dismiss stale approvals when new commits are pushed, so an approval always covers the code that is actually merged.
- Require review from code owners: a
CODEOWNERSfile maps paths to the people or teams who must approve changes to them. - Require status checks to pass: name the CI jobs that must succeed. You can also require the branch to be up to date with
main, so the checks ran against what will really be merged. - Require conversation resolution, so review comments cannot simply be ignored.
- Block force pushes and deletions of
main.
Apply the rules to administrators too (GitHub calls this "Do not allow bypassing the above settings"), or someone in a hurry will skip them. Then everyone can trust that whatever is on main was reviewed and passed the checks, which is what makes frequent merging safe.
A required check needs a check to require. On GitHub that is usually a job in a GitHub Actions workflow, a YAML file under .github/workflows/. This minimal workflow runs on every pull request and every push to main, and its job id, test, becomes the name of the check. The checkout action is pinned to a full commit SHA, with its tag in a comment, because a tag can later be moved to other code:
name: ci
on:
pull_request:
push:
branches: [main]
permissions:
contents: read
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
- run: sh run-tests.sh # replace with your own test or lint command#!/bin/sh
# Fails when api.conf no longer sets a limit; replace it with your real tests.
grep -q '^limit:' api.confPush both files to main before you protect the branch. GitHub offers only checks it has seen: its documentation says a required status check must have completed successfully in the repository during the past seven days. So open one pull request, let test run, then add test under Require status checks to pass. From then on, a pull request whose test job fails cannot be merged; try it with a branch that deletes the limit: line.
Keep job names unique across workflows, since duplicates make the required check ambiguous, and put no paths or branches filter on the pull_request trigger of a required workflow: a skipped workflow leaves its check pending, which blocks the merge. The continuous integration lab builds a full pipeline.
05. Short-lived branches and trunk-based development
How long should a branch live? The longer it lives, the further it drifts from main: more conflicts, bigger reviews and a late surprise when everything finally meets. Trunk-based development answers with a simple rule: everyone integrates into one shared branch (the trunk, here main) at least once a day. Teams that use pull requests do this with short-lived feature branches that last hours, or a day or two at most, each carrying one small change.
Two techniques make that work for features that take longer than a day:
- Feature flags: merge the code early but switched off, and turn it on when the whole feature is ready. Unfinished work lives on
mainwithout reaching users. - Slicing: split a feature into steps that are each safe to ship, such as adding a database column, then the code that writes it, then the screen that reads it.
Some teams keep long-lived develop and release branches instead. That can suit software shipped in infrequent versions, but every merge between them is bigger and riskier.
07. Check your understanding
08. Apply it
This is the core of the skill's practice project. Use a public, secret-free repository on GitHub, your own or one shared with a study partner: GitHub Free protects branches only in public repositories.
- Add
ci.ymlandrun-tests.sh(or your real test command), push them tomain, and open one pull request so thetestcheck runs. Then protectmain: require a pull request with one approval and theteststatus check. - Create a short-lived branch, make two or three focused commits, and open a pull request whose description covers what, why and how you tested it.
- Ask your partner to review and leave at least one comment. Address it with a new commit on the same branch and push.
- Merge once approved and green, delete the branch, pull
main, tag the merge commit asv0.1.0with an annotated tag, push the tag and create a GitHub release from it.
Working alone? GitHub does not let you approve your own pull request, so set the required approvals to zero (keep the status check) and review your own diff carefully before merging.
09. Review prompt
Without looking back, list the steps from git switch -c to a published release, naming the command or GitHub action for each. Then explain to a teammate which branch protection settings you would switch on first for a new service, and why. Finally, explain why a new CI job cannot be made a required check until it has run.
References
- GitHub Docs: Pull requests
- GitHub Docs: About protected branches
- GitHub Docs: About pull request reviews
- GitHub Docs: About pull request merges
- GitHub Docs: About releases
- GitHub Docs: Troubleshooting required status checks
- GitHub Docs: Quickstart for GitHub Actions
- GitHub Docs: Secure use reference
- Trunk Based Development
- Trunk Based Development: Short-lived feature branches
- Trunk Based Development: Feature flags
- Pro Git (Scott Chacon, Ben Straub)
- Pro Git: Remote Branches
- Pro Git: Tagging
Your progress
Reading state
Sign in to save that you finished reading this lesson.
Prove this skill
8 questions · about 6 minutes · scored automatically. Marking a lesson as read never demonstrates a skill.