tech katta

Branches and merging: fast-forward versus merge commit

Lesson 15 of 23 · 4 min readFree

In this lesson

Create and switch branches, understand that a branch is just a pointer, and see when a merge simply moves the pointer forward and when it creates a merge commit.

On this page
  1. Why this matters
  2. A branch is a pointer
  3. Merging, case 1: fast-forward
  4. Merging, case 2: a merge commit
  5. Controlling the style of merge
  6. Housekeeping
  7. Try it

A branch lets you work on a feature or a fix without disturbing the main line of code. Branches are cheap in Git, so teams use them for almost every change. This lesson shows what a branch really is, and the two ways Git can bring a branch back together with the main one.

Why this matters#

On a team, nobody works directly on the code that is deployed. You make a branch, do your work, have it reviewed and merge it. If you do not understand merging, you will be afraid of the screen that says "diverged" or "merge commit", and you will not be able to read your project's history. The pictures in this lesson are drawn by Git itself, using git log --graph.

All output comes from a real practice repository with a small API: a src/routes.js file and a README.

A branch is a pointer#

A branch is nothing more than a movable label attached to one commit. main is a label pointing at the latest commit of the main line. When you commit while on a branch, the label moves forward to the new commit. Creating a branch costs almost nothing, because Git does not copy any files; it only adds a new label.

HEAD says which branch you are on. Our repository has two commits on main:

Shell
git branch
git switch -c feature/search
git log --oneline --decorate
Text
* main
Switched to a new branch 'feature/search'
967086c (HEAD -> feature/search, main) Add courses route
11855fd Initial API

git switch -c name creates a branch and moves to it in one step. (Older guides use git checkout -b name, which does the same thing.) The decoration HEAD -> feature/search, main tells us that both labels point to the same commit right now and that we are standing on feature/search. The * in the git branch list marks the current branch.

Now make two commits on the feature branch:

Shell
echo 'app.get("/search", find);' >> src/routes.js
git commit -am "Add search route"
echo '- GET /search' >> README.md
git commit -am "Document search"
git log --oneline --decorate --graph --all
Text
* 9ab11f8 (HEAD -> feature/search) Document search
* 09bcfe8 Add search route
* 967086c (main) Add courses route
* 11855fd Initial API

The feature/search label moved forward twice, and main stayed where it was. Your files change when you switch: go back to main and the search route is gone from your folder, because that commit is not part of main. Git rewrites the working tree to match the branch. Switch with git switch main.

Merging, case 1: fast-forward#

To bring the feature into main, switch to main and merge the branch in:

Shell
git switch main
git merge feature/search
Text
Updating 967086c..9ab11f8
Fast-forward
 README.md     | 1 +
 src/routes.js | 1 +
 2 files changed, 2 insertions(+)

Fast-forward means main had no new commits of its own since the branch was created, so Git did not need to combine anything. It just moved the main label forward to the commit that feature/search points to.

Text
* 9ab11f8 (HEAD -> main, feature/search) Document search
* 09bcfe8 Add search route
* 967086c Add courses route
* 11855fd Initial API

The history is a straight line. No extra commit exists, and it looks as if the work had been done directly on main.

Merging, case 2: a merge commit#

Real life is messier. Someone else, or you, has committed to main while the feature branch was in progress, so the two lines have diverged. We create feature/login from the current main, add a commit, then go back to main and add a different commit:

Text
* 1b3bcaf (feature/login) Add login route
| * 26f0e16 (HEAD -> main) Add run instructions
|/  
* 9ab11f8 (feature/search) Document search
* 09bcfe8 Add search route
* 967086c Add courses route
* 11855fd Initial API

The graph forks at 9ab11f8. main cannot simply move forward to the login commit, because it would lose "Add run instructions". Git must combine the two lines, and it does that by creating a new commit with two parents.

Shell
git merge feature/login -m "Merge feature/login"
git log --oneline --decorate --graph
Text
Merge made by the 'ort' strategy.
 src/routes.js | 1 +
 1 file changed, 1 insertion(+)
*   157f341 (HEAD -> main) Merge feature/login
|\  
| * 1b3bcaf (feature/login) Add login route
* | 26f0e16 Add run instructions
|/  
* 9ab11f8 (feature/search) Document search
* 09bcfe8 Add search route
* 967086c Add courses route
* 11855fd Initial API

The merge commit 157f341 joins both lines. We can confirm it has two parents:

Shell
git log -1 --format='parents: %p'
Text
parents: 26f0e16 1b3bcaf

The word ort is the name of Git's default merge algorithm; you do not need to do anything with it. Git was able to merge automatically because the two sides changed different parts of the files. When both changed the same lines, Git stops and asks you to decide. That is a merge conflict, the topic of the next lesson.

Controlling the style of merge#

Many teams want a visible record that a feature branch existed. They ask Git for a merge commit even when a fast-forward is possible, using --no-ff:

Shell
git merge --no-ff feature/admin -m "Merge feature/admin"
Text
*   46284b6 Merge feature/admin
|\  
| * ce14c85 Add admin route
|/  

The opposite choice is --ff-only, which merges only if a fast-forward is possible, and otherwise refuses. This is useful when you want to guarantee a straight history:

Shell
git merge --ff-only feature/x
Text
fatal: Not possible to fast-forward, aborting.

Neither style is right or wrong. Fast-forward gives a clean line and loses the information about which commits belonged together. A merge commit keeps that information and adds extra commits to read. Lesson 18 compares merging with rebasing, and lesson 21 compares team workflows.

Housekeeping#

List branches with git branch. Once a branch is merged, delete its label, which does not delete any commits:

Shell
git branch -d feature/search
git branch -d feature/x
Text
Deleted branch feature/search (was 9ab11f8).
error: the branch 'feature/x' is not fully merged.
If you are sure you want to delete it, run 'git branch -D feature/x'

The lowercase -d refuses to delete a branch whose commits are not in your current branch. That protects you from losing work. Capital -D forces the deletion. git branch --merged and git branch --no-merged list which branches are safe to clean up.

Switching with unsaved changes#

Git will not switch branches if doing so would overwrite your uncommitted edits:

Text
error: Your local changes to the following files would be overwritten by checkout:
	src/routes.js
Please commit your changes or stash them before you switch branches.
Aborting

Commit the change first, or stash it (lesson 20). If your edits do not touch files that differ between the branches, Git carries them over with you.

Detached HEAD#

You can also check out a commit that is not a branch tip, for example with git switch --detach HEAD~2. Git then says HEAD detached at 157f341. That is fine for looking around. Commits made here belong to no branch and are easy to lose, so create a branch with git switch -c name before you start new work.

Try it#

  1. Create a repository with two commits. Create a branch feature/a, make two commits on it, and view git log --oneline --decorate --graph --all before and after the commits.
  2. Switch back to main and check that the files from the branch are not there. Merge the branch and confirm that Git says "Fast-forward".
  3. Create feature/b from main, add a commit, then switch back to main and add a different commit that touches a different file. Merge feature/b and find the merge commit in the graph.
  4. Show the two parents of the merge commit with git log -1 --format='%p'.
  5. Repeat step 3 with git merge --no-ff on a branch that could have fast-forwarded. Compare the graph with the earlier one.