How to Fix Git 'Detached HEAD' State
Git's 'detached HEAD' state for beginners, on a real repository: what HEAD is (the .git/HEAD file naming a branch, or a commit when detached), how a tag, a commit ID or origin/main puts you there, why a commit made there vanishes from git log when you switch away, how git reflog and git branch get it back, and how git switch -c keeps you from losing one.
Where did the commit go?
Git says you are in detached HEAD state, and the commit you made there seems to disappear. It almost certainly hasn't, and your fix is coming right up. Jess keeps a steam railway's timetable website. When she committed a fix, Git printed detached HEAD where a branch name usually goes, then the commit's ID. To be fair, Git had warned her. In a paragraph.
![Jess's commit, as Git printed it: diagram of [detached HEAD ea050c9], detached HEAD = no branch, ea050c9 = commit ID, Not gone: it can come back](/img/how-to-fix-git-detached-head-state/hook.jpg)
Then she switched back to her main branch. Git said she'd left one commit behind, and her history graph no longer shows it. So where did it go? First, the fixes.
git switch mainWarning: you are leaving 1 commit behind, not connected to any of your branches: ea050c9 Fix the Sunday departure time If you want to keep it by creating a new branch, this may be a good time to do so with: git branch <new-branch-name> ea050c9 Switched to branch 'main' Your branch is up to date with 'origin/main'.
git log --oneline --graph --all* 78c1bc2 (HEAD -> main, origin/main, origin/HEAD) Add the booking page * 7c68053 Add the Santa Specials page * 2289b49 (tag: v1.2) Winter timetable * 6d6b645 (tag: v1.1) Add Sunday trains * a461fa3 (tag: v1.0) First timetable
Haven't committed anything? git switch main takes you back, and nothing is lost. Committed, and still detached? Run git switch -c, then a new branch name. That creates a branch that keeps those commits. Already left, like Jess? Run git reflog, Git's record of everywhere HEAD has been, to find the commit's ID. Then run git branch with a new name and that ID.

What HEAD points to
So what is HEAD? Picture a book with a ribbon bookmark sewn in. A branch is the ribbon: every new commit moves it forward a page. HEAD is your finger. Normally your finger rests on the ribbon, so it moves along with it. Detached HEAD is your finger pressed on one page, off the ribbon. You can still write new pages there, but the ribbon doesn't follow them.

Here's Jess's history: five commits, each shown by the first seven characters of its ID. Three carry tags. A tag is a fixed label on one commit, and unlike a branch it never moves. v1.2 is the winter timetable, printed on the station's posters. Git keeps HEAD in a tiny file, and it just names a branch: main. Each branch is a tiny file too. Main's file holds one full commit ID, the newest one.
git log --oneline --decorate78c1bc2 (HEAD -> main, origin/main, origin/HEAD) Add the booking page 7c68053 Add the Santa Specials page 2289b49 (tag: v1.2) Winter timetable 6d6b645 (tag: v1.1) Add Sunday trains a461fa3 (tag: v1.0) First timetable
cat .git/HEADref: refs/heads/main
cat .git/refs/heads/main78c1bc23c42af60a7caf50d3683d72bde898967f
Her fellow volunteer Joe asks what the posters say for Sunday, so Jess checks out version one point two, which puts her files back as they were at that tag. Git answers with a whole note. In part, it says: you are in detached HEAD state. You can look around and make experimental commits, and throw them away by switching back. And in the middle, it already names the fix, git switch -c.
git checkout v1.2Note: switching to 'v1.2'. 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 2289b49 Winter timetable
Here's what changed in that tiny file. It no longer names a branch. It holds the commit's full ID instead. That's all detached means: HEAD points straight at a commit, not at a branch.

git status says it plainly: HEAD detached at v1.2. git branch normally stars the branch you're on. Here the star sits on a placeholder in brackets, because she's on no branch at all. And the file says the Sunday train leaves at 11:30. The real winter train leaves at 11:45.
git statusHEAD detached at v1.2 nothing to commit, working tree clean
git branch* (HEAD detached at v1.2) main
cat timetable.html<h1>Oak Hill Steam Railway: winter</h1> <p>Saturday: 10:15 Oak Hill to Millbrook</p> <p>Sunday: 11:30 Oak Hill to Millbrook</p> <p>Last train: 15:40</p>
So she edits that line right here, and commits: dash a takes every changed file, dash m adds the message. It works. The last three commits show the fix sitting on top of version one point two. But look at the brackets on the fix's line: only HEAD points at it. No branch does.
git commit -am 'Fix the Sunday departure time'[detached HEAD ea050c9] Fix the Sunday departure time 1 file changed, 1 insertion(+), 1 deletion(-)
git log --oneline --decorate -3ea050c9 (HEAD) Fix the Sunday departure time 2289b49 (tag: v1.2) Winter timetable 6d6b645 (tag: v1.1) Add Sunday trains
Here's what happened when she left. Right after her commit, HEAD was the only thing pointing at the fix. When she switches to main, HEAD moves onto the main branch. Nothing points at the fix now. No branch, no tag, and not HEAD. git log starts from those names and follows each commit back to the one before it, so it never reaches the fix. That's why the fix seems to vanish.

Getting the commit back
Back to Jess's missing fix. Her history graph doesn't show it, because it follows branches and tags. But Git keeps a second diary, called the reflog: every place HEAD has been. The top line is her switch to main. The next line is her commit, with its ID.
git reflog -378c1bc2 (HEAD -> main, origin/main, origin/HEAD) HEAD@{0}: checkout: moving from ea050c9a14c3a6b6872ef6ef2a2acc407bc01cd1 to main ea050c9 HEAD@{1}: commit: Fix the Sunday departure time 2289b49 (tag: v1.2) HEAD@{2}: checkout: moving from main to v1.2
How long is it safe there? Git's manual says a commit with nothing pointing at it is eventually deleted. That's garbage collection, Git's routine cleanup. Give it a name first, and it's safe. While the reflog still lists the commit, Git won't delete it, and the git gc page gives the time. Guess first: how long does Git keep a reflog entry like Jess's? Thirty days, by default. So rescue a lost commit soon.

Jess checks it's the right commit: git show lists one file changed, the timetable. Then she runs git branch sunday-fix, followed by that ID. Git prints nothing, but now a name points at the fix, and the graph draws it again, branching off version one point two.
git show --stat --oneline ea050c9ea050c9 Fix the Sunday departure time timetable.html | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
git branch sunday-fix ea050c9
git log --oneline --graph --all* 78c1bc2 (HEAD -> main, origin/main, origin/HEAD) Add the booking page * 7c68053 Add the Santa Specials page | * ea050c9 (sunday-fix) Fix the Sunday departure time |/ * 2289b49 (tag: v1.2) Winter timetable * 6d6b645 (tag: v1.1) Add Sunday trains * a461fa3 (tag: v1.0) First timetable
She's back on main, so merging the new branch brings the fix in. One file changed, and a search of main's timetable for Sunday now finds 11:45.
git merge sunday-fixMerge made by the 'ort' strategy. timetable.html | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
grep Sunday timetable.html<p>Sunday: 11:45 Oak Hill to Millbrook</p>
Other ways in
Maybe you never checked out a tag, and you're still detached. Here's how that usually happens. Check out a commit ID, maybe to look at an old version, and HEAD detaches. git switch -, a single dash, takes you straight back. Check out origin/main, your read-only copy of the server's branch, and it detaches again. And during a rebase, which replays your commits onto a newer starting point, Git detaches HEAD for a moment on its own, then puts it back. That one needs nothing from you.
git checkout 2289b49Note: switching to '2289b49'. 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 2289b49 Winter timetable
git switch -Previous HEAD position was 2289b49 Winter timetable Switched to branch 'main' Your branch is up to date with 'origin/main'.
git checkout origin/mainNote: switching to 'origin/main'. 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 78c1bc2 Add the booking page
git statusHEAD detached at origin/main nothing to commit, working tree clean
Never lose one again
So how do you never lose one? First, the newer command, git switch, is stricter. Ask it for a tag, and it refuses: a branch is expected. The hint says add --detach if you really mean it. That refusal is a good thing. You can't end up detached by accident.
git switch v1.2fatal: a branch is expected, got tag 'v1.2' hint: If you want to detach HEAD at the commit, try again with the --detach option.
Better still, start on a branch. git switch -c poster-fix v1.2: dash c creates a branch called poster-fix, starting at the poster version, and puts HEAD on it. The HEAD file names that branch, so every commit from here moves the ribbon.
git switch -c poster-fix v1.2Switched to a new branch 'poster-fix'
cat .git/HEADref: refs/heads/poster-fix
Recap
So: detached HEAD means HEAD names a commit, not a branch. Nothing committed? git switch main. Committed? git switch -c and a new name, before you leave. Already left? git reflog, then git branch.

If this got a commit of yours back, subscribing brings more Git fixes.
All the lab files and commands are linked in the description. Thanks for watching Cloud Explainers.
Sources
Demos: one container on our own Mac standing in for a laptop, with a local shared repository; the railway and its timetable are made up. Every command, output and commit ID was real. Watch the full video on YouTube.