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
- Switch to the branch that should receive the work, usually
main:git switch main. - Merge the other branch into it:
git merge feature-search. - 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.
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 statusorgit branchfirst. - 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