Branches and merges

Merge a branch

Bring a completed branch into the current branch.

8 minute lesson

~~~

A merge brings another line of history into the current branch.

First, make sure the working tree is clean and switch to the branch that should receive the work:

git status
git switch main
git merge add-search

The direction matters. This command merges add-search into main because main is the current branch.

Git finds the common ancestor and compares both histories. If main has not moved, Git can fast-forward its reference. If both branches have new commits, Git usually creates a merge commit after combining compatible changes.

Verify the result:

git status
git log --oneline --decorate --graph --all

After a successful merge, main reaches the feature commits. The add-search reference still exists until you delete it.

If Git reports a conflict, stop and inspect git status. You can resolve the files and continue, or return to the pre-merge state with git merge --abort before making unrelated changes.

Exercise: merge a one-commit branch into an unchanged main. Draw the graph before and after, and explain why no merge commit was necessary.

Lesson completed

Take this course offline

Get every free book, course edition, and software download.

Get the download library →