HEAD is Detached in Repository

Asked

Viewed 6,368 times

6

I’m using NETBEANS and GIT. I made a few changes to when the commit is giving this message:

HEAD is Detached in Repository

My repository is like this:

inserir a descrição da imagem aqui

2 answers

8

This happened because you checked out a commit. You probably should have executed this command git checkout a83c7867c2.

Git has a guy named HEAD. HEAD points to the current branch. It’s through HEAD that we know which branch we’re working on. The normal behavior is that HEAD is always pointing to some branch and the branch points to the last commit. When checking directly into a commit, HEAD will point directly to a commit. This situation features a Detached HEAD.

Anthony Accioly’s solution solves your problem.

git checkout master

However, if you commit to HEAD, which is what it looks like, your commits will be on a separate branch. They won’t appear in the master. The solution to this would be to create a branch that points to the last of these commits and then merge with the master. The command sequence would be:

  1. git checkout -b outra-branch (creating a new branch that points to the commit that HEAD is pointing to)
  2. git checkout master (returning to the master branch)
  3. git merge --no-ff outra-branch (bringing the changes from the other branch to the master)
  4. git branch -d outra-branch (if the branch is no longer needed, it can be removed)

If you don’t want to lose commits made on a Detached HEAD, you should create a branch that points to them. If a checkout is run on a branch, and those commits are loose, at some point, Git’s Garbage Collection will remove them.

  • 1

    explanatory!

7


That means you’re no longer in a branch local (that is, back to a previous commit or exploiting a remote commit).

To get back to normal (considering that there are no new changes to be committed), just check out the branch you want to work with. For example:

git checkout master

Browser other questions tagged

You are not signed in. Login or sign up in order to post.