Get the current git branch name using Node.js
npm install node-git-current-branch --save
const getCurrentBranchName = require('node-git-current-branch');
getCurrentBranchName(); // branch name or false
Returns branch name or false
Type: string
Default: process.cwd()
Required: false
The path which you want to get it's git repository's branch name.
Actually you don't need to pass anything, just call the function anywhere inside your git repository.
v2.0.x
no longer requires you to pass the root path which you initialized your git repository, it will automatically find the branch name if it exists (git initialized).