How to Run Git Through Node JS

How to Run Git Through Node JS

Use Node JS To Run Git Commands

Node JS has the ability to spawn child processes which allow you to execute commands from Javascript without using the terminal. Leveraging the power of Node JS alongside Bash Shell scripts can give you the freedom to execute commands during the lifecycle of your program, or even through the package.json prebuild state. Below I have put together a function which can used in tandem with process.argv to run commands through the shell, Git will be explored later.

var exec = require('child_process').exec;

var result = function(command, cb){

var child = exec(command, function(err, stdout, stderr){

if(err != null){

return cb(new Error(err), null);

}else if(typeof(stderr) != "string"){

return cb(new Error(stderr), null);


return cb(null, stdout);




exports.result = result;

This function expects a command to be given to it, and then it will run the command with the ability for a callback function to notify possible errors. While this is enough to get started, it's not enough to have Git be involved. Git should be aliased through the environment which can be tricky to run from Node. However, we can use Node to write shell scripts dynamically and then call on those scripts to carry out our commands.

We'll need to create a file to store our commands in. We'll need the first line in the shell script to be :

#! /usr/bin/bash

So that we can remain compatible cross-platform and also tell the interpreter that we are using bash to execute the process.

Add scripts below:

fs.appendFile('', command, (err) => {
if (err) throw err;

function runShell () {
execProcess.result("sh", function(err, response){
}else {

So far we should be able to take the input of our command and create a shell script which is going to be appended to the file and then execute the file. Now we can add support for Git. Class for commands:

const argv = process.argv
const operator = argv[2]
let args = argv.slice(3,argv.length)

class Command {
constructor(message, operator) {
this.operator = operator;
this.message = message.toString().replace(/,/g,' ');
this.command = '';
this.flag = '';
this.commit = '';

findFlag() {
let message = this.message
if (message.includes('-')) {
let flagIndex = message.slice(message.indexOf('-'), message.length)
flagIndex = flagIndex.split(' ')[0]
this.flag = flagIndex
this.message = message.split(flagIndex + ' ').pop()
} else {
this.flag = '.'

shell() {
this.command = "git add " + this.flag + " && git commit -m " + this.message
return this.command

module.exports = Command

Process.argv dependent switch statement which processes the operater

switch (operator) {
case 'ac':
let amount;
let input = new Command(args)
string = new Colorize(
string = string.success()
case 'i':


Follow the project on Github:

The Roadmap for This Project

  • Add Support For Pushing to Github
  • Add Support For GoLang style dependency manager using Github API
  • Add Support For Merging and Checkout Branches
  • Add Support For Creating Gitignore

Posted in: nodejs

backend nodejs shell

Leave a Comment