Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does git uses 2 different commands to show HEAD?

Tags:

git

I noticed there are 2 HEADs:

  • .git\HEAD
  • .git\refs\remotes\origin\HEAD

When I use git show-ref HEAD, it only gives me this:

ce0762c073b407d794f54f0b5d2a50375fdcb609 refs/remotes/origin/HEAD

Whle when I use git rev-parse HEAD, it gives me the value of .git\HEAD:

a758e523fe40e799194878bac567b7f4b8eec2b9

Why Git use 2 different commands to show HEAD? Any strong reason?

like image 608
smwikipedia Avatar asked Jul 09 '17 08:07

smwikipedia


People also ask

What command finds the head of the current branch?

One can check your HEAD using git show HEAD command. We have 2 commits and our HEAD is now pointing to the most recent commit we have done. You can also check this in your . git/refs/heads folder.

How do I get my Git head?

You can view your repository's heads in the path . git/refs/heads/ . In this path you will find one file for each branch, and the content in each file will be the commit ID of the tip (most recent commit) of that branch.

What does Git rev parse head do?

git rev-parse is an ancillary plumbing command primarily used for manipulation. One common usage of git rev-parse is to print the SHA1 hashes given a revision specifier. In addition, it has various options to format this output such as --short for printing a shorter unique SHA1.


1 Answers

git-show-ref shows a list of references in your repository and their commit IDs. It should probably be called git-show-refs. It's preferred over directly referencing files in the .git directory.

When you say git show-ref HEAD you're not asking for HEAD. What you're asking for is any references in the list which match the pattern HEAD. HEAD itself is normally filtered out, so you get refs/remotes/origin/HEAD. You can include HEAD with --head.

$ git show-ref --head HEAD
f37beeea95f3b0e6b064f6d4b5f835a058e0568c HEAD
aa1124b89f38eed793e2b9f2d2b2ba5d80a27a20 refs/remotes/origin/HEAD

So you shouldn't be using git show-ref <ref> to look up references.


git-rev-parse takes a revision parameter and gives back a commit ID. Its meant to deal with the myriad different ways you can refer to a commit. For example...

$ git rev-parse --verify master
aa1124b89f38eed793e2b9f2d2b2ba5d80a27a20
$ git rev-parse --verify heads/master
aa1124b89f38eed793e2b9f2d2b2ba5d80a27a20
$ git rev-parse --verify refs/heads/master
aa1124b89f38eed793e2b9f2d2b2ba5d80a27a20

git rev-parse --verify <ref> is what you should be using to look up the commit ID of a reference.

like image 97
Schwern Avatar answered Oct 13 '22 17:10

Schwern