0

I have a problem with git with a detached head which I tried to correct earlier but seems I made things worse. I am working on a local project myself with no branches but it seems due to a mistake when testing an earlier version (and attempts to correct that) "git status" tells me I am at version 23, my HEAD is detached from version 20 and "master" points to version 15. It looks as if I got into this situation originally like described here: Why did my Git repo enter a detached HEAD state?

I had tried to correct a detached head earlier using a method proposed here Fix a Git detached head? ("cherry-pick") but it seems I made a mistake there. Also the first solution there is for a fix for the previous commit, not 8 commits earlier.

I am really lost at this stage and I would like to know how to get my "HEAD" and "master" attached to my current version 23 again. I would like to keep all the changes that I have in my current version.

Alternatively I could simply delete the whole git repository and start fresh again with my current version as first commit. Any advise?

phd
  • 82,685
  • 13
  • 120
  • 165
  • Did you commit changes while in detached HEAD state? – Lasse V. Karlsen Sep 11 '19 at 08:23
  • Yes, about 8. In hindsight I know I shouldn't have done that. – user0109895 Sep 11 '19 at 08:56
  • 2
    OK, the easiest method of making sure you get these commits with you into the future would be to first create a backup of your entire folder, working folder and repository and everything (I will not be held responsible if you lose something :)), then create a branch where you're at (`git checkout -b some-branch-name`), then checkout master (`git checkout master`), then merge in your branch (`git merge some-branch-name`). If this solves your problem and is acceptable, it would probably be the easiest way to fix it. – Lasse V. Karlsen Sep 11 '19 at 09:14
  • I have to deal with other things at the moment but I will try that as soon as possible, backups are done anyway, so no problem. – user0109895 Sep 11 '19 at 19:52

1 Answers1

0

Being in a "detached HEAD" state is just that you checked out a commit instead of having a branch checked out.

So, to fix it, that is as simple as doing a check out on a branch like, for example:

git checkout master

It is a good practice, especially when you are a git beginner, to never commit when you are in a detached HEAD state (i.e. when you checked out a commit). Always check out a branch or create a new one when you want to commit.

Note: But before check out master, it's better that you stash your changes if did some.

when testing an earlier version (and attempts to correct that) "git status" tells me I am at version 23, my HEAD is detached from version 20 and "master" points to version 15.

There are multiple way to solve that but you didn't give enough information to solve it the way you expect. Once on your master branch, it could be either a pull (to merge your commits if you did some) or a reset --hard (to discard your commits if you did some).

PS: I think you should invest some time in doing a git tutorial or pairing on the subject with a coworker.

edit 1: If you made commits in detached head, create a new branch with git checkout -b my-fix to secure your new commits. Then it will be easier to fix your mess.

edit2: If you have difficulties with git checkout, update to git v2.23 and use the 2 new commands: https:///infoq.com/news/2019/08/git-2-23-switch-restore

Philippe
  • 28,207
  • 6
  • 54
  • 78
  • 1
    If he made commits while in detached HEAD state, it's not *just* checking out a branch. He probably also want to keep his changes so he need some additional steps. – Lasse V. Karlsen Sep 11 '19 at 08:24
  • Yes, I would like to keep the changes. I did a checkout when I wanted to temporarily use an old software version. Again this was wrong; I read a good bit about "reset" and "checkout" in the ProGit book but I still do not understand "checkout" 100%. Git is a powerful tool but can do damage if used by the unwise... :-) @Philippe: thank you for your answer; I will have to read up on "pull" before i do a "checkout master". "master" is way back (version 15) and I am very careful at this stage not to make the mess even bigger. – user0109895 Sep 11 '19 at 09:01
  • @user0109895 If you made commits in detached head, create a new branch with `git checkout -b my-fix` to secure your new commits. Then it will be easier to fix your mess (by doing some `rebase`s and `reset`s) – Philippe Sep 11 '19 at 10:02
  • @user0109895 If you have difficulties with `git checkout`, update to git v2.23 and use the 2 new commands: https://www.infoq.com/news/2019/08/git-2-23-switch-restore/ – Philippe Sep 11 '19 at 10:04
  • Hi Philippe, I checked, I have version 1.8.3.1 which I believe is the highest version available for my OS but I am thinking of changing. Thank you for the hint ! And yes, I will do more reading in the book and play with a test project to get a better grip on git. – user0109895 Sep 11 '19 at 19:55
  • @user0109895 v1.8.3 is very old (4 years). I tempted to think that no one should use a version older than 2.0..... – Philippe Sep 11 '19 at 23:04