Questions tagged [git-detached-head]

Use this tag for questions about a Git repository in "detached HEAD" mode. A repository is in this mode if `git status` says "HEAD detached at ..." or "HEAD detached from ..."

In a repository, a detached HEAD is a mode where HEAD contains a raw commit hash ID. This mode or state is perhaps best defined by contrasting it to the more typical attached HEAD state. When a repository's HEAD is attached, the special file .git/HEAD contains the name of the current branch. This results from running git checkout branch-name, which puts you on the branch named branch-name. That is, your HEAD is normally attached to some branch, so that Git knows which branch you're on. But Git's HEAD is easily detached from this branch, so that it can move to another branch, or even directly to a commit.

When in this detached HEAD mode, Git will tell you that you are not on any branch. For instance, running git branch may print:

* (HEAD detached at 3e5524907)
  master

and git status will say HEAD detached ..., rather than on branch ... (see ).

A detached HEAD is not an error state, but it is also not a typical mode in which one does work in the repository. You will most commonly see it in one of these situations:

  • during a paused interactive rebase (see ), when you choose to edit some commit(s);
  • during any rebase that pauses due to conflicts;
  • during normal operation of most submodules (see ).

You will also end up in "detached HEAD" state by checking out any commit using any name that is not itself a branch name. For instance, git checkout origin/master will often check out the same commit as git checkout master, but since origin/master is a remote-tracking name rather than a branch name, you will be in this "detached HEAD" state.

The simplest way to recover, if you are in this state unexpectedly—e.g., do not have a rebase to continue (if you are uncertain, use git status to find out)—is just to check out a branch by name, e.g., git checkout master. If you have made new commits in this state, however, consider creating a branch name to record them.

96 questions
5
votes
1 answer

git checkout refs/heads/master detaches HEAD

I have a local branch "master" tracking a remote branch "origin/master". When I checkout master like this: git checkout refs/heads/master I end up with a detached HEAD: Note: checking out 'refs/heads/master'. You are in 'detached HEAD' state. You…
Jan
  • 485
  • 3
  • 9
4
votes
1 answer

Git submodule detached head state

I've added 2 submodules to a project, subA and subB, in externals/subA and externals/subB. Today another team member committed his code and when it is pulled, both subA and subB show detached head status when using git status within externals/subA…
user9297918
4
votes
2 answers

How to continue project from specific commit and fix HEAD detached issue?

I checkouted specific commit from my project and continued from there, hoping that changes after that commit would be deleted, and that commit I checkouted would be new head. I commited new changes, but I can't push them. I'm still new to git. What…
Milorad Simic
  • 97
  • 1
  • 7
3
votes
2 answers

git HEAD detached *from* vs detached *at*

I am NOT new to git. I know what a detached HEAD is and that I can just checkout a new branch to move forward. But today I saw a detached from message instead of a detached at message. While trying to abort a merge in a repo with 2 submodules, one…
xdhmoore
  • 8,935
  • 11
  • 47
  • 90
3
votes
2 answers

Fixing detached head with a tag

Is it possible to fix a detached head with a tag? In other words, is it possible to prevent garbage collector deleting commits in a detached head with a tag at the last commit? Every other post that I've found talk about creating a branch in the…
Samuel
  • 12,073
  • 5
  • 49
  • 71
3
votes
4 answers

Push to master commit made on a detached head

I wanted to redo changes from a previous commit. Maxims-MacBook-Air:hellodebug.com maximveksler$ git log commit 7f9dd753d39fd65b4272af713ef9c07a9f84f016 Author: Maxim Veksler Date: Sun Dec 28 09:12:17 2014 +0200 Imagine…
Maxim Veksler
  • 29,272
  • 38
  • 131
  • 151
2
votes
1 answer

Set an existing branch to detached HEAD (after `git rebase --onto`)

When I have a detached HEAD that's several commits ahead of an existing branch, what's the right command(s) to advance that branch to the detached HEAD without changing anything in my working directory? Context: I just ran git rebase --onto mybranch…
Justin Grant
  • 44,807
  • 15
  • 124
  • 208
2
votes
1 answer

Is there a way to undo detached HEAD state without undoing the work I've done while in detached HEAD state?

I'm in a detached HEAD state but have made a bunch of commits while in detached HEAD that I can't lose. Is there a way to undo the detached HEAD state without losing any of my work?
gkeenley
  • 6,088
  • 8
  • 54
  • 129
2
votes
1 answer

git submodule replacement that doesn't detach heads

My situation: I have a large number of computers that I use for various tasks. I have a large number of libraries, each in its own git repo. My desire: I want to be able to modify one of the libraries on any computer, do a git commit/push; then go…
2
votes
1 answer

What does the environment variable IGNORE_NORMALISATION_GIT_HEAD_MOVE do?

I have a C# code base that uses GitVersion.MsBuild. Sometimes my Jenkins build server gives me this error: GitVersion.BugException: GitVersion has a bug, your HEAD has moved after repo normalisation. To disable this error set an environmental…
Claus Appel
  • 1,015
  • 10
  • 28
2
votes
2 answers

Restoring accidentally lost commit due to detached Head

I lost my code after switching to another branch (thought that I pushed it, but head was detached) I switched from master to origin/somebranch Did some work there Then I had to quickly fix something in master, so I did git add -A git commit -m "some…
John W
  • 151
  • 6
2
votes
2 answers

how to commit changes in detached HEAD to a new branch in git

I had a tag, at a very old revision of a repository. I have checked it out git checkout mytag Now I am in detached HEAD state. I have made some meaningful modifications. I would like to create a new branch mybranch_deviated locally and…
00__00__00
  • 4,834
  • 9
  • 41
  • 89
2
votes
1 answer

how to rebase changes in the master to detached HEAD safely

I did not realize I've been working on a detached HEAD (a32b42b123) branch until now. This branch is falling behind master a lot. I did the following operations, git checkout master && git pull origin master git checkout a32b42b123 && git rebase…
user2372074
  • 781
  • 3
  • 7
  • 18
2
votes
0 answers

Git pull shallow clone after detached HEAD

Let us consider two following cases. Case #1 (shallow clone). $ git clone --depth 5 ssh://{REMOTE_URL}/project.git ... $ git checkout fd459887439f2cf93725c2f4a1a39997e865a86d // it is just a latest commit (took it from "git rev-parse HEAD") ... $…
Alexander Samoylov
  • 2,358
  • 2
  • 25
  • 28
2
votes
1 answer

How to list git branches when in detached HEAD state?

Normally, I can list local branches with git branch, and remote branches with git branch -r. However, these commands don't work with detached HEAD - I get fatal: HEAD does not point to a branch (presumably because git branch tries to determine…
Jan Warchoł
  • 1,063
  • 1
  • 9
  • 22