Git & GitHub · 12 min · 140 XP

Branching and merging

Work on a branch, bring it back with a merge, and resolve a conflict calmly.

A branch is only a movable label pointing at a commit. Creating one costs nothing and copies nothing. Working on a branch keeps half-finished work away from main, so main always stays in a state you could ship.

Start a branch, work on it, look at the shape of history
git switch -c feature/discount-codes   # create the branch and move onto it
# ...edit, add, commit as usual...
git log --oneline --graph --all        # see where each branch points

git merge brings the branch back in two ways. If main has not moved since you branched, Git just slides the main label forward to your latest commit. This is called a fast-forward, and no new commit is created. If both branches have new commits, Git creates a merge commit with two parents, joining the two histories.

bash
git switch main
git pull                      # get everyone else's work first
git merge feature/discount-codes

A conflict happens when both branches changed the same lines and Git cannot decide which version should win. It stops, marks the file, and waits for you. Everything between <<<<<<< and the divider is your side (HEAD), and everything between the divider and >>>>>>> is the incoming branch.

What a conflict looks like inside the file
<<<<<<< HEAD
const FREE_SHIPPING_OVER = 50;
=======
const FREE_SHIPPING_OVER = 75;
>>>>>>> feature/discount-codes

To resolve it, edit the file so it reads the way it should. That might be one side, the other, or a mix of both. Delete all three marker lines, then run git add on the file and git commit to finish the merge. If you'd rather back out and think first, git merge --abort returns everything to how it was before the merge.

A conflict isn't an error. It means two people changed the same thing and someone has to decide which version is right. Read both sides before choosing, because the right answer is sometimes a combination of the two.

Scenario A
$ git log --oneline --graph --all
* 7d1e9a0 (feature/discount-codes) Validate discount code format
* 3c8b2f1 Add discount code field
* a1b2c3d (HEAD -> main) Fix rounding in cart total
Scenario B: someone pushed to main in the meantime
$ git log --oneline --graph --all
* 5e6f7a8 (HEAD -> main) Raise free shipping to 60
| * 7d1e9a0 (feature/discount-codes) Validate discount code format
| * 3c8b2f1 Add discount code field
|/
* a1b2c3d Fix rounding in cart total
The merge stops with a conflict in src/config.js
<<<<<<< HEAD
const FREE_SHIPPING_OVER = 60;
=======
const FREE_SHIPPING_OVER = 50;
const DISCOUNT_CODE_PATTERN = /^[A-Z0-9]{6}$/;
>>>>>>> feature/discount-codes
After editing the file
$ git status
On branch main
You have unmerged paths.
  (fix conflicts and run "git commit")

Unmerged paths:
        both modified:   src/config.js

Try it on your machine: make a conflict on purpose. On a new branch, change a line and commit. Switch back to main, change the same line differently and commit. Merge the branch, resolve the conflict, git add and commit, then look at the result with git log --oneline --graph.

Loading your workspace…