โ† Git Essentials

Branching and Merging

~260 words ยท 2 min read

Why branches?

A branch is an independent line of development. Instead of everyone committing to the same timeline, each person (or feature) gets its own branch. You work in isolation, then merge the result back.

Creating and switching

git branch feature-login   # create
git switch feature-login   # switch to it

# or in one step:
git switch -c feature-login

Merging

When the feature is done, you merge it back into the main line (usually called main or master):

git switch main
git merge feature-login

If both branches changed the same lines, Git reports a merge conflict. You resolve it by editing the conflicted files, then committing the resolution.

Fast-forward vs merge commit

  • Fast-forward โ€” the target branch hasn't moved since you branched. Git just slides the pointer forward. No merge commit needed.
  • Three-way merge โ€” both branches have new commits. Git creates a merge commit that joins the two histories.
Branches are cheap in Git. Create them freely โ€” one per feature, one per bugfix, one per experiment.