Sub topic 6
Create a Feature Branch
This chapter assumes you have already forked the repository, cloned your fork, and set up the origin and upstream remotes. You should also know the basic commands add, commit, status, and log.
Why you work on a branch
A branch is a movable pointer to a line of commits. Your fork's main branch should mirror the project's main. If you commit your changes directly to main, two problems follow:
- Your pull request is tied to
main, so any later commit you make there also lands in that pull request. - Syncing with upstream becomes harder, because your
mainhas commits that upstream does not have. A fast-forward merge then fails.
A feature branch avoids both problems. Each pull request gets its own branch, and main stays a clean copy of upstream.
Start from an up-to-date main
Before you branch, check which branch you are on and that it is current. A branch starts from whatever commit you have checked out, so an old main gives you an old starting point.
git switch main
git fetch upstream
git merge --ff-only upstream/mainIf the project's default branch has a different name, such as master or develop, use that name instead of main. The later chapter on syncing covers this step in more detail.
Create the branch
Use git switch -c to create a branch and move onto it in one step.
git switch -c fix-readme-typoConfirm where you are:
git status
git branchgit status prints On branch fix-readme-typo. git branch lists your local branches and marks the current one with *.
The older command git checkout -b fix-readme-typo does the same thing. Both work, but git switch is clearer because it only changes branches.
Choose a good branch name
The name is only a label, but a good one helps you and the maintainers. Follow these habits:
- Describe the change, not the date or your name:
fix-readme-typo, notmy-changes. - Use lowercase words separated by hyphens. Avoid spaces.
- Check CONTRIBUTING for a naming convention. Some projects want prefixes such as
fix/orfeature/, or an issue number such asissue-142-null-check. - Keep one topic per branch. If you want to fix a typo and add a feature, make two branches.
Common errors
| Symptom | Cause | Fix |
|---|---|---|
| `fatal: a branch named 'x' already exists` | You reused a name from an earlier attempt. | Pick a new name, or switch to the existing one with `git switch x`. |
| You edited files and then realized you are on `main`. | You made changes before creating a branch. | Run `git switch -c new-name`. Uncommitted changes move with you to the new branch. |
| Your branch contains unrelated commits. | You branched from a stale or wrong starting point. | Check with `git log --oneline`. Create a new branch from an updated `main`. |
| `error: Your local changes would be overwritten` when switching | Uncommitted edits conflict with the target branch. | Commit the edits, or set them aside with `git stash`. |
Try it
- Make sure your fork's
mainis current. - Run
git switch -cwith a descriptive name for the change you plan to make. - Run
git statusand confirm the branch name. - Run
git log --oneline -3and check that the latest commit matches upstream'smain.
Exercise
You are on main in your clone and have edited README.md but not committed. Which command moves the edit onto a new branch called fix-readme-typo without losing it, and what does main look like afterward?
Show answer
Run git switch -c fix-readme-typo. Git creates the branch from the current commit and moves your uncommitted edit with you. main is unchanged because the edit was never committed there.
Exercise
Why should you update main from upstream before creating a feature branch?
Show answer
A new branch starts from the commit you have checked out. If main is out of date, your branch begins from old code, which makes conflicts with upstream more likely when you open the pull request.