Branching and Merging

Harry · 14 Sep 2026 · 3 views
Advertisement
Advertisement

Why branches

A branch is a movable pointer to a line of development. Because Git branches are just lightweight references to commits, creating one is instant. You keep main stable and do each new feature or fix on its own branch, then merge it back when it is ready.

A feature branch splitting from main and later merging back

Create and switch

git switch -c feature/login   # create and switch in one step
# ...edit, add, commit on the branch...
git switch main               # go back to main

(git checkout -b feature/login is the older equivalent of switch -c.)

Merging back

When the feature is done, merge it into main:

git switch main
git merge feature/login
  • If main has not moved, Git does a fast-forward – it simply advances the pointer.
  • If both branches have new commits, Git creates a merge commit that ties the two histories together (a three-way merge).

Resolving conflicts

A conflict happens when the same lines changed on both branches. Git pauses the merge and marks the file:

<<<<<<< HEAD
color: blue;
=======
color: green;
>>>>>>> feature/login

Edit the file to the version you want, delete the marker lines, then finish the merge:

git add styles.css
git commit            # completes the merge

Key points

  • Branches are cheap pointers – keep main stable and build features on their own branch.
  • git switch -c name creates and switches; git merge name brings work back.
  • Merges fast-forward when possible, otherwise create a merge commit.
  • Conflicts are edited by hand, then staged and committed to finish the merge.
Share this post:

Comments (0)

Please login or register to comment.