Multi-purpose function for listing, creating, renaming, copying, or deleting branches. This is a low-level function that provides direct access to Git branch operations using flags.
For most use cases, prefer the specialized functions like createBranch, deleteBranch, or copyBranch which provide better error handling and convenience.
Throws if the branch already exists with message: Cannot create branch "[name]": branch already exists. Use a different name or delete the existing branch first.
import { deleteBranch } from 'usegit';// Delete a fully merged branchawait deleteBranch('feature/login');// Force delete a branch with unmerged changesawait deleteBranch('feature/incomplete', { force: true });
// Clean up multiple feature branchesconst oldBranches = ['feature/old-1', 'feature/old-2', 'feature/old-3'];for (const branch of oldBranches) { await deleteBranch(branch);}
import { copyBranch } from 'usegit';// Create a backup of the main branchawait copyBranch('main', 'backup-main');// Force copy to overwrite existing target branchawait copyBranch('main', 'backup-main', { force: true });
// Create a branch copy for experimentationawait copyBranch('feature/stable', 'feature/experiment');// Work on the experimental branch without affecting the stable one
Copying a branch does not change your current working branch. Use Git checkout functions to switch to the newly copied branch if needed.