Using Git to display your web app’s version

I’m mainly writing this down for my own benefit, but others might find this useful. In a previous post about setting up git to deploy to a remote web server, I wrote about a way to display the lastest abbreviated commit hash in your web app’s footer.

Today, I discovered that simply running:

git describe

will output the most recent tag and the current abbreviated commit hash (if there are commits after the tag), so you can use tags to show more human-readable versions of your app (v1.0, etc.) along with the current commit. Unfortunately, you apparently can’t run ‘git describe’ on a bare git repository like what I’m using for deployment on the remote server.

(local) Lamont Audio Archive

My solution for now (although I think there has to be a better way) is to create my own little shell script that will run a “git push”, overwrite a local version.txt file with the current tag/commit hash, then use SCP to upload the file to the environment I just pushed to.

The version.txt and push scripts (“./testpush”, “./prodpush”) are all left out of the repository and stored in the base folder of my app.

[Previous script removed}

Updated (3/7/2013):

A single, more efficient script simply called "push". Enter "./push [environment]” to do everything above to the designated environment:


REMOTE_PATH=[full remote path to application root]/$VERSION_FILE
LOCAL_PATH=[full local path to application root]/$VERSION_FILE
TEST_SERVER=[test ip/domain]
PROD_SERVER=[prod ip/domain]

if [ -z "$1" ]; then

  echo 'no server parameter given'


  if [ $1 == "test" ]; then
    elif [ $1 == "prod" ]; then
    echo 'invalid server parameter'

  $GIT_PATH push $1
  echo "pushed to $1"
  $GIT_PATH describe > $LOCAL_PATH
  echo "updated version.txt"
  echo "copied version.txt to $1"


I’d like to somehow roll this functionality into a git hook instead of needing to create my own scripts, but I just haven’t figured that out yet.