Git & GitHub for Developers Branching & Merging
5 / 11
Next
Branching & Merging ~14min

Branching & Merging

Branches let you work on features or fixes in isolation without touching the main codebase. This is how professional teams work.

Branch commands

# Create and switch to a new branch
git checkout -b feature/login
# or (modern syntax)
git switch -c feature/login

# List branches
git branch -a

# Switch branches
git checkout main
git switch main

# Merge a branch into current branch
git merge feature/login

# Delete a branch after merging
git branch -d feature/login

The feature branch workflow

1. git switch main && git pull    # start fresh
2. git switch -c feature/my-feature
3. # ... make changes, commit often
4. git push origin feature/my-feature
5. Open a Pull Request on GitHub
6. Get reviewed & approved
7. Merge PR β†’ main on GitHub

Merge conflicts

A conflict happens when two branches changed the same line differently. Git marks the conflict in the file:

<<<<<<< HEAD
My version of the code
=======
Their version of the code
>>>>>>> feature/their-branch

You manually edit the file to keep what you want, then git add and git commit.

Tasks
Preview