nodejs 获取git 当前分支名,hash 值

6,857 阅读1分钟

git获取当前分支名,hash值的方式参考这篇git 获取当前分支,当前hash值。 在nodejs中使用需要使用execa 这个库帮助我们执行git命令,可以封装两个方法:

function getGitHash() {
  const res = execa.commandSync('git rev-parse --short HEAD');
  return res.stdout;
}

function getGitBranch() {
  const res = execa.commandSync('git rev-parse --abbrev-ref HEAD');
  return res.stdout;
}