Git Merge

Updated · 11 min read
mediumGit 2.53gitgit-mergemerge-commitmerge-baseinterview

Two people write different chapters of the same report. One adds a chapter on fees, the other fixes the introduction. At the end, someone has to put both into one finished document: take the fees chapter from the first copy, the new introduction from the second, and everything they share from either.

git merge does that for branches. It takes the work done on another branch and combines it with the branch you are on. Most of the time Git does it completely on its own, because the two branches changed different files or different lines. When both changed the same line, Git asks you to decide, which is the topic of the lesson on merge conflicts.

In this lesson, you will merge a finished feature branch into main, see the merge commit Git creates, and understand how Git works out what to combine.

The Starting Point

Advitha built receipt printing on its own branch with two commits. Meanwhile, Navika added the hostel fee on main:

Terminal
mkdir fee-tracker
cd fee-tracker
git init --quiet
echo "Fee Tracker" > README.md
echo 'Term 1 fee: $12,000' > fees.txt
git add .
git commit --quiet -m "Add README and the term fee"
git switch --quiet -c feature/receipts
echo "Receipt No. 1" > receipts.txt
git add receipts.txt
git commit --quiet --author="Advitha Anamaneni <advitha@example.com>" -m "Add receipt numbers"
echo "Print after payment" >> receipts.txt
git commit --quiet -a --author="Advitha Anamaneni <advitha@example.com>" -m "Print a receipt after payment"
git switch --quiet main
echo 'Hostel fee: $9,000' >> fees.txt
git commit --quiet -am "Add the hostel fee"
git log --oneline --graph --all
Output
* d9fe232 (HEAD -> main) Add the hostel fee
| * 23743f5 (feature/receipts) Print a receipt after payment
| * 6dcd72f Add receipt numbers
|/
* 4988b51 Add README and the term fee

The two branches have diverged: each has commits the other doesn't. main doesn't have receipts.txt, and feature/receipts doesn't have the hostel fee.

Merge a Branch

A merge always brings another branch into the branch you are on. So first switch to the branch that should receive the work, here main, then name the branch to merge:

Terminal
git branch --show-current
git merge --no-edit feature/receipts
Output
main
Merge made by the 'ort' strategy.
 receipts.txt | 2 ++
 1 file changed, 2 insertions(+)
 create mode 100644 receipts.txt

Merge made by the 'ort' strategy means Git combined the two lines of work on its own. ort is the name of Git's standard merge method. The list below it is what the merge brought into main: the new file receipts.txt.

--no-edit accepts the message Git suggests. Without it, Git opens your editor with that message, the same way as git commit without -m, so you can add a note.

Now look at the history:

Terminal
git log --oneline --graph
ls
Output
*   f3fd1ee (HEAD -> main) Merge branch 'feature/receipts'
|\
| * 23743f5 (feature/receipts) Print a receipt after payment
| * 6dcd72f Add receipt numbers
* | d9fe232 Add the hostel fee
|/
* 4988b51 Add README and the term fee
README.md
fees.txt
receipts.txt

The new commit at the top, Merge branch 'feature/receipts', is a merge commit. It has two parents: the hostel fee commit (the last commit on main) and Advitha's last commit. The graph shows the two lines joining. main now has both the hostel fee and the receipts file, and Advitha's commits are part of main's history, with their original IDs, authors and messages.

feature/receipts itself didn't change. Merging only moves the branch you are on.

A merge commit joins two lines of work

Syntax

Terminal
git merge [options] <branch>
OptionIt is used to
git merge <branch>Merge <branch> into the current branch, opening the editor for the message if a merge commit is made
--no-editAccept Git's merge message without opening the editor
-m "<message>"Give the merge commit message on the command line
--no-ffAlways make a merge commit, even when a fast-forward is possible
--ff-onlyMerge only if it can be a fast-forward, otherwise stop
--squashCombine the branch's changes into the staging area, without a merge commit
--abortStop a merge that has conflicts and go back to how things were

--no-ff, --ff-only and --squash are explained in the next lesson, "Fast-Forward vs Merge Commit".

Tidy Up After Merging

Advitha's branch is finished, and all its commits are now on main. git branch --merged confirms it, and -d deletes it without complaint:

Terminal
git branch --merged
git branch -d feature/receipts
Output
  feature/receipts
* main
Deleted branch feature/receipts (was 23743f5).

Deleting a merged branch deletes only its name. The commits stay in main's history.

Keep a Feature Branch Up to Date

Merging works in both directions. While a feature takes days or weeks, main keeps moving. To bring main's latest work into your branch, and find any problems early, merge main into the feature:

Terminal
git switch --quiet -c feature/exam-fee
echo 'Exam fee: $800' > exam.txt
git add exam.txt
git commit --quiet -m "Add the exam fee"
git switch --quiet main
echo "Run the app." >> README.md
git commit --quiet -am "Explain how to run the app"
git switch --quiet feature/exam-fee
git merge --no-edit main
git log --oneline --graph
Output
Merge made by the 'ort' strategy.
 README.md | 1 +
 1 file changed, 1 insertion(+)
