git checkout — Legacy Branch & Commit Inspection
Understanding legacy git checkout: checking out commits (detached HEAD state), legacy branch switching (-b), and modern replacements.
git checkout — Legacy Branch & Commit Inspection
While modern Git uses git switch and git restore, git checkout remains widely used for inspecting historical commits in Detached HEAD state.
1. Traditional Branch Switching with checkout
# Switch to existing branch:
git checkout main
# Create and switch to new branch (-b):
git checkout -b feat/user-auth2. Inspecting Historical Commits (Detached HEAD)
You can check out any historical commit SHA hash to inspect or build older states of your application:
git checkout 39efceaOutput:
Note: switching to '39efcea'.
You are in 'detached HEAD' state. You can look around, make experimental
changes and commit them, and you can discard any commits you make in this
state without impacting any branches.What is "Detached HEAD"?
Normally, HEAD points to a named branch pointer (e.g. main), which moves forward with each commit. In detached HEAD state, HEAD points directly to a raw commit SHA. Any new commits made in detached HEAD will be lost when you switch back to main unless you create a branch:
# Save detached HEAD experiments into a named branch:
git switch -c my-saved-experiment
# Or return safely to main:
git switch mainRelated Topics
git switch — Modern Branch Switching
How to switch branches cleanly in modern Git: creating and switching (-c), switching to existing branches, and why switch replaces checkout.
git merge — Combining Branch Histories
How to merge branches in Git: Fast-Forward merges, 3-way merge commits (--no-ff), squash merging (--squash), and resolving divergence.