1

I have downloaded a git repository some time ago and continued to work on that locally. I was making some changes and occasionally committed them locally. I haven't pushed any changes either. The last time I tried to commit I found a message (in pyCharm but I guess it's a generic message). I am not sure what I have done to wreck chaos to my repository.

Anyway the message was:

The Git repository ...\PycharmProjects\tensorflow1\deeplab is in the detached HEAD state. You can look around, make experimental changes and commit them, but be sure to checkout a branch not to lose your work. Otherwise you risk losing your changes.

As I am not familiar with all git jargon I was a bit alarmed about the message. As far as I understand checkout will give me a version of the remote git repository to work with, right? I just want to have a committed local version so checkout is not what I would want, right?

To make it clear, I am not interested in getting any newer version of the original git repository. I just want to work with my local version. So, local commits suit me fine.

My conclusion is that:

  1. I should not checkout any version and
  2. Local commits are safe (even in this detached HEAD state) for my case.

Also, if the above are correct, how can I avoid this detached HEAD state? I should create a new branch of my local git repository and work with that one?

Edit:

After a bit of research I found out that I actually have made changes to both master (the branch I was supposed to work on) and to my detached HEAD state. So, when I tried to merge them (following the answers below) some conflicts occurred. Since, I am content with my current state (besides being a detached HEAD) I reverted the merge with:

git reset --hard HEAD

and just kept my code in a new branch. Since, there is no risk of data loss this solution satisfies me.

Eypros
  • 5,370
  • 6
  • 42
  • 75

2 Answers2

2

The detached head state simply means that you have checked out a commit, rather than a branch.

This is completely safe, but most of the time you have no reason to be in such a state.

The best way to get out of this state is running git checkout -b <new-branch>, this allows you to keep working in a sane way.

This will create a branch pointer to the currently checked out commit and move your HEAD to this.

That will give you the intended situation.

It seems like you are coming from a client/server based version control system like SVN and that muddles your understanding a bit.

I would try to run the tutorial at https://try.github.io to get a better feeling for Git.

The risk you are running is that if you check out a new point in history ( ie what's on master?), then how do you get back to the point where you are working on now ( the detached head state )?

It is possible through git reflog ( advanced, don't investigate this command yet ). But you might also end up in a situation where your commits gets garbage collected. ( By standard this takes 60-90 days or so, so not currently a concern ).

I hope this was helpful.

TL;DR

Create a branch on the commit and work on that branch. You are safe.

RandomSort
  • 600
  • 7
  • 22
2

Detached HEAD state means that you are not in any branch. That's why you get this error message.

If you want to verify that's the case, run from your terminal/command prompt

git branch

You will see that you are not in any branch.

  • First, make sure you committed all your work even though you are detached.
  • Now you want to get out of the detached state by running git branch temp_state. This will create a new branch and will keep your current changes.
  • Now, run git checkout master to go back to the branch you were supposed to work with (if it was master, then master).
  • git merge temp_state to merge your current work to the correct branch.
Rafael
  • 7,002
  • 5
  • 43
  • 52