Branching and Merging

Branches let developers work in parallel without mixing changes together too early. This is one of the most important Git concepts.

Why branches exist

A branch is a separate line of work. Developers can create a branch for:

This keeps the main branch cleaner and safer.

Basic branch flow

git switch main git pull origin main git switch -c feature/login

Then do the work, commit, and push:

git add . git commit -m "Implement login screen" git push -u origin feature/login

Merge strategy

Once the work is ready, merge it into main:

git switch main git pull origin main git merge feature/login

If there are conflicts, Git will stop and ask for manual resolution.

Merge conflicts

A merge conflict happens when both branches modified the same lines.

Example conflict message:

CONFLICT (content): Merge conflict in app.py

To resolve:

  1. open the conflicted file
  2. decide which version is correct
  3. remove conflict markers
  4. stage the resolved file
  5. complete the merge
git add app.py git commit -m "Resolve merge conflict"

Safe merge practices

Fast-forward merge

If no one changed main while the feature branch was in progress, Git may do a fast-forward merge.

This is the simplest kind of merge.

Merge commit

When both sides changed, Git creates a merge commit. This keeps the history visible and helps with rollback decisions.

Branch naming conventions

A simple convention is:

Good names are short, clear, and descriptive.

Production example

A release team may branch from main to create a release branch. QA tests it, then it is merged to production after approval. This gives a controlled release path.

Common pitfalls

Key takeaways