PricingSign inDownload

Blog / git

Coding agent committed on a detached HEAD? Get it back

An agent checked out a commit and kept working, so its commits are on no branch. Find them with git reflog, put them on a branch, and stop it happening again.

On this page
  1. What a detached HEAD is
  2. If you are still on the detached HEAD
  3. If you already switched away
  4. Put the commits on your branch
  5. If the reflog doesn't have them
  6. Keep the agent off a detached HEAD
  7. Where Joxo fits
  8. Frequently asked questions
  9. Are commits on a detached HEAD lost when I switch branches?
  10. How do I find commits made on a detached HEAD?
  11. How do I move commits from a detached HEAD onto main?
  12. How do I stop a coding agent from working on a detached HEAD?
  13. Try it with your team

You asked your agent to look at an older version, or to "go back to the last working commit". It ran git checkout <commit>, then kept working and committed twice. Later it, or you, ran git switch main, and git printed this:

Warning: you are leaving 2 commits behind, not connected to
any of your branches:

  c96874b Add d (agent)
  4f58771 Add c (agent)

The commits are not on any branch, so git log and git log --all don't show them. They are not gone. Here is how to get them back, and how to keep an agent off a detached HEAD. We ran every command with git 2.43 on a scratch repository.

What a detached HEAD is#

Normally HEAD points at a branch, and each commit moves that branch forward. After git checkout <commit>, git checkout HEAD~1 or git checkout v1.2, HEAD points at a commit directly. You can still commit, but no branch moves, so as soon as you switch away nothing points at the new commits.

git status says so on its first line:

HEAD detached at 11b6d4c

If you are still on the detached HEAD#

Nothing is lost yet. Make a branch where you are, and the commits are on it:

bash
git switch -c agent/rescue

If you already switched away#

The warning above printed the hash: git branch agent/rescue c96874b saves the commits. If the warning has scrolled off, git reflog lists every place HEAD has been, newest first:

bash
git reflog -6
8febbff HEAD@{0}: checkout: moving from c96874bb8d845f607e2c1c2c705e73a5ab036eeb to main
c96874b HEAD@{1}: commit: Add d (agent)
4f58771 HEAD@{2}: commit: Add c (agent)
11b6d4c HEAD@{3}: checkout: moving from main to HEAD~1
8febbff HEAD@{4}: commit: Add b
11b6d4c HEAD@{5}: commit (initial): Add a

Read it from the bottom up: the move to HEAD~1 is where the agent detached, the two commit: lines are its work, and the top line is the switch back to main. The newest of the agent's commits, c96874b, is the one to save:

bash
git branch agent/rescue c96874b
git log --oneline main..agent/rescue
c96874b Add d (agent)
4f58771 Add c (agent)

HEAD@{1} works in place of the hash, but only until HEAD moves again, so the hash is safer.

Put the commits on your branch#

Now that the work has a branch, bring it onto main (or the branch you meant it for). cherry-pick with a range copies every commit on agent/rescue that main doesn't have, oldest first:

bash
git switch main
git cherry-pick main..agent/rescue
git log --oneline
9b4c891 Add d (agent)
8278e7e Add c (agent)
8febbff Add b
11b6d4c Add a

The copies get new hashes, which is expected. Once they are where you want them, git branch -D agent/rescue removes the rescue branch. If the agent's commits started from an old commit and main has changed the same lines since, cherry-pick stops on a conflict: fix the files, git add them and run git cherry-pick --continue.

If the reflog doesn't have them#

The reflog lives in your .git folder and keeps entries for about 90 days, or 30 for commits on no branch. If it was cleared, or the agent worked in a clone that has since been deleted, ask git for commits nothing points to:

bash
git fsck --lost-found
dangling commit 215bd68e904974ca480e4de31e972272fe9a9848

Check each one with git show <hash> and branch the right one. These survive until git's garbage collection removes them, so do this soon. If the clone itself is gone, so are the commits: see what you can get back after git reset --hard for the other ways work goes missing.

Keep the agent off a detached HEAD#

Agents detach for good reasons: to run the tests on an old commit, or to find where something broke. The fix is to give them a way that doesn't need it. Add to your project instructions:

"To look at an older commit, use git show <commit> or git worktree add ../old <commit>; don't check it out in this folder. Never commit while git status says HEAD detached; make a branch first with git switch -c."

A worktree gives the agent a second folder at the old commit, so your folder stays on its branch. Remove it afterwards with git worktree remove ../old (see removing a worktree while an agent runs). If the agent is looking for the commit that broke something, git bisect detaches on purpose and returns you to your branch at the end with git bisect reset.

To make sure nobody commits on a detached HEAD, a pre-commit hook can refuse it. Save this as .git/hooks/pre-commit and make it executable with chmod +x:

bash
#!/bin/sh
git symbolic-ref -q HEAD >/dev/null || {
  echo "HEAD is detached: run git switch -c <branch> first" >&2
  exit 1
}

git symbolic-ref -q HEAD fails only when HEAD isn't on a branch. An agent can still skip hooks with --no-verify, so see stop a coding agent skipping git hooks too.

Where Joxo fits#

When your agent and a teammate's work on one project, a stray commit on no branch is easy to miss until someone asks where the work went. With Joxo every agent's task and handoff note shows on one board, so "I finished it" comes with where it is. Setup is one line pasted into your agent.

Frequently asked questions#

Are commits on a detached HEAD lost when I switch branches?#

No. Git keeps them, but no branch points at them. Find them with git reflog and run git branch <name> <hash> to save them.

How do I find commits made on a detached HEAD?#

Run git reflog. Look for the commit: lines after a checkout: moving from <branch> to <commit> line. The newest of them is the one to put a branch on.

How do I move commits from a detached HEAD onto main?#

Put a branch on them first with git branch rescue <hash>, then from main run git cherry-pick main..rescue.

How do I stop a coding agent from working on a detached HEAD?#

Ask it in your project instructions to use git show or git worktree add to look at old commits, and add a pre-commit hook that fails when git symbolic-ref -q HEAD fails.