01 / 11
Branches and Merging
INPUT · Slides
Branching and merging
02 / 11
A branch is an independent line of development
In Chapter 1, you saw commits accumulating as a line of snapshots. A branch is an independent development line split from a specific point in commit history.
Once branched, you can commit freely on either branch. Modifying files on one branch has zero effect on any other branch.
When your work is ready, you merge (integrate) the branch back together. This is Git's greatest strength for concurrent development.
03 / 11
git branch — List and create branches
Running git branch lists your local branches. An asterisk * marks the currently checked out (active) branch.
Running git branch <branch-name> creates a new branch pointing to the current commit. Note that this only creates the branch; it does not switch to it.
$ git branch* main$ git branch draft$ git branch draft* main04 / 11
git switch — Switch between branches
To switch to another branch, run git switch <branch-name>. Adding the -c flag creates a new branch and switches to it in one command (the most commonly used command in daily development).
When you switch branches, your working tree files immediately update to reflect the commits on the target branch. Files haven't vanished — they remain safely stored in the original branch's commits.
$ git switch -c new-menuSwitched to a new branch 'new-menu'05 / 11
Commit before switching branches
If you try to switch branches while holding uncommitted modifications in the working tree, Git refuses and produces an error.
This safety feature prevents uncommitted changes from being unintentionally overwritten or assigned to the wrong branch. Always stage and commit your changes with git add and git commit before switching.
When git status reports nothing to commit, working tree clean, you can switch branches safely.
$ git switch mainerror: Your local changes would be overwritten by switch.06 / 11
git merge — Integrate branch changes
When work on a feature branch is finished, switch to the receiving branch first, then run git merge <source-branch>.
Order is crucial. If you want to merge new-menu into main, first run git switch main, and then run git merge new-menu. The destination branch is always determined by whichever branch is currently checked out.
$ git switch main$ git merge new-menuUpdating 3b7e5d1..9f2a1c4Fast-forward07 / 11
Fast-forward merge
When git outputs Fast-forward, no new merge commit is created.
Because main had not accumulated any new commits since branching, Git simply advanced the main branch pointer forward to match the tip of the feature branch.
The commit graph remains a single linear progression without divergence.
08 / 11
3-way merge and merge commits
If both branches accumulated new commits after diverging, a Fast-forward is impossible.
Git combines both sets of changes into a new snapshot called a merge commit, which uniquely has two parent commits.
On the commit graph, you will see the two divergent lines merge back together at this commit.
$ git merge sideMerge made by the "ort" strategy.09 / 11
Merge conflicts
When both branches modify the same lines of the same file in conflicting ways, Git halts automatic merging. This is a conflict.
This is not a failure or bug — Git is simply stating: "Both branches altered these lines, and only a human developer can determine which version is correct."
Changes on different, separate lines are merged automatically by Git without conflict.
$ git merge ramenCONFLICT (content): Merge conflict in menu.txtAutomatic merge failed; fix conflicts and then commit the result.10 / 11
How to resolve a merge conflict
Opening a conflicted file reveals Git conflict markers:
<<<<<<< HEADto=======… content from your currently checked out branch=======to>>>>>>> <branch>… content from the incoming branch being merged
To resolve, delete all marker lines (<<<<<<<, =======, >>>>>>>) and edit the file to the desired final state.
After saving, run git add <file> to mark the conflict as resolved, followed by git commit to finalize the merge commit.
<<<<<<< HEADudon=======ramen>>>>>>> ramen11 / 11
Aborting a merge (git merge --abort)
If you encounter a conflict and prefer not to resolve it immediately, run git merge --abort to safely roll back to the state prior to starting the merge.
Nothing is lost or corrupted, allowing you to reassess or clean up before trying again.