Core repository operations for initializing, cloning, and managing Git repositories. These are callback-based functions that execute Git commands directly.
Creates an empty Git repository using git init.
/**
* Initializes a git repository using git init
* @param opt - Configuration options (optional)
* @param cb - Callback function (optional if opt provided)
*/
function init(opt?: RepoOptions, cb?: Callback): void;
function init(cb?: Callback): void;
interface RepoOptions extends BaseOptions {
// Inherits: cwd, args, quiet, maxBuffer
}Usage Examples:
const git = require('gulp-git');
// Basic initialization
git.init(function(err) {
if (err) throw err;
console.log('Repository initialized');
});
// Initialize with options
git.init({ args: '--bare' }, function(err) {
if (err) throw err;
console.log('Bare repository initialized');
});
// Initialize in specific directory
git.init({ cwd: './my-repo' }, function(err) {
if (err) throw err;
console.log('Repository initialized in ./my-repo');
});
// Initialize with quiet mode
git.init({ args: '--quiet', quiet: true }, function(err) {
if (err) throw err;
});Clones a remote repository for the first time using git clone.
/**
* Clones a git repository using git clone
* @param remote - Remote repository URL
* @param opt - Configuration options (optional)
* @param cb - Callback function (optional if opt provided)
*/
function clone(remote: string, opt?: RepoOptions, cb?: Callback): void;
function clone(remote: string, cb?: Callback): void;Usage Examples:
// Basic clone
git.clone('https://github.com/user/repo.git', function(err) {
if (err) throw err;
console.log('Repository cloned');
});
// Clone to specific directory
git.clone('https://github.com/user/repo.git', {
args: './custom-folder'
}, function(err) {
if (err) throw err;
console.log('Repository cloned to ./custom-folder');
});
// Clone with specific branch
git.clone('https://github.com/user/repo.git', {
args: '--branch develop'
}, function(err) {
if (err) throw err;
console.log('Develop branch cloned');
});
// Clone shallow copy
git.clone('https://github.com/user/repo.git', {
args: '--depth 1'
}, function(err) {
if (err) throw err;
console.log('Shallow clone completed');
});Removes untracked files from the working tree using git clean.
/**
* Cleans untracked files using git clean
* @param paths - File paths to clean (optional)
* @param opt - Configuration options (optional)
* @param cb - Callback function
*/
function clean(paths?: string, opt?: RepoOptions, cb?: Callback): void;
function clean(opt?: RepoOptions, cb?: Callback): void;
function clean(cb?: Callback): void;Usage Examples:
// Clean all untracked files
git.clean(function(err) {
if (err) throw err;
console.log('Untracked files cleaned');
});
// Clean with force flag
git.clean({ args: '-f' }, function(err) {
if (err) throw err;
console.log('Files forcefully cleaned');
});
// Clean specific paths
git.clean('./temp/*', { args: '-f' }, function(err) {
if (err) throw err;
console.log('Temp directory cleaned');
});
// Clean directories and files
git.clean({ args: '-fd' }, function(err) {
if (err) throw err;
console.log('Files and directories cleaned');
});
// Dry run - see what would be cleaned
git.clean({ args: '-n' }, function(err) {
if (err) throw err;
console.log('Dry run completed');
});const gulp = require('gulp');
const git = require('gulp-git');
gulp.task('setup-repo', function(done) {
// Initialize repository
git.init(function(err) {
if (err) return done(err);
// Add remote
git.addRemote('origin', 'https://github.com/user/repo.git', function(err) {
if (err) return done(err);
console.log('Repository setup complete');
done();
});
});
});gulp.task('clone-setup', function(done) {
git.clone('https://github.com/user/repo.git', './project', function(err) {
if (err) return done(err);
// Change to project directory for subsequent operations
process.chdir('./project');
// Install dependencies or perform other setup
console.log('Project cloned and ready');
done();
});
});gulp.task('cleanup', function(done) {
// Clean untracked files first
git.clean({ args: '-f' }, function(err) {
if (err) return done(err);
// Reset any uncommitted changes
git.reset('HEAD', { args: '--hard' }, function(err) {
if (err) return done(err);
console.log('Repository cleaned and reset');
done();
});
});
});function handleGitError(err, operation) {
if (err) {
console.error(`Git ${operation} failed:`, err.message);
// Handle specific error types
if (err.message.includes('already exists')) {
console.log('Repository already exists, continuing...');
return false; // Don't throw
}
throw err;
}
return true;
}
gulp.task('safe-init', function(done) {
git.init(function(err) {
if (handleGitError(err, 'init')) {
console.log('Repository initialized successfully');
}
done();
});
});