Sub topic 10
GitHub Flow
This chapter assumes you can branch, commit, push, and merge in Git. It also assumes you have used pull requests on a host such as GitHub, GitLab, or Bitbucket. GitHub Flow is a lightweight workflow built around one long-lived branch and many short-lived ones.
The rules
GitHub Flow has few rules:
mainis always deployable.- To start work, branch from
mainwith a descriptive name. - Commit on that branch and push it often.
- Open a pull request when you want feedback. You can open it early as a draft.
- Merge only after review and passing checks.
- Deploy after merging, ideally automatically.
There is no develop branch and no release branch. A merged pull request is meant to be releasable.
A worked cycle
The first part is local work on a feature branch:
git switch main
git pull
git switch -c feature/login-form
# edit files
git add .
git commit -m "Add login form"
git push -u origin feature/login-formThen open a pull request from feature/login-form into main. Reviewers comment, and you push more commits to the same branch. The pull request updates itself.
If main moves while you work, bring your branch up to date. You can merge main into it, or rebase it:
git fetch origin
git rebase origin/main
git push --force-with-leaseRebase only a branch that you alone use. --force-with-lease refuses to overwrite commits you have not fetched.
After approval, merge from the pull request page. Most teams choose squash or merge commit there, depending on the history they want. Then delete the branch. The host can do this automatically.
What makes it work
GitHub Flow depends on three supports:
- Short-lived branches. A branch that lives a few days has few conflicts and is easy to review.
- Automated checks. Tests and linters run on each pull request, so the reviewer does not have to find basic breakage.
- Fast deployment. Merging to
maintriggers a deploy, somainand production stay close.
Branch protection enforces these rules. Require a pull request, at least one approval, and passing status checks before merge.
Common errors
| Problem | Cause | Fix |
|---|---|---|
| Large, slow reviews | The branch grew for weeks | Split the work into smaller pull requests |
| Broken `main` | Merged without checks, or checks were stale | Require status checks and an up-to-date branch |
| Unfinished work blocks merging | The feature is too big for one pull request | Hide it behind a feature flag and merge in slices |
| Lost commits after a push | Plain `--force` after a rebase | Use `--force-with-lease` |
Where it fits
GitHub Flow suits web services and apps that deploy continuously, usually with a small or mid-sized team. It is simpler than Gitflow. It is also close to trunk-based development, but branches may live a little longer and review through pull requests is expected.
Exercise
Your pull request has been open for a week, main has changed, and the pull request shows conflicts. Name the steps you take to fix it under GitHub Flow, and one habit that would prevent it.
Show answer
Fetch, then rebase onto origin/main (or merge origin/main into the branch), resolve the conflicts, run the tests, and push with git push --force-with-lease if you rebased. To prevent it, keep branches short-lived and open smaller pull requests so they merge within a day or two.