Branching & merging
Use feature branches and understand fast-forward vs merge commits.
By Invicly Tech, , in Git, GitHub & GitHub Actions. About 10 minutes to read.
In short
Feature branches isolate work until it is ready. How you merge them decides what history looks like: a straight line, a visible merge commit or a single squashed commit.
Explained simply
A branch is a separate line of work. You create one, make commits on it, and main stays untouched until you decide to bring the work back. Because a branch is only a pointer to a commit, making one is instant, even in a huge project. Teams create a new branch for every feature or fix.
Merging brings the work back. Git looks at three commits: the tip of main, the tip of your branch, and the last commit they shared (called the merge base). It works out what each side changed since that shared point and combines the two sets of changes.
If main has not moved since you branched, there is nothing to combine. Git just slides the main pointer forward to your latest commit. That is a fast-forward. If both sides have new commits, Git creates a merge commit with two parents, which ties the two lines of history together.
Branches pile up quickly. After a branch is merged, delete it locally and on the server. Your copy of the server's branch list also goes stale, so fetch with prune now and then. A tidy branch list makes it obvious what work is still open.
Press Play on a diagram to walk through it one step at a time.
- Main had no new commits
- Pointer just moves forward
- No merge commit
- Linear history
- Branches diverged
- New commit with two parents
- Keeps full branch history
- git merge --no-ff forces one
- All branch commits combined
- One new commit on main
- Clean, readable main history
- Loses individual commits
- 1Branch from main
git switch -c feature/x origin/main
- 2Commit and push
git push -u origin feature/x
- 3Pull request
Review and CI on the branch
- 4Merge
Squash, merge commit or rebase
- 5Delete and prune
Remove the branch everywhere
- Back to step 1
In depth
How a three-way merge decides
Git finds the merge base with the same logic as git merge-base main feature. For each file it compares three versions: base, ours (main) and theirs (feature). If only one side changed a region, that change wins automatically. If both sides made the same change, there is no problem either. Only when both sides changed the same lines differently does Git stop with a conflict.
Since Git 2.34 the default merge engine is called ort. It replaced the older recursive strategy and is faster and better at renames. You rarely pick a strategy yourself, but options such as -X ours (prefer our side for conflicting hunks only) exist for special cases.
- 1Find merge base
The newest commit both branches share
- 2Diff base to ours
What main changed
- 3Diff base to theirs
What feature changed
- 4Combine hunks
Non-overlapping changes apply cleanly
- 5Commit or stop
Merge commit, or conflict for overlaps
Choosing a merge style as a team
On GitHub you can allow merge commits, squash merging and rebase merging. Many teams allow only squash: every pull request becomes one commit on main with the PR title as message, so main reads like a changelog and reverting a feature is one git revert. The cost is that the small steps inside the PR are gone from main.
Merge commits keep every commit and show exactly when a branch landed, which helps on long-running release branches. Rebase merging copies each commit onto main for a straight line with all the detail, but the copies get new hashes. Whatever you choose, pick one default and switch off the others in the repository settings so history stays consistent.
Why squash-merged branches look unmerged
A squash merge creates a brand new commit on main that contains the same changes, but it is not connected to your branch's commits. As far as Git's graph is concerned, your branch was never merged. So git branch -d feature refuses with "not fully merged" and git branch --merged main does not list it.
This is expected. Once you have confirmed the PR was merged, delete it with git branch -D. On GitHub, turn on "Automatically delete head branches" so the remote branch disappears on merge, then git fetch --prune removes your stale origin/feature copy. git branch -vv marks such branches with gone, which makes them easy to spot.
- git branch --merged main lists branches fully merged by merge or fast-forward
- git branch -vv shows [origin/x: gone] for branches deleted on the server
- git config --global fetch.prune true prunes on every fetch
Remote branches and tracking
origin/main is a remote-tracking branch: your local, read-only memory of where main was on the server the last time you fetched. It only moves when you run git fetch or git pull. Your own main is a separate pointer. git status compares the two and tells you if you are ahead or behind.
When you push a new branch with git push -u origin feature/login, the -u sets the upstream, so later git push and git pull know where to go without arguments. git pull is git fetch followed by a merge (or a rebase, if you set pull.rebase true). Setting pull.ff only makes pull refuse to create surprise merge commits.
Learn it in 4 short steps
About 45m each. Track them with a timer in your plan.
- 1Create and switch feature branches~45m
- 2Compare fast-forward and merge commits~45m
- 3Try squash merges~45m
- 4Delete merged branches and prune remotes~45m
Key points
- Create one branch per feature or fix, named like feature/login or fix/timeout.
- Fast-forward is only possible when main has not moved since you branched.
- Squash merges keep main tidy but hide the step-by-step history.
- Delete merged branches and prune stale remote refs to keep things clean.
Key terms
Tap a card to flip it. 0/5 known
Examples
git switch -c feature/login
git commit -am "Add login form"
git switch main
git merge --ff-only feature/login # fails if main moved
git merge --no-ff feature/login # always a merge commit
git merge --squash feature/login && git commit -m "Add login"
git branch -d feature/login
git fetch --pruneThe same branch merged three different ways.
Fast-forward or not? Checking before you merge
You want to know if merging feature/search will be a clean fast-forward. Step 1: fetch so origin/main is current. Step 2: count commits on each side with rev-list. If main has 0 commits the feature lacks, a fast-forward is possible. Step 3: if main moved, either merge (creating a merge commit) or rebase the feature first, which makes a fast-forward possible again.
git fetch origin
git rev-list --left-right --count origin/main...feature/search
# 0 4 -> main has 0 new commits, feature has 4: fast-forward works
# 2 4 -> both moved: a merge commit (or rebase first)
git log --oneline --graph origin/main feature/search -10Cleaning up after a busy sprint
Your laptop has 30 old branches. Step 1: fetch with prune so deleted remote branches vanish from origin/. Step 2: delete local branches already merged into main, keeping main itself. Step 3: list branches whose upstream is gone (usually squash-merged PRs), check them, then force-delete. Step 4: delete a remote branch you own that was never merged and is no longer needed.
git switch main && git pull --ff-only
git fetch --prune
git branch --merged main | grep -vE '^\*|main$' | xargs -r git branch -d
git branch -vv | grep ': gone]'
git branch -D fix/old-timeout # after checking it is merged
git push origin --delete spike/graphqlQuick reference
- git switch -c feature/x origin/main starts a branch from the latest server main
- git merge --ff-only fails instead of creating a merge commit
- git merge-base main feature prints the commit both branches share
- git branch -d deletes only if merged; -D deletes anyway
- git push -u origin feature/x pushes and sets the upstream for later pushes
- git push origin --delete feature/x removes the branch on the server
- git config --global pull.ff only stops git pull from making surprise merges
- git log --oneline --graph --all draws every branch in the terminal
Common mistakes
- Keeping a branch alive for weeks, so it drifts far from main.
- Deleting an unmerged branch with -D and losing work (reflog can still help).
Quick quiz
Pick an answer to see if you're right and why.
Practice exercises
Try each one yourself first. Use the hint if you get stuck, then compare with the solution.
- Exercise 1easy
Create a branch, make one commit, and merge it into main as a fast-forward. Confirm no merge commit was created.
- Exercise 2medium
Make main and a feature branch diverge (one commit each, different files), then merge with a merge commit and show both parents.
- Exercise 3hard
Write a one-line shell alias that deletes every local branch whose upstream is gone, and explain why it needs -D rather than -d.
Check yourself
Answer in your head, reveal, then be honest with yourself.
Feel confident about this topic?
Frequently asked questions
- Should I use git checkout or git switch?
- Both work. git switch (since Git 2.23) only changes branches, and git restore only restores files, so they are harder to misuse than the all-in-one git checkout.
- Is it better to merge or squash pull requests?
- Squash gives a clean, one-commit-per-PR main and easy reverts. Merge commits keep full detail and exact branch history. Pick one as the team default and disable the others.
- Why can I not create a branch called feature when feature/login exists?
- Branches are stored as file paths, so feature/login needs feature to be a folder. A branch named feature would have to be a file with the same name.