Skip to content

Instantly share code, notes, and snippets.

@magnetikonline
Last active July 23, 2024 00:36
Show Gist options
  • Save magnetikonline/d4d62e6ae5f270b12c1a to your computer and use it in GitHub Desktop.
Save magnetikonline/d4d62e6ae5f270b12c1a to your computer and use it in GitHub Desktop.
Fetch current Git commit SHA-1.

Fetch current Git commit SHA-1

Method 1

From within Git repository:

$ cd /path/to/git/repo
$ git rev-parse HEAD
c8f882b484eeb71068539fd02f04c5c2eada1c02

Fetch another branch/tag SHA-1:

$ cd /path/to/git/repo
$ git rev-parse BRANCH_OR_TAG
c8f882b484eeb71068539fd02f04c5c2eada1c02

Can also fetch SHA-1 from a path outside of Git repository (handy for scripting):

$ git --git-dir=/path/to/git/repo/.git rev-parse HEAD
c8f882b484eeb71068539fd02f04c5c2eada1c02

Method 2

$ git --git-dir /path/to/git/repo/.git log -1 --pretty=format:%H HEAD
c8f882b484eeb71068539fd02f04c5c2eada1c02

Reference

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment