add git commit hash to javascript variables during gulp build

I am using gulp to build my angularjs application and want to somehow inject the latest git commit hash into my angular app so I can use it when debugging production issues.

Even if it is just a case of injecting it into my index.html template as a global variable that would be great.

  • Accesing Jenkin API from AngularJS
  • possible to change where github pages looks for index.html?
  • Bower install: No versions available
  • How to run “grunt build” during Heroku deployment (angular and ruby-on-rails app)
  • Blocked script execution in <URL>. because the document's frame is sandboxed and the 'allow-scripts' permission is not set
  • How can I get a bleeding edge version of angular.js from Github?
  • Can anyone recommend a good technique and plugins to achieve this?

  • What is the best way to discard all your changes (with untracked files) on a git branch?
  • There is nothing changed, but eclipse egit marks the file as changed
  • git: How do you check out the initial version of a file in a git repo
  • How do I fast-forward other tracking branches in git?
  • How to import a GIT non-Eclipse Java project into Eclipse?
  • Auto generate/remove jenkins jobs
  • 2 Solutions collect form web for “add git commit hash to javascript variables during gulp build”

    I ended up using git-rev-sync and gulp-replace and put {{git}} in my index.html source file.

    var git = require('git-rev-sync');
    return gulp.src(pathConfig.src + 'index.html')
        .pipe($.replace('{{git}}', git.long()))
        .pipe(gulp.dest(pathConfig.dest + 'index.html'));

    inject the latest git commit hash

    Here is how you can do it:

    git store the latest commit di inside the .git/refs/head<branch name>.
    Read this content of the file with the below code and then put it wherever you need

    # load fs for reading /writing files
    fs = require("fs"),
    // define your task 
    gulp.task('doSometing', function() {
      return gulp.src(dirs.src + '/templates/*.html')
        // read the  SHA-1 of the latest commit
        .pipe(fs.readFile("path/to/.git/refs/head<branch name>", 
              // Default encoding for fs is binary so we have to set it to UTF-8      
              // Process the data you have read (SHA-1)
              function(err, _data) {
              //do something with your data
    Git Baby is a git and github fan, let's start git clone.