Take the 2-minute tour ×

is there a way to automatically update version number of a package.json (https://npmjs.org/doc/json.html) with the HEAD hash of git? I would like to have something like version: 1.0.0+rev82e4b91cfe42cd86e9453b4987b9cc446566de6 inside the project's package.json file. Eveything before the plus is set manually and the hash is updated everytime I commit something.

Is this possible? I couldn't find anything on this topic :-/

share|improve this question
add comment

3 Answers

Part of that can be solved with git-describe and there's actually a grunt plug-in that wraps that for you (https://github.com/mikaelkaron/grunt-git-describe/).

The second part you'd have to do manually (right now), but I actually have a similar problem at the moment, so I can try to hack up a grunt plug-in for you (and myself).

Full disclosure, I'm the author of grunt-git-describe above.

share|improve this answer
add comment

Automatically at what point? Possibilities are:

  1. Update the version field every time you run a build from a machine environment
  2. Update the version field every time you run any build.
  3. Update the version field in a git hook to keep in sync. Personally, I'd be nervous about a hook that performs a change when something changes. This strategy is highest risk, highest reward.

If you go with the first or second approach, it depends of course on what build tool you use. If you use grunt, see about a grunt plugin. I can't find any grunt plugins that do what you're asking for, but you can create one fairly easily.


If you do end up creating one, let me know as I am also in need of a similar process :)

In my case, I am using SVN, but want the same pattern. I want to put the SVN revision number as my build number.

My recommendation is to leave the build number blank in the file that is checked in and have your build environment do a git clone for a new build and update the build number. Then built packages always have something about them indicating the git commit they came from.

I think that for development, you don't really need it because you can always ask git which version you have checked out. There is a grunt-git plugin which you could maybe use to figure out the version in dev. (maybe git show?)

share|improve this answer
I just discovered git describe. It may be useful as well. It gives you both the abbreviated sha1 commit id and a count of commits since the most recent tag. kernel.org/pub/software/scm/git/docs/git-describe.html –  Brandon Apr 8 '13 at 14:44
add comment

git rev-parse HEAD will write out the last commit to the current head branch which you could then append to a version number in your package whenever you run your build.

There are a number of git rev-xxx commands that might be useful for any additional stuff you might want to record.

share|improve this answer
add comment

Your Answer


