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
2
votes
2 answers

Git - submodules HEAD is always detached after running update?

I have this configuration for submodule in .gitmodules file: [submodule "sub"] shallow = true branch = master path = sub url = https://path/to/repo.git Now I want when someone clones my repo and then runs these commands: git…
Andrius
  • 19,658
  • 37
  • 143
  • 243
2
votes
4 answers

How to commit files in detached head mode to a branch

I ended up in a weird situation where I checkout a previous commit on my branch v4.1.0-rc12. I've modified some files around and I want to commit it to the v4.1.0-rc12 branch. Now that HEAD pointer is in detached mode since I checked out a previous…
Samuel Robert
  • 10,106
  • 7
  • 39
  • 60
2
votes
1 answer

Detached HEAD in Visual Studio

Is there a way to get into a detached HEAD state using Visual Studio (2015 on Win10 x64, specifically) without having to go to the command line and do it manually? Edit: to give context, a) I'm new to git, coming from TFVC world, and b) I'm trying…
mlhDev
  • 2,235
  • 1
  • 22
  • 43
2
votes
3 answers

How to avoid getting into detached head state in git?

I am getting into detached state head state all the time. I don't know why and I don't know what it is. Every time I am trying to recover from this state, I am loosing some files (from last commit I did in detached head state). Is it possible to…
Dims
  • 47,675
  • 117
  • 331
  • 600
2
votes
2 answers

Git: Checkout certain commit: "detached head" state

I want to go back to a certain commit (not just one file; the whole project). I tried: git checkout 0780033 but then I got the following message: You are in "detached head" state. You can look around... And then I am not in actual Branch --> but…
lars111
  • 1,233
  • 4
  • 12
  • 21
2
votes
1 answer

How can I reset my branch to a commit I created while in detached-HEAD mode?

I've got no idea what the Git graph looks like in this instance, so before I accidentally lose any code, I figure I'll ask for help. I committed some code that broke things, so I checked out a previous commit to look at what went wrong. commit…
user3308774
  • 1,354
  • 4
  • 16
  • 20
2
votes
1 answer

Why is the Git remote/origins/master branch detached?

I have a strange git scenario: I've been developing on master, and ive noticed that when my CI clones down and attempts to modify a git repository, it doesn't see "master" in the refspec. Thus, the command git checkout master Fails. To fix this…
jayunit100
  • 17,388
  • 22
  • 92
  • 167
2
votes
3 answers

Git. How to checkout revision, which goes after HEAD?

What is the quickest way to checkout revision which is next (later in the time) to HEAD (when you are in detached head state)? This should be something like: git checkout HEAD~-1 but this command doesn't work. Also git log, which doesn't show next…
klm123
  • 12,105
  • 14
  • 57
  • 95
1
vote
1 answer

git/submodule: detached ahead after update --rebase

As per documentation man git submodule update, if run with an option --rebase then it should never leave submodules's HEAD in a detached state. --rebase This option is only valid for the update command. Rebase the current branch onto the…
TruLa
  • 1,031
  • 11
  • 21
1
vote
1 answer

Git Detached Head in Android Studio

I am using Git with Flutter and recently I noted that doing a commit I get the message about a Detached Head. I would like to remove the detached head situation. This was probably generated by the fact that I moved back from version 6.3.1…
1
vote
1 answer

Resolving Detached Head on Server in Copied "Test" Site and original "Live" Site

Introduction I am managing a Drupal 9.4 site as a novice GIT and composer user, having built it out using these tools and limiting plug-ins to only those I absolutely needed to build out the functionality of my site. I primarily see the need only…
duncanf
  • 11
  • 4
1
vote
0 answers

Detached Head state on the Git remote repository

When I clone an existing project, the project doesn't point to the tip of the branch, but to a commit, so the HEAD is detached. If I perform a "git checkout theMainBranch", that allows locally to work properly, but when I commit and push some…
PascalLyon
  • 11
  • 1
1
vote
2 answers

git cherry-pick says nothing to commit

(note: this is not a duplicate question, see explanation below) I first checkout master as a detached branch: % git checkout --detach master HEAD is now at fff9e1e687 modserver/go: skip Spotlight automod aspect ratio check for cheerio vids Your…
1
vote
2 answers

Git: If I've been working in detached head state for multiple commits, will I lose anything by getting out of it?

A while ago I checked out a previous commit, then checked out my most recent commit again. I didn't realize this at the time, but that left me in a detached head state. I've done multiple commits since then, and am still in the detached head state.…
gkeenley
  • 6,088
  • 8
  • 54
  • 129
1
vote
1 answer

How to go back to an old commit, make some changes and commit those changes at the old commit which also updates all the following commits

I was working on a file, I comitted some changes, then I realized that I want some of the changes I've made recently in an older commit, because it is a part of the older commit. So, I want to go back to the commit 2c57de7 and add some of the…
scarecrow
  • 57
  • 2
  • 8