Recent chats
ASKGEEZ Q&A GUIDETechnology & devices

What does detached HEAD mean in Git, and how can I keep new work?

Quick answer

Normally, HEAD refers to a branch, which points to a commit. If you check out a commit or tag directly, HEAD points to that commit instead: Git calls this detached HEAD. Commits you make there are not added to a named branch and may become hard to find after you move away. If the work matters, create a branch at the current commit with git switch -c keep-work.[S1]

LET’S WORK THROUGH IT

The question, the details, the next step.

Question

What does detached HEAD mean? I checked out a commit by its ID and Git seems to say I’m not on a branch. Did I lose my work?

Geez

Detached HEAD means HEAD points directly to a commit rather than referring to a named branch. That can happen when you check out a commit ID or a tag. It is a Git state. The important distinction is that a new commit made in this state is not added to a named branch, so it may be hard to find after you move elsewhere.[S1]

If you are still at the commit containing the work you want, give that commit a branch name before leaving it. For example, git switch -c keep-work creates a branch at the current commit.[S1]

Follow-up question

I’ve made a commit while detached. What should I do now to keep it?

Geez

If you are still at that commit, create a branch there now: git switch -c keep-work. Choose a descriptive name if you prefer, such as experiment-notes. The key is to create the branch while the wanted commit is current; the new branch gives that commit a name you can return to.[S1]

For example, suppose your commit is a hypothetical change to a draft parser experiment. While that commit is checked out, run git switch -c parser-experiment. This preserves a reference to that commit; it does not say whether the example change has been tested or merged. If you have several detached commits, make sure the current commit is the one you intend to preserve: the command names the current point, not a different commit you have in mind.

Follow-up question

I already switched away from the detached commit. Is there still a recovery path?

Geez

There may be. The Git checkout manual describes using the reflog to recover the object name of a commit made while detached.[S1] The documentation available here does not give the exact commands or establish that every past commit can be recovered, so a particular result cannot be promised. If you locate the wanted commit, preserve it by creating a branch at that commit. If you are unsure which entry is yours, avoid guessing and consult Git’s documentation or someone familiar with your repository before changing state.

Follow-up question

Can you give me a simple checklist, including what to do before I leave detached HEAD?

Geez

Use this short sequence:

  1. Decide which commit contains the work you want to keep.
  2. If that commit is current, create a branch there with git switch -c keep-work (replace keep-work with your chosen name).[S1]
  3. If you already moved away, use the reflog as a possible way to find the detached commit’s object name; recovery is not guaranteed.[S1]
  4. Once you have identified the intended commit, give it a branch name rather than relying on remembering an unnamed commit.

Run the branch-creation command while the wanted commit is current. This guide covers preserving a reference to a commit; it does not verify the contents of your changes or establish whether they are suitable to share.

A practical checklist

  • Identify the commit containing the work you want to preserve.

  • If it is current, run git switch -c keep-work before moving away.

  • Choose a branch name that helps you recognize the work later.

  • If you already moved away, consider the reflog as a possible way to locate the commit.

  • Do not assume recovery is guaranteed; identify the intended commit before acting on it.

A few more useful answers

Does detached HEAD mean my commit is already lost?

No. It means HEAD points directly to a commit rather than a named branch. A commit made there is not added to a branch and may become hard to find after you move away.[S1]

What does `git switch -c keep-work` preserve?

It creates a branch at the current commit, giving that commit a named reference. Run it while the commit you want to keep is current.[S1]

Can the reflog always recover a detached commit?

The Git manual describes using the reflog to recover a commit’s object name, but does not establish that every commit is recoverable. Treat it as a possible recovery path, not a guarantee.[S1]

Sources

References used for this guide.

  1. [S1] Git git-checkout documentation: detached HEAD
    git-scm.com · Checked

YOUR QUESTION. YOUR DETAILS.

Let’s work through your situation.

Get a tailored answer or help with your next task. Starts at $1, with a quote before you pay.

Ask your own question
← Back to Geez at Work