Branches
A branch is just a pointer to a commit. Creating one is instant and cheap.
git branch feature/auth # create branch
git switch feature/auth # switch to it
git switch -c feature/auth # create + switch in one step
git branch -d feature/auth # delete (after merge)
git branch -D feature/auth # force deleteMerging
git switch main
git merge feature/auth # merge feature into mainFast-forward vs merge commit
Fast-forward (no divergence):
main: A ← B ← C ← D(feature)
After: main = D
Three-way merge (diverged):
main: A ← B ← E
feature: A ← B ← C ← D
After: A ← B ← E ← M(merge commit)
↗
C ← D
Resolving Conflicts
When two branches edit the same lines Git pauses the merge:
<<<<<<< HEAD
const port = 3000;
=======
const port = 8080;
>>>>>>> feature/config
Edit the file to the correct version, then:
git add src/config.ts
git commit # completes the mergeRebase
Rebase replays your commits on top of another branch, producing a linear history.
git switch feature/auth
git rebase main # replay feature commits on top of mainNever rebase commits that have already been pushed to a shared branch. It rewrites history and causes problems for teammates.
Git Flow (simplified)
main ←─────────────────── production-ready only
develop ←── integration branch
feature/* ←── one branch per feature, merges into develop
hotfix/* ←── branch off main, merge to both main + develop