1

If my git repository is in a 'Detach Head' state, when i do 'git pull', it still can pull in the new commits remotely.

And I see the new commits after i do 'git pull'.

My question is 'if my repository is in Detach head' state, and I have made some local commits (did not push) and then I do 'git pull', I see the new commits, but my local commits are gone.

Why is that? and where/how i can see my local commits?

Thank you.

michael
  • 106,540
  • 116
  • 246
  • 346

2 Answers2

0

You should not commit on a detached head !
When you realize you are on a detached head (and you wish to make changes) you'll need to:

git checkout -b someBranch

Given you already made changes and commited them, once you lost them you'll need to do something like:

git branch someBranch HEAD@{1}

you can check this for more info.

Community
  • 1
  • 1
Ofir Farchy
  • 7,657
  • 7
  • 38
  • 58
0

USually, the HEAD pointer is a symbolic reference that points to a an actual reference (usually a branch). You can make commits on that. WHen you're in a detached HEAD state, the HEAD pointer directly points to a commit instead of an actual reference. Committing at this point will save the data but will make it unreachable. If you switch to an actual branch, there is no straightforward way of switching back to the new commits you made.

The solution is to cut a branch from where you are using git branch foo. Now if you switch branches, you can still switch back to the newly created foo branch that contains your new commits.

Noufal Ibrahim
  • 71,383
  • 13
  • 135
  • 169