Earn it: GitHub Achievements Academy home

Git 3: Branching and merging

Branches let you try things without risking main. Merging brings the work back. Merge conflicts are the part that scares people, so this lesson includes a safe practice conflict you can solve on your own machine.

Make, switch, list, delete

git switch -c add-search     # create a branch and switch to it
git switch main              # go back to main
git branch                   # list local branches (* marks where you are)
git branch -d add-search     # delete a branch that's been merged

Older tutorials use git checkout for this. git switch (for branches) and git restore (for files) do the same jobs with clearer names. Both have existed since Git 2.23.

Got uncommitted changes? git switch brings them with you if they don't clash. If they would be overwritten, Git refuses and asks you to commit or stash first. It never throws away your work silently.

Two kinds of merge

Fast-forward. If main hasn't moved since you branched, Git just slides the main label forward to your latest commit. No new commit is needed:

Before. feature is two commits ahead, and main hasn't moved.Try this in the sandbox
After git merge feature. The main label slides forward to E.Try this in the sandbox

Merge commit. If both branches have new commits, Git creates a new commit with two parents that joins them:

Both branches moved on, so merging creates M, a commit with two parents (F and E).Try this in the sandbox
git switch main
git merge feature            # fast-forward if possible, merge commit if not
git merge --no-ff feature    # always make a merge commit (keeps a visible "feature" bump)

On GitHub, the Merge pull request button gives you three choices:

OptionResult
Create a merge commitKeeps every commit, plus a merge commit
Squash and mergeAll the PR's commits become one commit on main
Rebase and mergeReplays each commit on top of main, with no merge commit
Squash and merge: the changes from D and E land on main as one new commit, S. D and E themselves stay on the PR branch.Try this in the sandbox

Merge conflicts, demystified

A conflict happens when both branches changed the same lines of the same file. Git can't guess which version is right, so it stops and asks you. Nothing is broken or lost.

The file will contain markers like this:

<<<<<<< HEAD
Welcome to the Blue Cafe!
=======
Welcome to the Green Cafe!
>>>>>>> rename-cafe
  • Between <<<<<<< HEAD and ======= is your current branch's version
  • Between ======= and >>>>>>> rename-cafe is the incoming version

To resolve:

  1. Edit the file so it says what it should say. Keep one version, combine them, or write something new. Delete all three marker lines.
  2. Stage it: git add <file>
  3. Finish the merge: git commit (Git suggests a message)

Changed your mind halfway? git merge --abort puts everything back the way it was before the merge.

See it happen: the sandbox has challenges for branching, fast-forward merges and merge commits, with a live graph.

VS Code shows Accept Current / Accept Incoming / Accept Both buttons above each conflict, and a 3-way merge editor. Use them, but always read the result before committing.

Practice: a conflict made just for you

This repo includes a script that builds a tiny throwaway repo with a conflict waiting in it. It runs entirely on your computer and never touches GitHub. Run it from your clone of this repo (you made one in Lesson 3). On Windows, use Git Bash:

./scripts/practice-conflict.sh
cd /tmp/git-conflict-practice     # the script prints the exact path
git merge rename-cafe             # → CONFLICT!

Now resolve it using the steps above. Check your work with git log --oneline --graph. You should see the two branches joined by a merge commit. Delete the folder when you're done.

Keep branches short-lived

The longer a branch lives, the more main moves underneath it, and the bigger your conflicts get. Good habits:

  • One branch per task, merged within days, not weeks
  • Pull main into your branch (or rebase onto it) regularly
  • Delete branches once they're merged. GitHub can do this automatically: Settings → General → Automatically delete head branches

What you learned

  • Creating, switching and deleting branches
  • Fast-forward merges vs. merge commits, and GitHub's three merge buttons
  • Reading conflict markers and resolving conflicts calmly

Next: Git 4: Rebase without fear

Check what you learned

1. When does a fast-forward merge happen?
2. In a conflict, what's between <<<<<<< HEAD and =======?
3. How do you back out of a merge in progress?

Show the answers
  1. When the target branch hasn't moved since you branched: Git just slides the label forward, so no merge commit is needed.
  2. Your current branch's version: The top half is yours (HEAD). The bottom half, before >>>>>>>, is incoming.
  3. git merge --abort: It puts everything back exactly as it was before the merge.

Your progress is saved in this browser only.

Last reviewed November 2026. Improve this page on GitHub. Every fix you send counts toward Pull Shark.