0

I have a git repository and worked there for a few months. Now I want to go back to a commit in the history. I listed the history by running:

git log -3

This showed the previous 3 commits. Then I choose one commit I want to go and ran:

git checkout 77486083c985e6858b8aecd6a085e2f028885b4b 

After that, I think my HEAD have been changed to that commit. When I run git status I get:

HEAD detached at 6b90718
nothing to commit, working directory clean

I don't understand where "6b90718" came from. How can I read this ID? If I want to show a list of commit where I am current is, how can I do that?

RJFalconer
  • 10,890
  • 5
  • 51
  • 66
Joey Yi Zhao
  • 37,514
  • 71
  • 268
  • 523
  • You should see the first 7 characters of the commit you checked-out. – Maroun Apr 10 '16 at 06:43
  • When you check out a commit ID like that, you point HEAD to that particular commit instead of the branch it's on. That's what a 'detached HEAD' state is -- HEAD is no longer pointing to any particular branch, so operations that require branches (like merge) won't work normally. You can undetach it again by checking out a branch (like master). As for the particular commit ID you're seeing there.. well, it *should* be the same as whatever you checked out. Are you sure you didn't do anything after the checkout command? – Jon Carter Apr 10 '16 at 07:01
  • I didn't do anything after checkout command. The commit id I checked out is "77486083c985e6858b8aecd6a085e2f028885b4b" but how that becomes to "6b90718" – Joey Yi Zhao Apr 10 '16 at 07:14
  • This would happen if you checked out an annotated tag. – jthill Apr 10 '16 at 07:39
  • sorry, I don't understand what you mean by check out an annotated tag. – Joey Yi Zhao Apr 10 '16 at 08:17
  • Read all about HEAD in here: http://stackoverflow.com/questions/34519665/how-to-move-head-back-to-a-previous-location/34519716#34519716 – CodeWizard Apr 10 '16 at 08:41
  • @CodeWizard I have read this post but it doesn't explain the ID of the ID. My question is why the ID shown at the detached HEAD is different with the commit ID I typed. – Joey Yi Zhao Apr 10 '16 at 14:30
  • Have you verified that its not a merge? – CodeWizard Apr 10 '16 at 14:37
  • Don't checkout commits. `git reset` the branch to that commit instead. – RJFalconer Apr 12 '16 at 22:37
  • @RJFalconer Why should he? This would repoint his branch and is maybe not what he wanted, otherwise he probably would have done it. – Vampire Apr 13 '16 at 06:22
  • I suspect that's probably not the case. When people are new to git they use `checkout` in this way and it's nearly always not what they want ("Detatched head? What?"). It's hard to speculate what op wants ("go back to a commit" can mean a lot of things), but `reset`ing the branch seems likely. Alternatively he wants to `rebase -i` to edit an old commit in the history or make a new branch and diverge from the old commit. Regardless, I'd argue that you nearly never want to `git checkout `. – RJFalconer Apr 13 '16 at 08:54

2 Answers2

0

I was wrong. Below is my now fixed answer.


After than, I think my HEAD have been changed to that commit.

git checkout changes your HEAD and makes your working directory look like the directory represented at the commit you give it, in this case 7748608. What it does not do is change any branch references. What it means to have a detached HEAD is that your HEAD ref doesn't point to any branch refs. If you make any commits you will branch away from any other branch history. Here's an example from the git checkout man page:

$ edit; git add; git commit

     HEAD (refers to commit 'e')
      |
      v
      e
     /
a---b---c---d  branch 'master' (refers to commit 'd')
    ^
    |
  tag 'v2.0' (refers to commit 'b')

In this example, the HEAD was detached to point at b and then a commit was made (commit e). Because e is not referenced by any branch or tag refs, it risks being deleted when the git garbage collector next runs. This is the danger of having a detached HEAD and is the reason git warns you when you have one.

Back to your question, though:

HEAD detached at 6b90718

This is saying that your HEAD is detached (it doesn't point at any branch ref) and it's letting you know that your HEAD is referencing commit 6b90718. The fact that it's 6b90718 and not 7748608 looks strange, but if you made any commits after the checkout then it makes perfect sense. 7748608 would be like b in the above example, and 6b90718 would be e.

ditzy
  • 101
  • 5
  • 2
    This is not correct. HEAD is always the commit you have currently checked out. If you have checked out a branch, HEAD points to that branch. If you have checked out some arbitrary commit-ish, HEAD points to that and is detached. The exception is, if you use checkout together with paths, which will only update those paths and not HEAD. Reset can (but does not have to) update HEAD, depending on parameters. More important is, with reset you can modify what the current branch is pointing to, which is impossible for checkout. Checkout works only on worktree and HEAD reference! – Vampire Apr 11 '16 at 18:34
  • Wow, I was really really wrong. You're right. I even realized after posting it that something didn't sound right about it, but for some reason couldn't see it. In my head I was thinking of the branch reference, not the HEAD ref. Will update my answer to correct it. Thank you for catching that. – ditzy Apr 12 '16 at 22:16
  • At least now your text is correct. It doesn't answer the question though. In the comments of the OP was the question and answer whether he did anything, like commit, after the checkout and he clearly stated that he didn't. I think this answer should be deleted. Though being accepted it does not answer the question at all. – Vampire Apr 13 '16 at 06:19
  • I accepted this answer mainly because he gives me a much better explaination than what I expected. I understand the background information a lot which I was really appreciated. – Joey Yi Zhao Apr 13 '16 at 06:26
  • I'm new to SO so I'm still figuring out what are and aren't appropriate answers, so I appreciate the feedback. My thought on why my answer would be appropriate is that there isn't really an answer to the OP's question as stated. At the very least there is missing info. The idea behind my answer is that it gives enough info for the OP to figure out where they might have deviated, causing the different commit to be checked out. – ditzy Apr 16 '16 at 06:20
0

If I want to show a list of commit where I am current is, how can I do that?

git log, gitk, or gitg.

I don't understand where "6b90718" came from

ditzy's answer covers this

Progressing from here

"I choose one commit I want to go"

What to do next depends on what you want to do exactly, and whether or not you've pushed upstream and whether or not you have people who have pulled from that.

If you just want to make some changes from the point you've checked out (and either update or re-do or delete the changes that follow), see a previous answer here.

Community
  • 1
  • 1
RJFalconer
  • 10,890
  • 5
  • 51
  • 66