Sub topic 1
Branches and commits: the unit of change
Prerequisites
This chapter assumes you can run basic Git commands (git add, git commit, git push) and have a GitHub account with a repository you can push to. You should be comfortable in a terminal and a text editor. Nothing else is required.
Why start here
A pull request does not contain files. It contains a comparison between two branches, made of the commits that one branch has and the other lacks. Before you open one, you need a branch with commits that are worth reviewing.
The working model
A commit is a snapshot of your project plus a message and a pointer to its parent commit. A branch is a movable name that points to one commit. When you commit while on a branch, the name moves forward to the new commit.
Reviewers see your work as a list of commits and a combined diff. So the shape of your branch determines how easy your pull request is to review.
Here main and fix-login-timeout both start from commit B. The pull request for fix-login-timeout would show commits C and D. Commit E belongs to main only.
Creating a branch and committing
Create a branch for each change, give it a short descriptive name, and push it to GitHub.
git switch -c fix-login-timeout
# edit files, then:
git add .
git commit -m "Increase login request timeout to 30s"
git push -u origin fix-login-timeoutgit switch -c creates the branch and moves you onto it. The -u flag on git push links your local branch to the remote one, so later git push and git pull commands need no arguments. After the push, GitHub usually shows a banner offering to open a pull request from the branch.
What makes a good unit of change
- One purpose per branch. A branch that fixes a bug and also renames a module is hard to review and hard to revert.
- Small commits with clear messages. Write the message in the imperative, as in "Increase login request timeout to 30s". A reviewer should understand each commit without opening the code.
- Branch from an up-to-date base. Run
git switch mainandgit pullbefore creating your branch. This reduces merge conflicts later. - Never commit directly to
mainin a shared repository. Branch protection often blocks it, and it skips review.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| GitHub shows no pull request banner | The branch was never pushed | Run `git push -u origin <branch>` |
| Pull request lists unrelated commits | The branch was created from an outdated or wrong base | Rebase onto `main`, or recreate the branch from a fresh `main` |
| Commit appears on `main` instead of your branch | You committed before switching | Create a branch at that commit with `git switch -c <name>`, then reset `main` to match `origin/main` |
Exercise
A teammate's pull request shows 14 commits, but they only meant to change one file. What is the most likely reason, and what should they check first?
Show answer
The branch was probably created from an outdated or different base, so it contains commits that main does not have. They should compare the branch with main (for example with git log main..HEAD), then rebase onto the current main or recreate the branch from it.