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

Why is my local Git branch detached?

What's happening with my local Git branches? Here is the sequence of commands I ran: $ git fetch --all $ git branch * feature/myfeature1 master branch-dev $ git branch -a * feature/myfeature1 master branch-dev …
Pracede
  • 4,226
  • 16
  • 65
  • 110
1
vote
0 answers

Ran "git checkout HEAD^^" on branch B, and now "git checkout B" takes me to the older commit

I am making some additions to a code on branch atomic_boron. After running into a bug, I ran the command: git checkout HEAD^^ in order to get back to the (presumably) working state to make sure it actually did work. However, now whenever I…
1
vote
1 answer

rebase multiple feature branch

Me and my friends are collaborating a project (all amateurs) and we're having a hard time on git rebase. Our workflow is that we work on each of our own branches and every time someone finishes their work, they push it and send a pull request. After…
Finks
  • 1,661
  • 2
  • 16
  • 25
0
votes
1 answer

git/submodule: NOT a detached ahead after update --checkout

As per documentation man git submodule update, if run with an option --checkout then it should leave submodules's HEAD in a detached state. --checkout This option is only valid for the update command. Checkout the commit recorded…
TruLa
  • 1,031
  • 11
  • 21
0
votes
1 answer

Git: abandon bad commit and make HEAD the mainstream

I made some changes to this repository and checked them in and pushed (4326b5, "Waypoint"), before falling back to the previous well behaved commit, 64b7d01 "Merge commit". Then I made some changes from that point and they solved a lot of bugs I had…
Joymaker
  • 813
  • 1
  • 9
  • 23
0
votes
0 answers

Lost local files after git checkout

I detached git from VS Code accidentally. But there were a lot of new files on local and I didn't push them. Then I lost all of them and only got the last version I pushed on Git. Is there a way to recover the lost files? Really appreciate.
0
votes
0 answers

How to make a separate branch when in detached head state?

I took over a project in a detached HEAD state. I don't know for how many commits it's been in that state. I'm currently on main and want to create a new branch feature1. When I create this branch and then check back to main, most of the recent work…
gkeenley
  • 6,088
  • 8
  • 54
  • 129
0
votes
0 answers

HEAD is detached in the repository [repo_name] and no branch selected - NetBeans

I'm using NetBeans to develop a project in Java. Whenever my mate tries to push what he has done, it shows the error: HEAD is detached in the repository [repo_name] When I go to the Repository Browser in his program it shows that no branch is…
0
votes
2 answers

"HEAD" equivalent in detached HEAD state

Lets say I'm in a detached HEAD state and I run the following command: git push origin HEAD:foo git will output the following error: error: The destination you provided is not a full refname (i.e., starting with "refs/"). We tried to guess what you…
Simoyd
  • 506
  • 4
  • 11
0
votes
2 answers

What's the best way to find old detached HEAD after repo sync?

Say that there are some local commits(A, A1, A2, A3) on the current detached HEAD which points to commit A, after repo sync, the local detached HEAD is overridden with the remote latest detached HEAD which points to commit B, what's the best way to…
Chen Li
  • 4,824
  • 3
  • 28
  • 55
0
votes
2 answers

How to fix "Head detached from "?

I checked out a previous commit in my project recently, and just noticed when I ran git status that it was returning HEAD detached from 7263532. These are my most recent two commits: commit 8a870e8a1cb63bac7e9ec732908e54f20e841bb3 (HEAD) commit…
gkeenley
  • 6,088
  • 8
  • 54
  • 129
0
votes
4 answers

How to switch (or checkout) to a existing remote branch in Git without facing detached HEAD

At first when I clone the Git repository I am in the master branch. But I have already created a remote develop branch. I run git fetch origin develop. Then, I use git checkout develop and it switched to a new branch develop like this: It creates…
Mands
  • 171
  • 1
  • 3
  • 14
0
votes
1 answer

Why do I have two main branches in git repo?

Can some explain what is happing here? Why is there two main branches one named "origin/main" and other one is "main" Made an alias graph DRB@SamiUllah MINGW64 /d/30days_python/myscrapping (main) $ alias graph="git log --all --decorate --oneline…
Sami Ullah
  • 39
  • 1
  • 6
0
votes
1 answer

I return from 'detached HEAD' state and git deleted my .env file from the local directory

I was checking my commit history to fixing a bug. I use git checkout 4a3cf4ebfc8c3d4a7f8b055b3f38cc90acf2a0cd to Switching between commits. I was in - 'detached HEAD' state. in that state, I use some command like: git add . git commit -m…
0
votes
1 answer

Why changes a new branch in detached head state all the parent refs?

If I compare the git/refs when creating a new branch from last commit of a branch versus creating from a detached head state, the git/refs shows different behavior (names) and I like to understand why. Lets assume the head is on master. I need a…
onemorequestion
  • 1,056
  • 1
  • 10
  • 15