What is git checkout?
Why Interviewers Ask This
This question tests conceptual clarity. Interviewers want to hear a precise, confident definition before moving to more complex Git & GitHub topics. It also reveals how well you can explain technical ideas to non-experts.
Answer
git checkout is one of Git's most versatile commands with multiple purposes. Main uses: (1) Switch branches: git checkout main — move to main branch; Git updates the working directory to match; (2) Create and switch: git checkout -b feature-x — create and immediately switch to a new branch; (3) Checkout a specific commit (detached HEAD): git checkout abc1234 — view the repo at that point in history; (4) Restore files: git checkout -- file.txt (legacy) or git restore file.txt (modern) — discard working directory changes to a file, reverting to the staged or last-committed version. Irreversible — discards unsaved changes! (5) Checkout a file from another branch: git checkout feature -- src/utils.js — bring a specific file from another branch into current working directory. Git 2.23+ split checkout's concerns into: git switch (for changing branches) and git restore (for restoring files) — clearer semantics. git checkout still works for backward compatibility but the newer commands are recommended for clarity.
Pro Tip
This topic has Git & GitHub-specific nuances that differ from general programming. Highlighting those nuances in your answer shows expertise rather than generic knowledge.