C.W.K.
Stream
Lesson 02 of 05 · published

git switch, checkout, and Detached HEAD

~20 min · switch, checkout, head

Level 0Untracked Rookie
0 XP0/47 lessons0/14 achievements
0/100 XP to next level100 XP to go0% complete

switch is the modern verb; checkout is overloaded

For years, git checkout did two completely unrelated jobs: switching branches and restoring files from history. That overloading is the source of countless beginner mistakes — typing git checkout file.js meaning "open this file" and accidentally discarding all uncommitted edits. Git 2.23 (2019) split these responsibilities into git switch (branches) and git restore (files). The new commands are clearer, safer, and what every modern guide should use.

git switch <branch> changes the current branch. git switch -c <branch> creates and switches in one step. git switch - jumps to the previous branch (like cd -). git switch --detach <commit> intentionally enters detached HEAD. The old git checkout still works and you will see it in old guides, but in new code, prefer switch.

Detached HEAD is the state where HEAD points directly at a commit instead of at a branch name. It is not an error; it is a valid state, used for inspecting old commits or building from a tagged release. The risk: any commits you make in this state are not on any branch. If you switch away, those commits become orphaned. The escape hatch: git switch -c rescue-branch at the detached commit creates a branch pointing right where HEAD is, anchoring the work.

Before you switch, Git checks the working tree. If switching would overwrite uncommitted changes, Git refuses. Two clean responses: commit (or amend) what you have, or stash it (git stash push -u). Some teams configure git config --global advice.detachedHead false to silence the verbose detached-HEAD message once they understand it.

Code

Modern branch switching·bash
# Switch to an existing branch
git switch feature/login

# Create AND switch in one step
git switch -c feature/profile

# Jump back to the previous branch
git switch -

# Switch to remote-tracking branch (auto-creates local tracking branch)
git switch feature/from-teammate

# Old equivalents (still valid, but use switch in new code):
git checkout feature/login
git checkout -b feature/profile
Detached HEAD: safe entry, safe exit·bash
# Inspect an old release without leaving a branch
git switch --detach v1.4.0
# Now HEAD points at the v1.4.0 commit, no branch

# Made experimental commits while detached?
git log --oneline -5

# Anchor those commits before switching away:
git switch -c rescue/v1.4-investigation

# Already switched away and lost the tip?
git reflog              # find the dangling commit
git switch -c rescue HEAD@{2}

External links

Exercise

In a scratch repo with two branches and at least three commits each, run git switch back and forth. Then git switch --detach <old-commit>, make a new commit. Run git status and read what it says about HEAD. Anchor your new commit by running git switch -c experiment. Confirm the commit is now on the new branch. Write two sentences on what would have happened if you had switched away without anchoring.

Progress

Progress is local-only — sign in to sync across devices.
Spotted a bug or have feedback on this page?Report an Issue

Comments 0

🔔 Reply notifications (sign in)
Sign inPlease sign in to comment.

No comments yet — be the first.