Algocraft / Git Commands / git branch & checkout

🌿

git branch & checkout

A branch is just a movable pointer to a commit. HEAD tracks which one you're standing on — or lets you stand on a commit directly

Branch Pointer HEAD Detached HEAD
Commit Graph Step 0 / 0
Working Directory → Staging → Repository
Terminal Transcript

Key Concepts

🎯 When to Use
  • Starting any new piece of work in isolation
  • Inspecting an old commit without disturbing any branch
⚖️ Branch vs Checkout
  • git branch name: create the pointer, stay where you are
  • git checkout name: move HEAD to point at it
  • git checkout -b name does both in one step
  • Modern Git splits this into git switch / git restore
🎓 Pro Tips
  • Commit while detached and you must branch it or lose it to garbage collection
  • git branch -d refuses to delete unmerged work; -D forces it