How to get the commit hash and branch name with git rev-parse
Running git rev-parse HEAD prints only the hash of the current commit, on one line. Running git rev-parse --abbrev-ref HEAD prints only the name of the current branch. These commands are useful when you want to put the commit hash into the name of a built file or a version display, or when you want to branch the logic in a shell script or CI based on the current branch name. I checked the output in a test environment before and after making a commit, after switching branches, and in detached HEAD state.
The test environment is Ubuntu 24.04 with Git 2.43.0. I set the author name and email to example values (Neko <neko@example.com>) and fixed the commit dates. The output was written to a file and captured, so it has no color. I did not test Windows or macOS, and I did not test output that uses remote branches (such as @{u}).
Get the commit hash
I created three commits in a repository named neko-cafe. git log --oneline shows them like this:
$ git log --oneline
52dce6e ケーキを足す
45dc7e8 紅茶を足す
0ed10c0 メニューを作る
Running git rev-parse HEAD printed the hash of the newest commit as a full 40-character string. Nothing else was printed.
$ git rev-parse HEAD
52dce6e578fbb0f77b8a21e1d7029f0569595b3d
If you want a shorter hash, add --short. You can also set the length, as in --short=12.
$ git rev-parse --short HEAD
52dce6e
$ git rev-parse --short=12 HEAD
52dce6e578fb
According to the official git rev-parse reference, --short shortens the hash to the length needed to be distinct from other objects. If you do not give a length, it follows the core.abbrev setting, and the shortest length you can specify is 4. In this test, the result was 7 characters, the same as git log --oneline.
Instead of HEAD, you can pass a branch name, a tag name, or a form like HEAD~1 (the commit one before the current one). If you list several, each is printed on its own line.
$ git rev-parse HEAD~1
45dc7e8f13f51225eb109f05f4025a558cdf8f95
$ git rev-parse --short HEAD~2
0ed10c0
$ git rev-parse HEAD HEAD~1 HEAD~2
52dce6e578fbb0f77b8a21e1d7029f0569595b3d
45dc7e8f13f51225eb109f05f4025a558cdf8f95
0ed10c0cff7c0a8385c0648f68fe36fda182dd70
The forms such as HEAD~1 are described in the official gitrevisions reference. ~3 means go back three generations along the first parent, and writing only @ is the same as HEAD.
Get the branch name
You can get the current branch name with --abbrev-ref HEAD. With --symbolic-full-name HEAD, you get the full name that starts with refs/heads/.
$ git rev-parse --abbrev-ref HEAD
main
$ git rev-parse --symbolic-full-name HEAD
refs/heads/main
When I moved to a new branch with git switch -c feature/cake-price, both outputs changed to the name of the new branch. The hash was still 52dce6e, the same as before the switch, because creating a branch by itself does not change the commit.
$ git switch -q -c feature/cake-price
$ git rev-parse --abbrev-ref HEAD
feature/cake-price
$ git rev-parse --symbolic-full-name HEAD
refs/heads/feature/cake-price
$ git rev-parse --short HEAD
52dce6e
After I made a commit on this branch, the hash for HEAD changed to the hash of the new commit. Passing main printed the hash of the commit that main points to. main did not move, so it stayed at the original 52dce6e.
$ sed -i "s/ケーキ 500円/ケーキ 550円/" menu.txt && git commit -q -am "ケーキを550円にする"
$ git rev-parse --short HEAD
3c33af5
$ git rev-parse --short main
52dce6e
In detached HEAD, the output is HEAD, not a branch name
I ran the same commands in a detached HEAD state, where you are moved directly to a tag or commit instead of a branch. I went back to main, added an annotated tag v1.0 to the previous commit (HEAD~1, 45dc7e8) with git tag -a, and then moved to it with git switch --detach v1.0.
$ git tag -a v1.0 -m "最初の版" HEAD~1
$ git switch -q --detach v1.0
$ git rev-parse --short HEAD
45dc7e8
$ git rev-parse --abbrev-ref HEAD; echo "終了ステータス: $?"
HEAD
終了ステータス: 0
$ git rev-parse --symbolic-full-name HEAD; echo "終了ステータス: $?"
HEAD
終了ステータス: 0
$ git branch --show-current; echo "終了ステータス: $?"
終了ステータス: 0
Both --abbrev-ref HEAD and --symbolic-full-name HEAD printed the word HEAD, exited with status 0, and did not report an error. The other command for getting a branch name, git branch --show-current, printed nothing. The official git branch reference also says that it prints nothing when HEAD is detached.
If you use the output of git rev-parse --abbrev-ref HEAD as a branch name while in detached HEAD, you will treat it as if a branch named HEAD existed. When you use a branch name in a script, decide in advance what to do when the output is HEAD.
When I went back with git switch -q main, the output of --abbrev-ref HEAD returned to main.
$ git switch -q main
$ git rev-parse --abbrev-ref HEAD
main
Get the commit hash from a tag name
When I passed the name of an annotated tag such as v1.0 directly, the output was not the commit hash. The commit that v1.0 marks is 45dc7e8, but a different hash, 031c40b…, was printed.
$ git rev-parse v1.0
031c40b1ef1b2f8e11162a721b75755edd24c1ae
$ git rev-parse "v1.0^{commit}"
45dc7e8f13f51225eb109f05f4025a558cdf8f95
$ git rev-parse --short HEAD~1
45dc7e8
I checked the object types with git cat-file -t. v1.0 is a tag object, and v1.0^{commit} is a commit.
$ git cat-file -t v1.0
tag
$ git cat-file -t "v1.0^{commit}"
commit
According to the official gitrevisions reference, ^{commit} follows the given object until it reaches a commit. If you want the hash of the commit a tag points to, pass the name with ^{commit} added.
An error occurs when there are no commits yet
When I ran git rev-parse HEAD in a repository just after git init, before any commit existed, it returned an error.
$ git rev-parse HEAD
fatal: ambiguous argument 'HEAD': unknown revision or path not in the working tree.
Use '--' to separate paths from revisions, like this:
'git <command> [<revision>...] -- [<file>...]'
HEAD
The HEAD line after the error is not part of the error message. It was printed to standard output. When I discarded standard error, only the HEAD line remained, and the exit status was 128.
$ git rev-parse HEAD 2> /dev/null; echo "終了ステータス: $?"
HEAD
終了ステータス: 128
In a script that stores the output in a variable, such as $(git rev-parse HEAD), the text HEAD ends up in the variable in place of a hash. With --verify, nothing is printed to standard output when the argument cannot be turned into a hash, and the command exits with an error. Adding -q (--quiet) also suppresses the error message, and the command exits with status 1.
$ git rev-parse --verify HEAD; echo "終了ステータス: $?"
fatal: Needed a single revision
終了ステータス: 128
$ git rev-parse --verify -q HEAD; echo "終了ステータス: $?"
終了ステータス: 1
According to the official reference, --verify checks that the argument is exactly one item that can be turned into an object hash. If it can, the hash is printed to standard output; if not, it reports an error. -q has an effect only together with --verify. In addition, --short behaves the same way as --verify, so passing the nonexistent hash 9999999 produced the same error.
$ git rev-parse --short 9999999; echo "終了ステータス: $?"
fatal: Needed a single revision
終了ステータス: 128
In this repository, --abbrev-ref HEAD also returned an error when there were no commits. git branch --show-current, on the other hand, printed main, the name of the branch that will be used for the first commit, even before any commit existed.
$ git rev-parse --abbrev-ref HEAD; echo "終了ステータス: $?"
fatal: ambiguous argument 'HEAD': unknown revision or path not in the working tree.
Use '--' to separate paths from revisions, like this:
'git <command> [<revision>...] -- [<file>...]'
HEAD
終了ステータス: 128
$ git branch --show-current
main
Use in shell scripts
From here on, I tested after switching back to main, which has three commits. Embedding the output in a string with $(...) lets you build a name that contains the hash.
$ echo "build-$(git rev-parse --short HEAD)"
build-52dce6e
To check whether a branch exists, pass refs/heads/ followed by the branch name to --verify -q, and decide based on the exit status. The hash in the output is not needed, so I discarded it to /dev/null.
$ for b in main feature/cake-price feature/nothing; do if git rev-parse --verify -q "refs/heads/$b" > /dev/null; then echo "$b: ある"; else echo "$b: ない"; fi; done
main: ある
feature/cake-price: ある
feature/nothing: ない
To see whether two branches point to the same commit, compare their hashes. Since I was on main, HEAD and main are the same. feature/cake-price has one more commit, so the comparison reported that they differ.
$ test "$(git rev-parse HEAD)" = "$(git rev-parse main)" && echo "HEAD と main は同じコミット"
HEAD と main は同じコミット
$ test "$(git rev-parse HEAD)" = "$(git rev-parse feature/cake-price)" && echo "同じ" || echo "違う"
違う
The official reference also says that when a script uses --verify to check a name received from outside, it is a good idea to use --end-of-options so that the name is not mistaken for an option. Placing --end-of-options before the name looks like this:
$ b=main; git rev-parse --verify -q --end-of-options "refs/heads/$b"
52dce6e578fbb0f77b8a21e1d7029f0569595b3d




