Branching and Merging
Branches are Git's killer feature — they let you diverge from the main line of development and work in isolation.
Creating branches
git branch feature/login # create
git checkout feature/login # switch (old syntax)
git switch feature/login # switch (modern syntax)
git checkout -b feature/login # create + switch in one step
Merging
git checkout main
git merge feature/login
Git will create a merge commit that has two parents — combining the histories.
Fast-forward vs merge commit
- Fast-forward: no divergence; Git just moves the pointer forward
- 3-way merge: diverged histories; creates a new merge commit
Best practices
- Keep branches short-lived (hours/days, not weeks)
- Use descriptive names:
feature/oauth-login,fix/null-pointer-auth - Delete branches after merging:
git branch -d feature/login