gitjs

A wrapper for running git commands

gitjs

A Node.js module for interacting with git repositories.

$ npm install gitjs
var git = require('gitjs');

Opens a git repository so git commands can be run. If the autoCreate parameter is given and is truthy, then the open function will init a new repository if one is not already in the given directory.

git.open('/path/to/repo', function(errrepo) {
    
    // ... 
    
});

Intializes a new git repo in the given directory.

git.init('/path/to/project', function(errrepo) {
    
    // ... 
    
});

Runs a git command in the repository. If an args array is given, it will be used to populate ? placeholders in the command. As an example, on the command line you would create a new branch like so:

$ git branch foo

The same could be done with gitjs like this:

repo.run('branch foo', function(errstdoutstderr) {
    
    // ... 
    
});

Also, you could make the branch name variable using the args parameter like this:

function createBranch(repobranchName) {
    repo.run('branch ?', [branchName], function(errstdoutstderr) {
        
        // ... 
        
    });
}

Stages files to be committed.

// All of the following are acceptable formats for files 
 
repo.add('*', function(errstdoutstderr) {
    
    // ... 
    
});
 
repo.add('.', ...);
 
repo.add('foo.txt bar.txt', ...);
 
repo.add([ 'baz.txt', 'foo.html', 'bar.css' ], ...);

Commits the repository changes.

repo.commit('Changed foo.txt', function(errstdoutstderr) {
    
    // ... 
    
});

Commits the repository changes with a git commit -a.

Does a local clone of the repository to the given target path.

repo.cloneTo('/path/to/clone', function(errstdoutstderr) {
    
    // ... 
    
});

Runs a git clean. If the dirs parameter is truthy, a -d flag will be used.

Gets an array of branch names for the repository. If the giveObjs parameter is given and is truthy, objects about the branches will be given instead of just names.

repo.listBranches(function(errbranches) {
    
    console.log(branches[0]);  // "master" 
    
});
 
repo.listBranches(true, function(errbranches) {
    
    console.log(branches[0]);  // { name: "master", isCurrent: true } 
    
});

Gets the name of the current branch.

repo.currentBranch(function(errbranch) {
    
    console.log(branch);  // "master" 
    
});

Gets the names of all remotes for the repository.

repo.listRemotes(function(errremotes) {
    
    console.log(remotes[0]);  // "origin" 
    
});

Determines if a given remote exists.

repo.remoteExists('origin', function(errexists) {
    
    console.log(exists);  // true 
    
});

Creates a new branch.

repo.createBranch('dev', function(errstdoutstderr) {
    
    // ... 
    
});

Deletes a branch from the repository. If the force parameter is given and is truthy, the -f flag will be given, forcing the branch to be deleted, even if it has unmerged changes.

Checks out the given branch.

repo.checkout('master', function(errstdoutstderr) {
    
    // ... 
    
});

Runs a git status --porcelain -z command and parses the results into an array of files and the changes. Each value in the array is an object with the file name and the x and y change flags as defined in the git man pages.

{
    x: 'M', y: ' ',
    file: 'foo.txt'
}

Pushing commits to git

repo.push(function(errstdoutstderr) {
    
    // ... 
    
});