joseph.hainline
joseph.hainline

Reputation: 26608

Why is git checkout SHA returning a different SHA?

I have a fork of the https://github.com/carrierwaveuploader/carrierwave repo, and recently tried to checkout a specific SHA, but was pointed to a different SHA. It's acting as if two different SHAs actually point to the same location, which shouldn't be able to happen!

Here's the copy/pasted bash output:

$ git checkout 16bafdd41cb993f65919fcf12fc03ff6c9246a33
Note: checking out '16bafdd41cb993f65919fcf12fc03ff6c9246a33'.

You are in 'detached HEAD' state. You can look around, make experimental
changes and commit them, and you can discard any commits you make in this
state without impacting any branches by performing another checkout.

If you want to create a new branch to retain commits you create, you may
do so (now or later) by using -b with the checkout command again. Example:

  git checkout -b new_branch_name

HEAD is now at 396f538... Merge pull request #1160 from pjg/patch-1

$ git status
# Not currently on any branch.
nothing to commit, working directory clean

$ git log
commit 396f5384c67f8a17c153c15da0f1d06bc1f116e4
...

$ git checkout 396f5384c67f8a17c153c15da0f1d06bc1f116e4
HEAD is now at 396f538... Merge pull request #1160 from pjg/patch-1

$ git checkout 16bafdd41cb993f65919fcf12fc03ff6c9246a33
HEAD is now at 396f538... Merge pull request #1160 from pjg/patch-1

To answer Michael Anderson:

$ git cat-file -t 16baf
tag

$ git cat-file -t 396f5
commit

Upvotes: 1

Views: 721

Answers (1)

Akash
Akash

Reputation: 5221

You answered your question.

You can only check out commits. Since tags are one of the 4 git objects (others being blob, tree and commit), they get an SHA. But they are still pointers to commits. And you can only ever check out a commit. No other object can be checked out.

When you try to checkout a tag or branch, the commit which is pointed to by these gets checked out.

Upvotes: 4

Related Questions