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 ?

Mostafa Abdellaoui
  • 355
  • 1
  • 6
  • 15
  • 2
    Don't use raw commit IDs with `checkout`; use existing branch heads. – chepner Apr 01 '20 at 16:10
  • Does this answer your question? [HEAD detached at origin/master](https://stackoverflow.com/questions/25232056/head-detached-at-origin-master) – matt Apr 01 '20 at 16:31
  • Relevant: https://stackoverflow.com/questions/34987957/how-did-i-end-up-with-a-detached-head/34994175#34994175 – jub0bs Apr 01 '20 at 16:37
  • @jub0bs Yes Thanks it works – Mostafa Abdellaoui Apr 01 '20 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 Aug 11 '21 at 23:20

3 Answers3

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
chepner
  • 497,756
  • 71
  • 530
  • 681
  • 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 ? – Mostafa Abdellaoui Apr 01 '20 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 Apr 01 '20 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 Aug 11 '21 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 !

Mostafa Abdellaoui
  • 355
  • 1
  • 6
  • 15
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.

shirakia
  • 2,369
  • 1
  • 22
  • 33
  • Tanks , but when I do this I lose all modification I did in commit `f24cb85`and it returns to commit `a06bbac` , How to keep the commit `f24cb85` – Mostafa Abdellaoui Apr 01 '20 at 16:24