Git and node.js

Git is a free and open source distributed version control system designed to handle everything from small to very large projects with speed and efficiency.

Git is easy to learn and has a tiny footprint with lightning fast performance. It outclasses SCM tools like Subversion, CVS, Perforce, and ClearCase with features like cheap local branching, convenient staging areas, and multiple workflows.

Using Simple Git

A light weight interface for running git commands in any node.js application.

Installation – Easiest through npm: npm install simple-git

Dependencies – Requires git to be installed and that it can be called using the command git.

Usage – Include into your app using:

const simpleGit = require(‘simple-git’)(workingDirPath);

where the workingDirPath is optional, defaulting to the current directory.

Use simpleGit by chaining any of its functions together. Each function accepts an optional final argument which will be called when that step has been completed. When it is called it has two arguments – firstly an error object (or null when no error occurred) and secondly the data generated by that call.

Authentication – The easiest way to supply a username / password to the remote host is to include it in the URL, for example:

const USER = ‘something’;

const PASS = ‘somewhere’;

const REPO = ‘github.com/username/private-repo’;

const git = require(‘simple-git/promise’);

const remote = `https://${USER}:${PASS}@${REPO}`;

git().silent(true)

.clone(remote)

.then(() => console.log(‘finished’))

.catch((err) => console.error(‘failed: ‘, err));

Be sure to enable silent mode to prevent fatal errors from being logged to stdout.

Environment Variables – Pass one or more environment variables to the child processes spawned by simple-git with the .env method which supports passing either an object of name=value pairs or setting a single variable at a time:

const GIT_SSH_COMMAND = “ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no”;

const git = require(‘simple-git’);

git()

.env(‘GIT_SSH_COMMAND’, GIT_SSH_COMMAND)

.status((err, status) => { /*  */ })

const gitP = require(‘simple-git/promise’);

gitP().env({ …process.env, GIT_SSH_COMMAND })

.status()

.then(status => { })

.catch(err => {});

Get industry recognized certification – Contact us

Menu