1

I am working on a single branch. Now I'm in a state I don't quite understand. My git status says:HEAD detached from 25e7afb.

Actually, I did check out ( $ git checkout "sha XXX" ), and after that I keep getting the warning HEAD is detached and pull new changes. While there is not any new change to pull.

Although I reset the checkout and HEADS at "sha of XXX", it still says HEAD is detached .

When I intend to push new commit, I get the attached error.

Could anyone help me how I solve this problem?

enter image description here

sepideha
  • 1,669
  • 1
  • 11
  • 14

2 Answers2

3

The solution comes after understanding what checkout does actually and how things work.

When you initially create a commit, git automatically creates a branch with the default known name master.

C1
 ^
 |
 master

As you can see, C1 represents the initial commit. There's a pointer (branch), which is master, that points to C1.

When you create another commit, the pointer moves on to the new one:

C1 <- C2
      ^
      |
      master

master now points to C2. C2 has a parent which is C1. Every commit points to the preceding commit.

There's one more pointer not shown in the figures. This is HEAD pointer. The HEAD points to the current commit you are on. Meaning, if you are C2, HEAD points to C2. This helps you to jump between commits easily.

At this point of time, if you checkout to C1, you are moving the HEAD pointer to point to C1 and leave (detach from) C2.

Therefore, in most cases, if you checkout to another commit, you generally at some later point, you checkout back again to the most recent commit.

This part of the tutorial discusses checkout and reset. You might need to start reading starting from branches.

joker
  • 3,416
  • 2
  • 34
  • 37
1

I did the following and the problem has been resolved!

  $  git checkout master

This helps to go back to the master (the latest version), and not being detached anymore!

P.S. Assumed we don't have new changes to loose.

sepideha
  • 1,669
  • 1
  • 11
  • 14