The Detached HEAD Escape Plan

Three real commits, made in detached HEAD. Rescued before switching away would strand them.

Checking out a specific real commit hash directly — instead of a branch name — genuinely detaches HEAD: it now points straight at that commit, not at any branch. New real commits made from here still get made, completely normally, but no branch pointer follows along.\n\nSwitching to a real branch afterward leaves those detached commits referenced by nothing but reflog — still real, still on disk, but one real garbage collection away from disappearing for good. Creating a genuine new branch at that exact point, before switching away (or immediately after, via reflog), is the standard, real escape plan.

Entering Detached HEAD

git checkout <commit-hash>

HEAD now points directly at that real commit, not at any branch. git warns about this on the real terminal, but nothing is actually wrong yet.

Real Work, No Branch Following It

git commit -m "..."

Real, genuine commits — they exist on disk exactly like any other. No branch pointer moves with them, though.

The Real Escape

git switch master
git switch -c rescued-work HEAD@{1}

HEAD@{1}, run right after switching away, is exactly the tip of the detached work. A genuinely new branch there keeps every real commit safe.

Detached HEAD

A real, valid git state where HEAD points directly at a commit instead of a branch. Confirmed directly in this lab: commits made there are completely real and functional, they simply have no branch pointer following them.

git switch -c <branch> <ref>

Creates a genuinely new branch at any real ref — including a reflog reference like HEAD@{1} — and switches to it in one step. Confirmed directly in this lab: it captured all three real detached commits onto a new, permanent branch pointer.

🪂 Detach on Purpose

Check out a real, specific commit hash directly.

cd ~/team-recipes && git log --oneline -3
cd ~/team-recipes && git checkout $(git log --format=%H -3 | tail -1)

student@lab:~$ cd ~/team-recipes && git log --oneline -3 0062366 (HEAD -> master) Add air fryer fries recipe c620524 Add instant pot risotto recipe 7fb687e Add slow cooker chili recipe student@lab:~$ cd ~/team-recipes && git checkout $(git log --format=%H -3 | tail -1) Note: switching to '7fb687e268d1d10bfba20f753efb8ba9d25101f7'. 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 by switching back to a branch. If you want to create a new branch to retain commits you create, you may do so (now or later) by using -c with the switch command. Example: git switch -c <new-branch-name> Or undo this operation with: git switch - Turn off this advice by setting config variable advice.detachedHead to false HEAD is now at 7fb687e Add slow cooker chili recipe

📝 Three Real Commits, Detached

Real commits, made anyway — no branch pointer follows them.

cd ~/team-recipes && echo 'Emergency Snack' > emergency-snack.md && git add emergency-snack.md && git commit -m 'Add emergency snack idea'
cd ~/team-recipes && printf '\nToast with peanut butter.\n' >> emergency-snack.md && git add emergency-snack.md && git commit -m 'Add second emergency snack idea'
cd ~/team-recipes && printf '\nTrail mix from the pantry.\n' >> emergency-snack.md && git add emergency-snack.md && git commit -m 'Add third emergency snack idea'

student@lab:~$ cd ~/team-recipes && echo 'Emergency Snack' > emergency-snack.md && git add emergency-snack.md && git commit -m 'Add emergency snack idea' [detached HEAD 40730ae] Add emergency snack idea 1 file changed, 1 insertion(+) create mode 100644 emergency-snack.md student@lab:~$ cd ~/team-recipes && printf '\nToast with peanut butter.\n' >> emergency-snack.md && git add emergency-snack.md && git commit -m 'Add second emergency snack idea' [detached HEAD 9c968dc] Add second emergency snack idea 1 file changed, 2 insertions(+) student@lab:~$ cd ~/team-recipes && printf '\nTrail mix from the pantry.\n' >> emergency-snack.md && git add emergency-snack.md && git commit -m 'Add third emergency snack idea' [detached HEAD cf28c85] Add third emergency snack idea 1 file changed, 2 insertions(+)

⚠️ Switch Away — Don't Panic

Back on a real branch. The detached commits are still there, just unreferenced.

cd ~/team-recipes && git switch master
cd ~/team-recipes && git log --oneline -3

student@lab:~$ cd ~/team-recipes && git switch master Warning: you are leaving 3 commits behind, not connected to any of your branches: cf28c85 Add third emergency snack idea 9c968dc Add second emergency snack idea 40730ae Add emergency snack idea If you want to keep them by creating a new branch, this may be a good time to do so with: git branch <new-branch-name> cf28c85 Switched to branch 'master' Your branch is ahead of 'origin/master' by 114 commits. (use "git push" to publish your local commits) student@lab:~$ cd ~/team-recipes && git log --oneline -3 0062366 (HEAD -> master) Add air fryer fries recipe c620524 Add instant pot risotto recipe 7fb687e Add slow cooker chili recipe

🚑 Rescue Them

A genuinely new branch, at exactly the right real commit.

cd ~/team-recipes && git reflog | head -5
cd ~/team-recipes && git switch -c rescued-snacks HEAD@{1}
cd ~/team-recipes && git log --oneline rescued-snacks -3

student@lab:~$ cd ~/team-recipes && git reflog | head -5 0062366 HEAD@{0}: checkout: moving from cf28c857a630ae7a4429ae3306b85466387003a4 to master cf28c85 HEAD@{1}: commit: Add third emergency snack idea 9c968dc HEAD@{2}: commit: Add second emergency snack idea 40730ae HEAD@{3}: commit: Add emergency snack idea 7fb687e HEAD@{4}: checkout: moving from master to 7fb687e268d1d10bfba20f753efb8ba9d25101f7 student@lab:~$ cd ~/team-recipes && git switch -c rescued-snacks HEAD@{1} Switched to a new branch 'rescued-snacks' student@lab:~$ cd ~/team-recipes && git log --oneline rescued-snacks -3 cf28c85 (HEAD -> rescued-snacks) Add third emergency snack idea 9c968dc Add second emergency snack idea 40730ae Add emergency snack idea

Lab 3.4.4 complete. A genuine detached-HEAD detour, three real commits, fully rescued:\n\n\n Detached HEAD : ✅ entered on purpose\n 3 real commits : ✅ made while detached\n Switched away : ✅ commits unreferenced\n git switch -c + HEAD@{1}: ✅ fully rescued\n

Enable JavaScript to run the live terminal and track your progress.