Aditya
Aditya

Reputation: 417

Does the HEAD reference in Git, point to the tag or the SHA-1 value when checked out by specifying the tag?

In Git, when we check out to a specific commit using the following command:

git checkout <commit>

If we use the tag (assume that commit has a tag referencing it) for that particular commit, instead of its SHA-1 value in place of < commit >, will the HEAD reference, point to the tag (i.e. will the HEAD act as a symbolic reference) or point to the SHA-1 value (i.e. act as a reference)?

Thank you!

Upvotes: 1

Views: 434

Answers (3)

Guildenstern
Guildenstern

Reputation: 3773

According to man git checkout,[1] these are the only things that you can feed this command:

git checkout [snip] [<branch>]
git checkout [snip] [<commit>]
git checkout [snip] [<pathspc>]

Notably it says nothing about ref as a general thing that you can make HEAD point to—only <branch> and <commit>.

Further, the only mention of tags is under the section “Detached HEAD” (v2.0 is a tag):

It is sometimes useful to be able to checkout a commit that is not at the tip of any named branch, or even to create a new commit that is not referenced by a named branch. Let’s look at what happens when we checkout commit b (here we show two ways this may be done):

$ git checkout v2.0  # or
$ git checkout master^^

[…]

Notice that regardless of which checkout command we use, HEAD now refers directly to commit b. This is known as being in detached HEAD state.

So if you checkout a commit directly or through a tag the result is the same; you are in “detached HEAD” state, pointing directly at a commit.

So I would say that HEAD may only point to a ref (and no other kind of ref) of type branch:

# The contents of `.git/HEAD`
ref: refs/heads/main

Or else it has to point directly at an object:

# The contents of `.git/HEAD` (in “detached `HEAD`” state)
17abc2521ca2f1331cc8c4a02b501443fd92f11d

Notes

  1. Git 2.40.0

Upvotes: 0

Nitsan Avni
Nitsan Avni

Reputation: 841

you can see exactly what the HEAD is referencing:

<.git/HEAD

HEAD is just a file.

Upvotes: 2

Tim Biegeleisen
Tim Biegeleisen

Reputation: 521123

As far as I know, a Git tag is basically just a pointer to some commit. This is similar to a branch name, which logically also points to the recent commit of a branch. Checking out either a tag name or a branch name will move the HEAD to the relevant commit.

To your exact question, a tag is a pointer to a commit (which has a unique SHA-1 hash value), and checking out a tag will move the HEAD to point to the commit referred to by the tag.

Upvotes: 3

Related Questions