Linux and Git commands · Lesson 21 of 23
Team workflow: pull requests, trunk-based versus Git Flow, and good commit messages
How a change travels from your laptop to production on a team, the two most common branching strategies, and how to write commit messages your teammates will thank you for.
Git gives you branches, merges and tags. A team workflow is the agreement about how to use them: where work starts, how it is reviewed, and how it reaches production. This lesson covers the pull request, two popular branching strategies, and the commit message habits that make any workflow work better.
Why this matters#
When you join a team, you will not choose the workflow, but you will be expected to follow it and to explain it. A review is where your code is judged, and a commit message is the only note your future teammates (and future you, at 2 a.m. during an incident) will have about why a line exists. Small habits here pay off for years.
Pull requests are a feature of hosting services, not of Git itself, so we could not run one in the practice repositories. The branch, merge and tag commands below were run for real. The hosting-service steps are described from how they commonly work.
The pull request flow#
A pull request (PR, called a merge request on some services) is a proposal: "please merge my branch into main". It gives your team a place to review the changes, discuss them and run automatic checks before anything reaches the shared branch. The usual steps are:
- Update your local
main:git switch mainthengit pull --ff-only. - Create a branch for one piece of work:
git switch -c fix/expired-login. - Commit in small steps, then publish the branch:
git push -u origin fix/expired-login. - Open a pull request on the hosting service. Write what changed and why.
- Automatic checks (tests, linting, a build) run on the branch. Fix failures by pushing more commits.
- Teammates review and comment. You reply and push changes. When they approve and the checks pass, the branch is merged.
- Delete the branch, switch back to
mainand pull.
Keeping PRs small is the most useful rule. A PR of 50 changed lines gets read properly. A PR of 2,000 lines gets a quick "looks good" and hides bugs. If a feature is big, split it into several PRs that each leave the application working.
When reviewing someone else's PR, read for what the change does and why, look for missing tests and unclear names, and ask questions rather than issuing orders. When your own PR is reviewed, a comment is about the code, not about you.
Three ways to merge a PR#
Hosting services usually offer three buttons, and your team will pick one.
- Merge commit keeps every commit and adds a merge commit, as in lesson 15.
- Rebase and merge replays your commits onto
main, giving a straight history (lesson 18). - Squash and merge combines all the PR's commits into a single commit on
main. Your noisy "wip" commits disappear. We simulated it withgit merge --squash, which left one commit:
* 94a99f9 Add search endpoint
* 8d76c6f Add b
* c42511d wipThree "wip" commits on the branch became one clean "Add search endpoint". Squashing keeps main tidy, but you lose the step-by-step history, and a big PR becomes one big commit that is harder to bisect.
Two branching strategies#
A branching strategy says which long-lived branches exist and how work moves between them. These two are the ones you will meet most.
Trunk-based development#
Everyone works from one main branch, the "trunk", which is always kept in a deployable state. Branches are short-lived: hours or a day or two, then merged back. Work that is not ready for users is hidden behind a feature flag, a setting that keeps the code switched off until you turn it on. Releases are tagged from the trunk.
We simulated it: two small branches, each merged straight back and deleted, then a tag.
d15e65e (HEAD -> main, tag: v1.1.0) Add flag, off by default
4accc90 Fix typo in README
94a99f9 Add search endpointThe history is a line. It relies on good automatic tests and fast reviews, because every merge is a potential release.
Git Flow#
Git Flow uses several long-lived branches with fixed roles: main holds released code and is tagged; develop collects finished features; feature/* branches are made from develop; release/* branches are for stabilising a release; hotfix/* branches fix production and are merged to both main and develop.
We ran one feature, one release and one hotfix through it:
* ea9d15f (HEAD -> develop) Merge main into develop
|\
| * c7c52fc (tag: v1.2.1, main) Merge hotfix/1.2.1
| |\
| | * 7ea5a8d (hotfix/1.2.1) Hotfix 1.2.1
| |/
| * 8d2c2bd (tag: v1.2.0) Release 1.2
| |\
| | * 1c9646a (release/1.2) Bump version 1.2
| |/
|/|
* | 2fd4a85 Merge feature/a
|\ \
| |/
|/|
| * 92bddb6 (feature/a) Feature A(We cut the graph off at its first lines.) You can see how many merges and branches even a tiny release creates. It gives a clear place for everything, which suits software that ships in numbered versions, such as a mobile app, a library or a product with several supported versions.
The trade-off: Which branching strategy should your team use for a web API that you deploy several times a week?
- Trunk-based development: Small merges, little branch overhead, fast feedback and an always-releasable main. It needs reliable automated tests, quick reviews and feature flags, so unfinished work does not reach users. A weak test suite makes it risky.
- Git Flow: Clear roles for every branch and a safe place to stabilise releases and support old versions. The cost is many long-lived branches, big merges, late integration and extra ceremony, which slows a team that deploys continuously.
For a service that you deploy continuously, such as the API in this course, trunk-based development with short branches and PRs is the common choice. A team that ships boxed versions to customers may still prefer Git Flow or a lighter variant. The best strategy is the one the team understands and follows.
Good commit messages#
A commit message has a subject line, a blank line and an optional body. We committed one with git commit -m "subject" -m "body":
Reject login with expired password
Expired accounts were still allowed to log in because the check
compared the expiry date as a string. Parse it as a date and add a test
for the boundary day.
Fixes #42The subject was 34 characters long. These habits make messages useful:
- Subject: keep it to about 50 characters, start with a capital letter, skip the full stop, and write in the imperative, as if giving a command: "Fix", "Add", "Remove", not "Fixed" or "Fixing". A good test is that it completes "If applied, this commit will ... ".
- Blank line between subject and body, because many tools treat the first line as the title.
- Body: explain why, not what. The diff already shows what changed. Say what problem it solves, why you chose this approach and anything surprising. Wrap lines at about 72 characters.
- References: link the issue (
Fixes #42), so the tracker and the code stay connected. - One logical change per commit, so a message can be honest and short.
Compare what shows up in git log --oneline for careless messages:
c42511d wip
ec8f708 fixed stuff
85e4e1c Reject login with expired passwordOnly the last line tells you anything. When you are hunting a bug six months later with git log -S or git blame, the first two are useless.
Some teams add a prefix style such as fix:, feat: or docs: (the Conventional Commits format) so tools can build changelogs and version numbers. Follow whatever your team uses. Trailers at the end can credit others, for example Co-authored-by: Dev Two <dev2@example.com> when you pair on a change.
You can search messages from the command line:
git log --oneline --grep='login'85e4e1c Reject login with expired passwordTry it#
- Write your usual commit message for the last change you made. Rewrite it with a 50-character imperative subject and a body that explains why. Commit it with two
-mflags and view it withgit log -1. - Practise the PR steps on a repository you own: branch, commit, push with
-u, and open a pull request on your hosting service. Review your own PR and merge it with "squash and merge". - Simulate trunk-based flow locally: make two tiny branches, merge each with
--ff-only, delete them, and tag the result. Look atgit log --oneline --decorate. - Simulate Git Flow: create
develop, a feature branch, a release branch and a hotfix branch, merging with--no-ff. Compare the graph with the trunk-based one. - Find three bad commit messages in a repository (yours or an open-source project) and rewrite each as a good one.
Sign in to keep track of the lessons you finish.