Get hash of most recent git commit in Node

后端 未结 7 1741
礼貌的吻别
礼貌的吻别 2020-12-24 01:57

I\'d like to get the id/hash of the most recent commit on the current branch in NodeJS.

In NodeJS, I\'d like to get the most recent id/hash, with respect to git and

7条回答
  •  有刺的猬
    2020-12-24 02:17

    Short solution, no external module needed (synchronous alternative to Edin's answer):

    revision = require('child_process')
      .execSync('git rev-parse HEAD')
      .toString().trim()
    

    and if you want to manually specify the root directory of the git project, use the second argument of execSync to pass the cwd option, like execSync('git rev-parse HEAD', {cwd: __dirname})

提交回复
热议问题