I want to display the git commit id (i.e. SHA) of the head of master on my website as an identifier.
How can I pull this information from git?
To find a git commit id (or hash), you can simply use the git log command. This would show you the commit history, listing the commits in chronological order, with the latest commit first.
Commit IDs are unique SHA-1 hashes that are created whenever a new commit is recorded. If you specify a commit ID when you add a repository, Domino will always pull the state of the repository specified by that commit in a detached HEAD state.
To get a list of all commit ids of a branch, you can use git rev-list branchname .
This command will get you the latest commit's SHA-1
git rev-parse HEAD
Instead of the HEAD SHA1, I would rather go with git describe
, as a more readable way to get a "build id". For instance:
git describe --abbrev=4 HEAD
If you don't care about tags, and considering that git describe
is a porcelain command, which shouldn't be used in a script, then, yes, git rev-parse
(a plumbing command) is more appropriate.
But again, if you are to display a SHA1 on your website as an id, I would go with:
git rev-parse --short HEAD
(In order to display only the first 7 digits of the SHA1)
git rev-parse HEAD
(meaning the all 40 digits) is still useful, when you want to check if what you just deployed is indeed what HEAD
refers to.
See for instance this deployment script:
It first triggers an update:
#If requested, perform update before gathering information from repos. if $update; then echo "Updating fred-official." cd "$fredDir" git_update if ! [[ "$forceFredID" = "" ]] then checkGitID "$forceFredID" fi echo "Updating website repo." cd "$websiteDir" git_update if ! [[ "$forceWebsiteID" = "" ]] then checkGitID "$forceWebsiteID" fi cd "$startingDir" fi
The update itself refreshes the website content:
# Discard any local changes, update remote branches and tags, and # check out to the latest master branch. git_update() { #To update tags and branches. git remote update git clean -dfx git reset --hard origin/master }
And then it uses git rev-parse HEAD
to check what just has been checked out:
function checkGitID { checkID=$1 echo Checking git ID is "$checkID" if ! git checkout "$checkID" then echo Failed to checkout "$checkID" exit 4 fi if ! actualID=$(git rev-parse --verify HEAD) then echo Failed to verify "$checkID" git checkout master exit 5 fi if ! [[ "$actualID" = "$checkID" ]] then echo Git verification failed, something very bad is happening exit 6 fi echo Git ID verified: "$checkID" }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With