9

I need to find out if a ref to a commit exists. One thing that almost works is git show which sometimes returns non-zero if the ref does not exist. However, it also accepts paths within the current source tree. So if I have a file named master but the ref master does not exist, git show succeeds. I have not been able to find any more specific commands.

I would like the method to accept:

  • Names of branches
  • Commit hashes
  • Tags
  • Keywords like HEAD

Edit: I'm doing this because I want to preform sanity checks on user input programatically.

5 Answers 5

13

You can use git rev-parse with the --verify flag to parse various refs and decode them to their SHA-1 hash value. This will get rid of all invalid refs and also file paths:

$ git rev-parse --verify HEAD
a93775d7fcd9bf27bbd89ee69e84a03e06223e9b
$ git rev-parse --verify HEAD~2
4100b19c32fac3e2c0838c85d180cd4f50500c2f
$ git rev-parse --verify master
e88352936f4ffc703cdfb0da95ad5592456feb0b
$ git rev-parse --verify origin/master
e88352936f4ffc703cdfb0da95ad5592456feb0b
$ git rev-parse --verify README.md
fatal: Needed a single revision
$ git rev-parse --verify doesnotexist
fatal: Needed a single revision

Note that this will unfortunately also accept other object types, e.g. trees:

$ git rev-parse --verify "HEAD^{tree}"
5fdca9a3c0db1e1414229a7b50dac20e4e87a3b8

What you can do is use git cat-file as this operates directly on Git’s object database. So it can only find objects that actually exist. It also parses any kind of revision argument you pass to it, so it can understand various references. By using the -t parameter, it will give you the type of the object you are passing. If that type is a commit, then whatever you passed is a valid way to specify an existing commit:

$ git cat-file -t HEAD
commit
$ git cat-file -t HEAD~2
commit
$ git cat-file -t master
commit
$ git cat-file -t origin/master
commit
$ git cat-file -t README.md
fatal: Not a valid object name README.md
$ git cat-file -t doesnotexist
fatal: Not a valid object name doesnotexist
$ git cat-file -t "HEAD^{tree}"
tree

So just check whether that returns commit and you seem to have a good way to verify whether a user input is a valid commit reference.

5
  • Thank you! It appears that "git cat-file -t" is what I was looking for :)
    – timthelion
    Commented Jan 6, 2016 at 9:37
  • 1
    Please note, that it will return tag for tags, so you would need to check if it is either commit or tag in the output. Commented Mar 22, 2016 at 10:03
  • Exactly what I needed! Good for finding out how far you can go back with HEAD~ e.g. for finding the first commit
    – xdevs23
    Commented Sep 30, 2017 at 11:39
  • Note that this won't work for verifying that a commit ID exists. Instead, the help page for git rev-parse suggests that git rev-parse --verify <commit_id>^{commit} can be used (or ^{object} for any type of object).
    – dinvlad
    Commented Jul 12, 2023 at 17:20
  • @CaptainAhab there's syntax for "resolve tags to whatever they point at", ^{}, so v2.44.0^{} will return whatever that tag resolves to no matter how many annotated tags it has to chase through.
    – jthill
    Commented Mar 3 at 15:06
1
git show [options] <object>

can help you if you write more precisely object name as described here. For example there is a note about master:

master typically means the commit object referenced by refs/heads/master. If you happen to have both heads/master and tags/master, you can explicitly say heads/master to tell Git which one you mean.

More information about git show

1
  • That works when I'm at the command line, but what I am doing now is trying to sanitize user input programatically. At the point where my program is preforming the check it doesn't know anything about the input, only that it is a string that was typed in.
    – timthelion
    Commented Jan 6, 2016 at 9:21
1

You have another option beside git rev-parse/git cat-file/git show

git show-ref --exists

With Git 2.43 (Q4 2023), rc1, teach "git show-ref"(man) a mode to check the existence of a ref.

See commit 0497e6c, commit 9080a7f, commit 1307d5e, commit 199970e, commit ee26f1e, commit b0f0be9, commit 8465098, commit 7907fb0, commit 53921d5, commit dbabd0b, commit b14cbae, commit ff546eb (31 Oct 2023) by Patrick Steinhardt (pks-t).
(Merged by Junio C Hamano -- gitster -- in commit d8972a5, 08 Nov 2023)

builtin/show-ref: add new mode to check for reference existence

Signed-off-by: Patrick Steinhardt

