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
1
vote
1 answer

Recover lost data GIT detached head

Im not very good at GIT (I dont really undestarnd this HEAD and "master" things) and I've just lost my code. Can anyone help me to retrieve it? My steps were like: I added some code and I wanted to commit it A window popped up with a message like…
Przemek
  • 59
  • 4
1
vote
2 answers

Git started detaching head when viewing any previous commits on a branch?

I'm a newer git user so this maybe a dumb question, but all the sudden whenever I checkout any previous commit with something like git checkout 050aa9f in my Development branch, git immediately detaches the head: You are in 'detached HEAD' state.…
Jhon Piper
  • 513
  • 2
  • 8
  • 21
1
vote
3 answers

How to git-commit from a branch to a detached HEAD?

My ref HEAD points the branch foo, that points to the commit 123abc; and I have some staged work. How to git commit that work, moving the HEAD to the newly created commit, but without advancing the branch? (hence: leaving foo point to 123abc) Is it…
Kamafeather
  • 8,663
  • 14
  • 69
  • 99
1
vote
2 answers

What is difference between git checkout remotes/origin/branch and git checkout branch?

When I execute git checkout remotes/origin/test_branch my HEAD goes in a detached state. Below is output: C:\..\git_test>git checkout remotes/origin/test_branch Note: checking out 'remotes/origin/test_branch'. You are in 'detached HEAD' state.…
rns
  • 1,047
  • 10
  • 25
1
vote
2 answers

Strange branch name after creating branch from detached head

I had a problem with a detached head in my Git. I did checkout on the previous commit. After that, we had made commits. So after I created branch for this commits. git checkout -b detached-head-after-gitlab-crush So after that, I made changes and…
ardakshalkar
  • 625
  • 2
  • 7
  • 21
1
vote
1 answer

gitpython get last commit with detached head

In a up-to-date repository we do the following: git checkout HEAD~5 Then using GitPython, we can obtain the head commit, which is detached: import git repo = git.Repo('.') head = repo.head head_commit = head.commit print(head.is_detached) >…
jabozzo
  • 591
  • 1
  • 6
  • 17
1
vote
2 answers

GIT Detached head issue on a branch

I created a branch for my change and did a lots of commit over there. But then I wanted to rollback my latest commit. sO i did following 1. I checkout the previous commit with git checkout on my branch. 2. Then I did my changes to local directory…
1
vote
2 answers

Detached Head while building pipeline on Azure DevOps

I notice the following during the pipeline build stage for my git branch. I however get no 'HEAD DETACHED' error when I checkout the branch in my dev environment. Is this error significant or is it just something that is to be expected for all git…
Chubsdad
  • 24,777
  • 4
  • 73
  • 129
1
vote
2 answers

After git reset --hard, why didn’t my topic branch change?

I accidentally rebased my branch with the DEV branch and then pushed it to a remote repository. With the rebase, I selected the current changes and hence my local changes got overwritten. I lost my earlier commit in the rebase but found it by…
Saurabh Tiwari
  • 4,632
  • 9
  • 42
  • 82
1
vote
0 answers

Detached HEAD in android studio

I am using TFS plugin in android studio. my last commit was buggy an I want to get back to last well commit. I go to version control and select Checkout Revision and that put me in Detach head state. I do not know about git command line and only…
faeze saghafi
  • 650
  • 10
  • 25
1
vote
1 answer

Go back two commits but keep the two last in new branch

I want to go back two commits, which would lead to a detached head, with two commits after HEAD. However I would like these two commits to keep them in a new branch, and master pointing to the checked out commit. How should I do this?
luis.ap.uyen
  • 1,314
  • 1
  • 11
  • 29
1
vote
1 answer

Fix detached head without losing commits made while detached

I'm currently having a detached HEAD after doing a rebase. I did the rebase as I thought this would solve my main problem: a remote repo that thinks it's up to date with the local repo (but in fact isn't). So here are my latest commits: a65597d…
Ansjovis86
  • 1,506
  • 5
  • 17
  • 48
1
vote
1 answer

Fix Git Branching with Detached Head and Unrelated Histories

I need to fix a Git repository that has a detached head and 2 consecutive branches with unrelated histories. I have to use SourceTree at work, but I can use command lines too if needed. What I have to do is to reconcile the current [Head] (3 on the…
1
vote
1 answer

Can't 'reattach' detached HEAD git

I had a branch bug-fix that was cut off of another branch develop After merging my changes from bug-fix into develop, I deleted the branch through github, however I believe I was still checked into the branch when I deleted it. I also had one…
lmo523
  • 459
  • 1
  • 7
  • 18
1
vote
3 answers

git HEAD detached from after reset --hard

I needed to do away with changes that I made to the working directory and go back and start from the last commit, so I did git reset --hard Now when I do git commit and git status I get this string in red saying "HEAD detached from: and some 7 char…
Fred J.
  • 5,759
  • 10
  • 57
  • 106