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
2042
votes
30 answers

How do I fix a Git detached head?

I was doing some work in my repository and noticed a file had local changes. I didn't want them anymore so I deleted the file, thinking I can just checkout a fresh copy. I wanted to do the Git equivalent of svn up . Using git pull didn't seem to…
Daniel
  • 23,365
  • 10
  • 36
  • 34
149
votes
2 answers

Git create branch where detached HEAD is

I tried something like this: git branch temp to create a new branch but don't move the HEAD. But I get: # Not currently on any branch. I don't want to merge anything, I just want a new branch at the current HEAD.
bsky
  • 19,326
  • 49
  • 155
  • 270
20
votes
4 answers

How to save changes when in detached-head state?

Working with Git, I had to go back to a specific commit. I made some changes and I now want to commit them. What is a proper way of doing this? My project is now in detached-HEAD state. Will my changes be saved if I make a commit with git commit ?…
Nikolas
  • 2,322
  • 9
  • 33
  • 55
17
votes
4 answers

Programmatically check if HEAD is detached?

I want to know whether I'm in a "HEAD detached" state in a script. Currently I parse the output of git status but I dislike doing so because I've heard that it's a bad practice to parse Git output that's meant for human - I want a program-friendly…
iBug
  • 35,554
  • 7
  • 89
  • 134
14
votes
3 answers

How did I end up with a detached HEAD?

I checked out a commit/branch from master, and then checked out back to master and wrote something. After that, I committed it, but I ended up with a detached HEAD. Why? Here is what I did: Create a new project and create git repository. git…
guo
  • 9,674
  • 9
  • 41
  • 79
10
votes
1 answer

HEAD detached at the HEAD commit id, what's the meaning?

git status says: HEAD detached at e1997bd and git rev-parse HEAD says: e1997bd What's the meaning of this? I was thinking HEAD detached means HEAD is not pointing to to branch tip.
KcFnMi
  • 5,516
  • 10
  • 62
  • 136
9
votes
2 answers

git pull remote master in detached head

This has been bothering me about git. I have a repository with multiple remotes, and I need to apply hotfixes to the remotes' master branches. So, I try doing: git fetch remote1 master git checkout remote1/master But, I always end up in a…
user2659205
  • 157
  • 1
  • 1
  • 7
8
votes
2 answers

Detached HEAD after checking out a branch; how to `push`?

Here's what I did: Checked out a remote git repository. Added to the [remote "origin] section of .git/config: fetch = +refs/heads/release/BranchName:refs/remotes/origin/release/BranchName Checked out the corresponding branch: git checkout…
Michael
  • 5,775
  • 2
  • 34
  • 53
7
votes
4 answers

Why do I have a detached HEAD after checking out the most recent commit?

Recently, while working in a git repository, I wanted to view the code at an old commit (68cce45), so I did git checkout 68cce45 After viewing the changes, I wanted to return to the current version of the repository and keep working. Since 2bcfd11…
Trevor
  • 545
  • 4
  • 19
7
votes
2 answers

Can I push a commit made in detached HEAD state

Using git I would like to go into detached HEAD state and create a new commit. I'd then like to create a tag and push both the 'detached commit' and the tag to the remote repo. Is this possible? If I create the detached commit in my local repo and…
7
votes
2 answers

Can git log --decorate unambiguously tell me whether the HEAD is detached?

I know that, in Git parlance, "detached HEAD" corresponds to a state in which the symbolic reference HEAD is not pointing to any branch. I also know that git branch, for instance, will tell me whether I'm in detached-HEAD state, e.g. * (detached…
jub0bs
  • 60,866
  • 25
  • 183
  • 186
6
votes
1 answer

Switch to a remote branch getting detached head

Here is a list of all my branches: $ git branch -a * temp remotes/heroku/master remotes/origin/dev remotes/origin/master When I type git checkout remotes/origin/master to switch to my origin master branch, Git switches to a detached HEAD…
5
votes
2 answers

Why recently, git rebase -i squash results in detached head

Why does git rebase -i with squashes recently result in a detached head? It used to to update my current branch with the result of the interactive rebase. How do I get interactive rebase to stop going to detached HEAD? I've always used git rebase -i…
joseph
  • 2,429
  • 1
  • 22
  • 43
5
votes
1 answer

How to reconcile detached head and origin/master? Already checked out old hash

Intro and search So I think I have made a grave mistake and I am concerned. I have parsed the forums for detached heads and I have come close to finding an answer, but the scenarios are too specific and not applicable to where I am at. If you find a…
Jennings
  • 438
  • 5
  • 17
5
votes
3 answers

How to prevent commit in detached HEAD

Why does git allow you to commit to a detached head? Is there any pre commit hook that can disable it? What is the purpose? Many new developers do this and I'd like to find a way to disable it.
Ben
  • 16,124
  • 22
  • 77
  • 122
1
2 3 4 5 6 7