1

I tried to switch to another commit, so I did : git checkout 031c057 (fourth commit in order)

After I switched back to a06bbac then I did some modification and I did a 'commit'

Now when I git status I see :

$ git status
HEAD detached from a06bbac
nothing to commit, working tree clean

Here my git log :

f24cb85 (HEAD) seconde template
a06bbac (mostafa-test) sc just for test
19c2ad5 (origin/mostafa-test) first sample template
031c057 sc
f6c72a0 make component for table and header
89a0dd3 material-table ready
748ce3b first grid & first table
605562f (master) git ignore fix
1ec70f4 sc
eadfa97 (origin/master, origin/HEAD) Initial commit

How to make it normal ?

5
  • 2
    Don't use raw commit IDs with checkout; use existing branch heads.
    – chepner
    Commented Apr 1, 2020 at 16:10
  • Does this answer your question? HEAD detached at origin/master
    – matt
    Commented Apr 1, 2020 at 16:31
  • Relevant: stackoverflow.com/questions/34987957/…
    – jub0bs
    Commented Apr 1, 2020 at 16:37
  • @jub0bs Yes Thanks it works Commented Apr 1, 2020 at 16:43
  • I did the same thing. I solved the problem by checking out my changes on a new branch, switching to the main branch, and then merging the changes from the new branch.
    – Joe Lapp
    Commented Aug 11, 2021 at 23:20

3 Answers 3

3

HEAD is a special symbolic reference. It's meant to refer to branch heads, not commits directly. When it refers to something that isn't a branch head, we say that HEAD is in a detached state.

After you ran git checkout a06bbac, your Git state resembled

HEAD ----------------> a06bbac ---> 19c2ad5 ---> ...
                         ^
                         |
mostafa-test ------------+

rather than

HEAD ----> mostafa-test --> a06bbac ---> 19c2ad5 ---> ...

As a result, running git commit did not update mostafa-test as it should have.

To fix this, you can simply checkout mostafa-test, then use git reset to fix it.

$ git checkout mostafa-test
$ git reset f24cb85
3
  • Thanks , I did it but the changes I did in commit f24cb85 (HEAD) seconde template are not applied ! Is it possible to "merge" this commit to my current branch ? Commented Apr 1, 2020 at 16:35
  • You shouldn't need to. f24cb85 already has a06bbac as its parent; the only problem is that it isn't correctly identified as the branch head mostafa-test. What does git log --graph --online --all --decorate show?
    – chepner
    Commented Apr 1, 2020 at 20:59
  • I did the exact same thing as the OP and tried your procedure, but I lost my changes. Fortunately, I copied my repo to a new directory before attempting this and so restored it without having to do git magic. I solved the problem by checking out my changes on a new branch, switching to the main branch, and then merging the changes from the new branch.
    – Joe Lapp
    Commented Aug 11, 2021 at 23:19
1

I think I found It :

First I kept my commit by : `git branch -f mostafa-test HEAD

Then : git checkout mostafa-test

It seems working !

0

It means HEAD is detached from a branch.

So when come back to a06bbac, use git checkout mostafa-test.

Normally HEAD is on a branch, not commit.

1
  • Tanks , but when I do this I lose all modification I did in commit f24cb85and it returns to commit a06bbac , How to keep the commit f24cb85 Commented Apr 1, 2020 at 16:24

Not the answer you're looking for? Browse other questions tagged or ask your own question.