CH.11 · PART 3

Bring the work together.

Fast-forward or merge commit: learn what Git changes, and which branch receives the work.

two parents, one merge

Merge into the branch you are on

First switch to the receiving branch. Then name the branch whose work you want to bring in. git merge feature/greeting while on main updates main, not feature/greeting.

git status
git switch main
git merge feature/greeting

Start with a clean working tree. Committing or stashing beforehand makes it easier to recover if you change your mind. Merging is local: it does not push anything to GitHub.

One command, two history shapes

Choose a history shape. This is a visual model, not terminal output.

A → B → C
main moves from A to C. No new commit.

Main has no separate commits. Git only moves its pointer forward.

Read the result

Fast-forward

Main was behind the feature branch. Moving the main pointer includes the new commits without making another commit.

Merge commit

Both histories have new work. Git combines their changes and records a commit with two parents.

Conflict

Git cannot combine some changes automatically. It pauses for you to resolve them. Chapter 12 walks through that.

A real run: first fast-forward, then a merge

This continues the Chapter 10 repository. After the fast-forward, create feature/footer and commit a new footer.txt containing goodbye. Back on main, commit a different new file, title.txt, containing our project. Those non-overlapping changes produce a merge commit.

The transcript was recorded locally on Git 2.34.1. File edits and the two intermediate commits are described above rather than printed. Hashes and strategy wording may differ in your version.


$ git switch main 
Switched to branch 'main'

$ git merge feature/greeting 
Updating af9f623..d1ebb55
Fast-forward
 greeting.txt | 1 +
 1 file changed, 1 insertion(+)

$ git log --oneline --decorate 
d1ebb55 (HEAD -> main, feature/greeting) Add welcome
af9f623 Start greeting

$ git switch -c feature/footer 
Switched to a new branch 'feature/footer'

$ git switch main 
Switched to branch 'main'

$ git merge feature/footer --no-edit 
Merge made by the 'ort' strategy.
 footer.txt | 1 +
 1 file changed, 1 insertion(+)
 create mode 100644 footer.txt

$ git log --graph --oneline --all --decorate 
*   23b3088 (HEAD -> main) Merge branch 'feature/footer'
|\  
| * 69be6d2 (feature/footer) Add footer
* | 5e0cf4a Add title
|/  
* d1ebb55 (feature/greeting) Add welcome
* af9f623 Start greeting

Pick the option deliberately

git merge --ff-only feature/greeting permits only a fast-forward; it refuses a diverged history. git merge --no-ff feature/greeting asks for a merge commit even when a fast-forward would be possible. Neither is the right default for every team. Follow your repository's convention.

To check the result, use git log --graph --oneline --all --decorate and run your project's tests. A merge that completes is not proof that the combined application works.

Quick check

Does a fast-forward create a new merge commit?

No. It advances the receiving branch to an existing commit.

You want the feature in main. Which branch should be checked out?

Main. Git merges into the current branch.

Sources

Official git merge documentation · Git book: Basic Branching and Merging

Git, Visually · Part 3 · English