TetSharing
Git & CLI tutorial · 12 chapters
  1. Terminal basics
  2. Working with files
  3. Pipes and redirection
  4. What Git is
  5. Staging and commits
  6. Viewing history
  7. Comparing changes
  8. Branches
  9. Merging
  10. Merge conflicts
  11. Undoing changes
  12. Remotes, push and pull

Final Git & CLI quiz
Cheat sheet: git commands
Cheat sheet: linux shell commands

Chapter 9 of 12 · By Ethan Cole · Updated November 21, 2026

Git merge: Combine Branches, Fast-Forward or Not

Branches are only useful if their work can come back together. git merge takes the commits from another branch and combines them into the branch you are on.

The routine

  1. Switch to the branch that should receive the work, usually main: git switch main.
  2. Merge the other branch into it: git merge feature-search.
  3. Delete the finished branch: git branch -d feature-search.

The direction matters: you always merge into the current branch. Merging main into a feature branch is also common, to bring it up to date before finishing.

Fast-forward merges

If main has not moved since the feature branch was created, there is nothing to combine. Git simply slides the main label forward to the feature's latest commit. This is a fast-forward, and Git prints Fast-forward in its output. History stays a straight line.

Merge commits

If both branches have new commits, their histories have diverged. Git then compares the two tips with their common ancestor, combines the changes, and records a merge commit with two parents. Git opens an editor for the merge commit's message; --no-edit accepts the default message, which our transcript uses so it can run unattended.

Terminal · bash + git · copy and run locally
git init -q
echo "Home" > index.txt
git add index.txt
git commit -q -m "Add home page"
git switch -q -c about
echo "About us" > about.txt
git add about.txt
git commit -q -m "Add about page"
git switch -q main
git merge about
git branch -d about
git switch -q -c contact
echo "Contact" > contact.txt
git add contact.txt
git commit -q -m "Add contact page"
git switch -q main
echo "Footer" > footer.txt
git add footer.txt
git commit -q -m "Add footer"
git merge --no-edit contact
git log --oneline --graph --format="%s"

Transcript (verified by our test suite in a fresh temp folder)

$ git init -q
$ echo "Home" > index.txt
$ git add index.txt
$ git commit -q -m "Add home page"
$ git switch -q -c about
$ echo "About us" > about.txt
$ git add about.txt
$ git commit -q -m "Add about page"
$ git switch -q main
$ git merge about
Updating 725737b..cdc8fdb
Fast-forward
 about.txt | 1 +
 1 file changed, 1 insertion(+)
 create mode 100644 about.txt
$ git branch -d about
Deleted branch about (was cdc8fdb).
$ git switch -q -c contact
$ echo "Contact" > contact.txt
$ git add contact.txt
$ git commit -q -m "Add contact page"
$ git switch -q main
$ echo "Footer" > footer.txt
$ git add footer.txt
$ git commit -q -m "Add footer"
$ git merge --no-edit contact
Merge made by the 'ort' strategy.
 contact.txt | 1 +
 1 file changed, 1 insertion(+)
 create mode 100644 contact.txt
$ git log --oneline --graph --format="%s"
*   Merge branch 'contact'
|\
| * Add contact page
* | Add footer
|/
* Add about page
* Add home page

The transcript does both kinds. The first merge is a fast-forward. Then main and a second branch each get a commit touching different files, and the merge creates a merge commit. The --graph view at the end draws the fork and the join.

As long as the two branches changed different files, or different parts of the same file, Git combines them automatically. When both changed the same lines, you get a conflict, which is the subject of the next chapter.

Fast-forward or merge commit?

Some teams always want a merge commit so that every feature appears as a visible unit in history. git merge --no-ff feature forces one even when a fast-forward was possible. Others prefer a straight line and use rebase to replay feature commits on top of main before merging. Both are valid; follow your team's convention.

Common mistakes

  • Merging while on the wrong branch. Check with git status or git branch first.
  • Starting a merge with uncommitted changes. Commit or stash first so a merge problem cannot mix with unfinished work.
  • Forgetting to delete merged branches, which leaves a long list of stale names.

Summary

  • Switch to the receiving branch, then git merge other.
  • Fast-forward when only one side moved; otherwise a two-parent merge commit.
  • Delete merged branches with git branch -d.

Quiz: Merging

1. You are on main and run git merge feature. Which branch changes?

git merge brings the other branch's work into the current branch.

2. When does Git do a fast-forward merge?

With nothing new on the receiving side, Git just moves its label forward.

3. How many parents does a regular merge commit have?

A merge commit joins two lines of history, so it has two parents.

Official docs

Written under the pen name Ethan Cole with AI-assisted drafting. Every code example on this page is executed by our test suite before publishing. Found a mistake? Tell us.