While we have multiple ways to show the value of a given reference, we do not have any way to check whether a reference exists at all.
While commands like git-rev-parse(1) or git-show-ref(1) can be used to check for reference existence in case the reference resolves to something sane, neither of them can be used to check for existence in some other scenarios where the reference does not resolve cleanly:

  • References which have an invalid name cannot be resolved.
  • References to nonexistent objects cannot be resolved.
  • Dangling symrefs can be resolved via git-symbolic-ref(1), but this requires the caller to special case existence checks depending on whether or not a reference is symbolic or direct.

Furthermore, git-rev-list(1) and other commands do not let the caller distinguish easily between an actually missing reference and a generic error.

Taken together, this seems like sufficient motivation to introduce a separate plumbing command to explicitly check for the existence of a reference without trying to resolve its contents.

This new command comes in the form of git show-ref --exists(man).
This new mode will exit successfully when the reference exists, with a specific exit code of 2 when it does not exist, or with 1 when there has been a generic error.

Note that the only way to properly implement this command is by using the internal refs_read_raw_ref() function.
While the public function refs_resolve_ref_unsafe() can be made to behave in the same way by passing various flags, it does not provide any way to obtain the errno with which the reference backend failed when reading the reference.
As such, it becomes impossible for us to distinguish generic errors from the explicit case where the reference wasn't found.

git show-ref now includes in its man page:

The --exists form can be used to check for the existence of a single references. This form does not verify whether the reference resolves to an actual object.

git show-ref now includes in its man page:

--exists

Check whether the given reference exists.

Returns an exit code of:

  • 0 if it does,
  • 2 if it is missing, and
  • 1 in case looking up the reference failed with an error other than the reference being missing.

With Git 2.44 (Q1 2024), batch 11, update to a new feature recently added, "git show-ref --exists"(man).

See commit 0aabeaa (10 Jan 2024) by Toon Claes (To1ne).
(Merged by Junio C Hamano -- gitster -- in commit cf58f59, 29 Jan 2024)

builtin/show-ref: treat directory as non-existing in --exists

Signed-off-by: Toon Claes

9080a7f ("builtin/show-ref: add new mode to check for reference existence", 2023-10-31, Git v2.43.0-rc1 -- merge) added the option --exists to git-show-ref(1).

When you use this option against a ref that doesn't exist, but it is a parent directory of an existing ref, you get the following error:

$ git show-ref --exists refs/heads
error: failed to look up reference: Is a directory

when the ref-files backend is in use.
To be more clear to user, hide the error about having found a directory.
What matters to the user is that the named ref does not exist.
Instead, print the same error as when the ref was not found:

error: reference does not exist

As an alternative, "git show-ref --verify"(man) accepts pseudorefs. It did not show things like "CHERRY_PICK_HEAD", which has been corrected with Git 2.44 (Q1 2024), rc1.

See commit 1af410d, commit 1dbe401 (07 Feb 2024) by Phillip Wood (phillipwood).
(Merged by Junio C Hamano -- gitster -- in commit b3370dd, 12 Feb 2024)

show-ref --verify: accept pseudorefs

Signed-off-by: Phillip Wood

"git show-ref --verify"(man) is useful for scripts that want to look up a fully qualified refname without falling back to the DWIM rules used by git rev-parse(man) rules when the ref does not exist.
Currently it will only accept "HEAD" or a refname beginning with "refs/".
Running

git show-ref --verify CHERRY_PICK_HEAD

will always result in

fatal: 'CHERRY_PICK_HEAD' - not a valid ref

even when CHERRY_PICK_HEAD exists.
By calling refname_is_safe() instead of comparing the refname to "HEAD" we can accept all one-level refs that contain only uppercase ascii letters and underscores.

0

So given something that resolves to an existing commit, you want to find all refs that point to it?

The following seems to give what you want:

git show-ref --head --heads --tags | grep `git rev-parse $THING`
1
  • I want to sanity check user input. show-ref actually gets me half way there. I can now check if a branch name or other reference, like HEAD, exists/is valid. I still don't know how to determine if a commit hash exists.
    – timthelion
    Commented Jan 6, 2016 at 9:16
0

If you want to check all the branches, you can use git branch -a to list all the remote and locate branches.

If you want to check all commit hashes, you can use git log to list all commits.

If you want to check all the tags, you can use git tag -l to list all the tag names.

And if you want to know which commit your HEAD point to, you can use git rev-parse HEAD to check. And take this as a reference.

If you want to learn more, you can check the bookPro Git.

1
  • Does git log actually list all commits or merely the ones that are linked to from HEAD? What about commits in other branches?
    – timthelion
    Commented Jan 6, 2016 at 9:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.