Pebellslearn by building
0%
RoadmapsMatrixCoursesSandboxSign in
Version Control and Hosting  ›  Lesson

Branches and Merging

Git 12 minA branch is a movable pointer to a commit, and merging either fast-forwards that pointer or creates a merge commit, stopping for a conflict when both sides changed the same lines.

Branches and Merging

You are halfway through adding search to notes-api when an urgent bug report arrives. Your half-finished work must not ship, but the fix must ship today. Branches let both lines of work exist side by side and meet again only when each is ready.

The rule

A branch is a movable label on a commit; merge joins two histories.

A branch is not a copy of your files. It is a tiny label that points at a commit and moves forward every time you commit on it. HEAD marks the branch you are on.

git switch -c feature/search     # create a branch here and move onto it
# ... commit your work ...
git switch main                  # go back; your files change to main's version
git switch -c fix/login-timeout  # branch for the urgent fix
# ... commit the fix ...
git switch main
git merge fix/login-timeout

When you merge, Git does one of two things:

  • Fast-forward: if main has not moved since you branched, Git simply slides the main label forward to your latest commit. No new commit is needed.
  • Merge commit: if both branches have new commits, Git creates a commit with two parents that combines them.

If both branches changed the same lines, Git cannot choose, so it stops with a conflict and marks the file:

<<<<<<< HEAD
timeout: 30
=======
timeout: 60
>>>>>>> fix/login-timeout

The top part is your current branch, the bottom part is the branch being merged. You edit the file to the correct final text, delete the markers, then git add the file and git commit to finish.

The shortcut

Before merging, draw the two labels in your head and ask: has the target moved since I branched? No means fast-forward, nothing to decide. Yes means a merge commit, and possibly conflicts only where both sides touched the same lines.

Shipping the fix while search waitsStep 1 of 4
  1. Branch from main

    git switch -c fix/login-timeout from main. The search work stays safe on its own branch.

The trap: resolving a conflict by blindly keeping "ours" or "theirs". Read both sides; the right answer is sometimes a mix.

Drills

Answer fast — trust the rule.

quick drill

What is a Git branch, underneath?

quick drill

You merge fix into main, and main has no new commits since fix was created. What happens?

quick drill

In a conflict, which part is the current branch's version?

quick drill

After editing a conflicted file to its correct content, what finishes the merge?

Remember

  • A branch is a movable label on a commit; merge joins two histories.
  • Shortcut: has the target moved? No means fast-forward; yes means a merge commit.
  • Trap: resolving conflicts without reading both sides.

Practice — level up

0/1 solved

Write your answer, then compare it with a rubric and a model answer.

ExplainResolve a conflict

Merging fix/login-timeout into main produced a conflict in config.yaml: HEAD says timeout: 30 and the branch says timeout: 60. Explain what the markers mean and every step to finish the merge correctly.