*   67adc90 (HEAD -> feature/exam-fee) Merge branch 'main' into feature/exam-fee
|\
| * 2f75a0c (main) Explain how to run the app
* | 1084be1 Add the exam fee
|/
*   f3fd1ee Merge branch 'feature/receipts'
|\
| * 23743f5 Print a receipt after payment
| * 6dcd72f Add receipt numbers
* | d9fe232 Add the hostel fee
|/
* 4988b51 Add README and the term fee

The feature branch now has the README change from main, and its own exam fee commit. When the feature is finished, merging it into main will be simple, because it already contains everything main has. Another way to bring a branch up to date is git rebase, which has its own lessons.

In Practice

On a team, merges into main usually happen through a pull request (called a merge request on GitLab) on GitHub, GitLab or Bitbucket. You push your branch, teammates review it, and the website performs the merge with a button. It runs the same merge you did here, and offers the options of the next lesson: a merge commit, a squash or a rebase. Merging locally, as in this lesson, is what you do in your own projects, and what you use to bring main into your feature branch.

Common Mistakes

Merging in the wrong direction. git merge main while on main does nothing, and merging the feature while on the feature brings nothing new. Check git branch --show-current first.

Merging with uncommitted changes. Git refuses if the merge would overwrite them, and they make it hard to undo a merge. Commit or stash first, so git status is clean.

Expecting the merged branch to change. Only the current branch moves. The other branch stays where it was until you delete it or keep working on it.

Forgetting to delete finished branches. After a merge, git branch -d keeps the branch list short and tells everyone the work is done.

Panicking at the editor. A merge without --no-edit or -m opens your editor with a ready message. Save and close it to finish the merge.

Under the Hood

A merge of two diverged branches is a three-way merge. Git uses three snapshots:

  1. The last commit of the current branch, called ours.
  2. The last commit of the branch being merged, called theirs.
  3. The merge base: the newest commit that both branches have in common, where they split.

git merge-base shows it:

Terminal
git merge-base main feature/exam-fee
git log --oneline -1 main
Output
2f75a0c4f4cd58292206c42cc7bc8bbe34391fde
2f75a0c (main) Explain how to run the app

The merge base of main and feature/exam-fee is now main's newest commit, because the feature already merged it. For the first merge in this lesson, it was "Add README and the term fee".

For every file, Git compares both sides with the base. If only one side changed something, Git takes that change. If both sides made the same change, Git takes it once. If both sides changed the same lines differently, Git can't choose, and reports a conflict. That is why merging is safe for work on different parts of a project, and why a conflict is not an error: it is Git asking you to decide.

Interview Corner

What does git merge do?

It combines the history of another branch into the current branch. If the current branch has no commits of its own since the branches split, Git moves it forward (a fast-forward). Otherwise it does a three-way merge and records the result as a merge commit with two parents.

What is a merge commit?

A commit with two (or more) parents, created when two diverged branches are merged. Its snapshot combines both sides' changes since the merge base, and it records in the history that the two lines of work were joined.

What is a merge base?

The best common ancestor of the two branches being merged, usually the commit where they split. Git compares both branch tips with it to decide which changes each side made. git merge-base <a> <b> prints it.

How do you bring the latest changes from main into your feature branch?

Switch to the feature branch and run git merge main (after git fetch and updating main if you work with a remote). The alternative is git rebase main, which replays your commits on top of main instead of creating a merge commit.

Does merging change the branch that is merged?

No. Only the current branch moves to the new merge commit. The merged branch stays where it was and can be deleted with git branch -d once its work is merged.

Practice

Challenge: merge the exam fee. In the same repository, you are on feature/exam-fee, which already contains main. Switch to main, merge feature/exam-fee, and print the one-line log of the three newest commits.

What does Git print? Before you open the solution, decide whether Git needs to make a new merge commit this time. Think about which commits main has that the feature doesn't.

Show solution
Terminal
git switch --quiet main
git merge feature/exam-fee
git log --oneline -3
Output
Updating 2f75a0c..67adc90
Fast-forward
 exam.txt | 1 +
 1 file changed, 1 insertion(+)
 create mode 100644 exam.txt
67adc90 (HEAD -> main, feature/exam-fee) Merge branch 'main' into feature/exam-fee
2f75a0c Explain how to run the app
1084be1 Add the exam fee

No new merge commit. main had no commits that the feature lacked, because the feature had already merged main. So Git simply moved main forward to the feature's newest commit, and said so: Fast-forward. That is why the newest commit on main is now the merge made on the feature branch, Merge branch 'main' into feature/exam-fee. The next lesson is about exactly this kind of merge.

Checkpoint. In a practice repository, create two branches from main, make a commit on each that changes a different file, and merge both into main. Look at git log --oneline --graph after each merge, and find the merge base of the two branches before you start.

Quick check: Git merge

4 questions
1You want the work of feature/receipts in main. Where do you run git merge feature/receipts?
2What is special about a merge commit?
3What does --no-edit do?
4What does Git compare with in a three-way merge, besides the two branch tips?

Key Takeaways

Quick reference

CommandIt is used to
git merge <branch>Merge a branch into the current one
git merge --no-edit <branch>Merge with Git's default message
git merge -m "<message>" <branch>Merge with your own message
git merge --abortCancel a merge with conflicts
git merge-base <a> <b>Show where two branches split
git branch --mergedList branches already merged
git log --oneline --graphSee merges in the history

Was this tutorial helpful?

Lesson 30 of 106