Lesson 94 of 296
Branches and merging
What you will learn
- What a branch really is
- HEAD
- Creating and switching branches
- Merging: fast-forward and merge commits
- Stashing unfinished work
- Branch naming
On this page
What is a branch?
A branch is a separate line of work. You can build a feature on a branch without touching the main code. When it is finished and reviewed, you merge it back.
Technically, a branch is just a movable label that points to a commit. Creating a branch is instant and costs almost nothing, so use branches freely.
The main branch is usually called main (older projects use master). It should always contain working code.
HEAD
HEAD means "where you are now": usually it points to the branch you are on. When you commit, the branch HEAD points to moves forward to the new commit.
Creating and switching branches
| Command | Meaning |
|---|---|
git branch | List local branches; the current one has a * |
git switch -c feature/login | Create a new branch and switch to it |
git switch main | Switch to an existing branch |
git branch -d feature/login | Delete a branch that has been merged |
git branch -D feature/login | Force-delete a branch, even if not merged |
Older tutorials use git checkout -b name and git checkout name. They still work; switch is the newer, clearer command.
Before switching, commit or stash your changes. Git may refuse to switch if your uncommitted changes would be overwritten.
A typical feature flow
git switch main
git pull
git switch -c feature/password-reset
... edit, git add, git commit (several times) ...
git push -u origin feature/password-reset
... open a pull request, get it reviewed, merge it ...Lesson 95 covers pull, push and pull requests.
Merging
git merge brings the commits from another branch into your current branch.
git switch main
git merge feature/password-resetFast-forward merge
If main has not changed since you created the branch, Git simply moves the main label forward to the branch's last commit. No new commit is created.
Before: main
|
A - B - C <- feature
After: main, feature
|
A - B - CMerge commit (three-way merge)
If both branches have new commits, Git combines them and creates a merge commit with two parents.
Before: A - B - D <- main
\
C - E <- feature
After: A - B - D ----- M <- main
\ /
C - E ---If both branches changed the same lines in different ways, Git cannot decide and stops with a merge conflict (Lesson 96).
To always create a merge commit, even when a fast-forward is possible:
git merge --no-ff feature/password-resetThis keeps a visible record that a feature branch existed.
Stash: save unfinished work
You are halfway through something, and need to switch branches to fix an urgent bug:
git stash save your uncommitted changes and clean the working directory
git switch main
... fix the bug ...
git switch feature/login
git stash pop bring the changes back| Command | Meaning |
|---|---|
git stash | Save changes aside |
git stash -u | Also include new untracked files |
git stash list | See all stashes |
git stash pop | Re-apply the latest stash and remove it from the list |
git stash apply | Re-apply but keep it in the list |
Do not leave work in stashes for long; it is easy to forget. A small "work in progress" commit on your own branch is often better.
Branch naming
Common patterns:
feature/password-reset
fix/vat-rounding
chore/update-deps
SHOP-142-basket-discounts (with a ticket number from Jira or Linear)Lowercase, hyphens, and a clear description.
Branching strategies
- Trunk-based development: short-lived branches (hours to a couple of days), merged into
mainoften. Popular with teams that deploy frequently. Unfinished features are hidden with feature flags. - GitHub flow:
mainis always deployable; every change goes through a branch and a pull request. - Git flow: separate
develop,releaseandhotfixbranches. More complex; used by teams with scheduled releases.
Short-lived branches mean fewer and smaller conflicts.
Common mistakes
- Working directly on
main. - Long-lived branches that drift far from
mainand become painful to merge. - Forgetting to pull
mainbefore creating a branch. - Switching branches with uncommitted changes and getting confused.
How to say it in an interview
"A branch is a lightweight pointer to a commit, and HEAD points to the branch I am on. I create a short-lived branch for each change from an up-to-date main, commit as I go, then open a pull request. When merging, Git fast-forwards if main has not moved, otherwise it creates a merge commit with two parents, and stops with a conflict if the same lines changed. I use stash to set work aside temporarily, and prefer short-lived branches to keep merges small."
Key words
| Word | Meaning |
|---|---|
| Branch | A movable pointer to a commit; a separate line of work |
| main | The primary branch |
| HEAD | Where you are now |
| Merge | Combine another branch's commits into the current one |
| Fast-forward | A merge that just moves the branch pointer forward |
| Merge commit | A commit with two parents that joins branches |
| Stash | Temporarily saved uncommitted changes |
| Feature flag | A switch that hides unfinished features in deployed code |
Now prove it
This lesson is complete when you pass the quiz and all three practice levels.