Creates an instance of the Git class.
Configuration options for the Git instance.
Stages one or more files or directories for the next commit.
This method wraps git add
to prepare specified files or directories for commit.
You can provide a single path or an array of paths. By default, it stages all changes.
The file(s) or directory path(s) to stage.
Use "."
to stage all changes. If an array is provided, all listed paths will be staged.
A promise that resolves with the command's stdout if successful.
Creates, deletes, renames, or lists branches.
Optional
name: stringOptional branch name. Required for some flags.
Optional branch flags like -d
, -m
, --list
, etc.
A promise that resolves with the result of the Git command.
Switches to the given branch or commit, optionally creating it.
The branch, tag, or commit hash to checkout.
Optional
flags: CheckoutFlag | CheckoutFlag[]Optional checkout flags like -b
, --orphan
, etc.
A promise that resolves with the result of the Git command.
Clones a Git repository into the current directory or specified folder.
The Git repository URL to clone.
Optional directory to clone into.
A promise that resolves to the Git instance.
Commits staged changes to the repository with a custom message and optional flags.
This method wraps git commit -m "<message>"
with support for additional commit flags.
It safely escapes double quotes in the commit message to avoid shell issues.
The commit message to use. Will be wrapped in quotes and escaped.
Optional
flags: CommitFlag | CommitFlag[]Optional list of commit flags to customize the commit behavior.
Each flag must be a valid CommitFlag
value.
A promise that resolves with the command's stdout if the commit succeeds.
const git = new Git({ cwd: "/repo" });
await git.commit("feat: add login API");
await git.commit("fix: typo", ["--amend", "--no-edit"]);
Runs git describe
to generate a human-readable identifier for a commit.
This wraps the git describe
command and returns a string such as
v1.2.3-2-gabcdef
based on the most recent tag and commit information.
One or more optional git describe
flags to customize the output.
Can be a single flag or an array of flags.
Optional
ref: stringOptional Git reference to describe (e.g., a branch, tag, or commit hash).
Defaults to HEAD
if not provided.
A promise that resolves with the git describe
output.
Retrieves the name of the current branch.
A promise that resolves with the current branch name.
Retrieves the default branch name of the repository (e.g., main
or master
).
Falls back to "main" if the default branch cannot be determined.
A promise that resolves with the default branch name.
Retrieves the latest reachable Git tag (e.g., v1.2.3
) without commit metadata.
This uses git describe --tags --abbrev=0
to return only the most recent tag name,
ignoring additional suffixes like commit counts or hashes.
A promise that resolves with the latest tag as a string.
Retrieves the configured Git user email.
A promise that resolves with the user's email.
Retrieves the configured Git user name.
A promise that resolves with the user's name.
Checks if there are no staged but uncommitted changes.
A promise that resolves to true
if there are no staged changes, otherwise false
.
Checks if there are no unstaged changes in the working directory.
A promise that resolves to true
if there are no unstaged changes, otherwise false
.
Checks if the current branch has an upstream branch configured.
A promise that resolves to true
if an upstream branch is set, false
otherwise.
Initializes a new Git repository in the working directory.
A promise that resolves to the Git instance.
Checks if the working directory is completely clean i.e., no staged or unstaged changes.
A promise that resolves to true
if the working directory is fully clean, otherwise false
.
const git = new Git({ cwd: "/repo" });
const isClean = await git.isWorkingDirClean();
console.log(isClean); // true if no changes, false if dirty
Pushes changes to the specified remote and optionally to a specific branch.
A promise that resolves with the result of the Git command.
Resets the current HEAD to the specified commit hash, with an optional behavior flag.
This method wraps git reset <hash> <flag>
and moves the current branch pointer to the given commit.
It does not modify the working directory or the index unless additional flags (e.g., --hard
, --soft
) are added manually.
The target commit hash to reset to. This must be a valid Git commit SHA (full or abbreviated).
Optional
flag: ResetFlagOne or more Git reset flags.
Examples: "--soft"
, "--hard"
.
A promise that resolves with the command's stdout if the reset succeeds.
Restores working tree files from the index or a specified source.
This method wraps git restore
to unstage files or restore their contents
from the index (staged) or from a specific commit/branch.
One or more file paths to restore.
Use "."
to restore all tracked files. When using an array, all paths will be included.
Optional
flag: RestoreFlagAn optional Git restore flag like --staged
or --source=<commit>
.
Use this to restore from a specific source or unstage changes.
A promise that resolves with the command's output on success.
Sets the global Git user email.
This command configures the user.email
value in the global Git configuration.
It wraps git config --global user.email "<email>"
.
The Git user email address to set.
A promise that resolves when the email has been successfully set.
Sets the global Git user name.
This command configures the user.name
value in the global Git configuration.
It wraps git config --global user.name "<name>"
.
The Git user name to set. If it includes spaces, it will be quoted automatically.
A promise that resolves when the name has been successfully set.
Creates, deletes, or lists Git tags with optional flags.
A promise that resolves with the result of the Git command.
A smart, user-friendly Git utility class for Node.js CLIs.
Git
provides a high-level interface for interacting with Git repositories using simple, intuitive commands. Designed for use in GitNifty, it wraps common Git operations like checking repository status, retrieving user info, detecting upstream branches, and more — all with clean automation and helpful defaults.This class is built to make version control effortless for developers who want precision and productivity, without dealing with complex Git shell commands directly.
Example
Remarks
child_process.exec
under the hood.hasUpstreamBranch()
orisWorkingDirClean()
returnfalse
instead of throwing.See