Master Git branching with useGit - create, list, switch, rename, and delete branches
Branches allow you to develop features, fix bugs, or experiment in isolated environments. This guide covers comprehensive branch management with useGit.
import { branch } from 'usegit';// Create a new branchawait branch('feature/api-integration');// Create a branch from a specific commitawait branch('hotfix/security-patch', { flags: ['--force']});
import { renameBranch } from 'usegit';// Rename the current branchawait renameBranch('old-name', 'new-name');// Force rename (overwrite if new name exists)await renameBranch('feature/old', 'feature/new', { force: true});
import { deleteBranch } from 'usegit';// Delete a merged branchawait deleteBranch('feature/completed-feature');// Force delete an unmerged branchawait deleteBranch('feature/abandoned', { force: true});
Use force: true when deleting a branch that hasn’t been merged. Be careful - this will permanently delete the branch and its commits if they’re not referenced elsewhere.
import { add, commit } from 'usegit';// Make changes to your filesawait add('.');await commit( 'feat: add dashboard layout', 'Implements the basic dashboard structure with navigation');
3
Continue development
// Make more changesawait add(['src/components/Dashboard.tsx']);await commit('feat: add dashboard widgets');
4
Clean up after merge
import { deleteBranch } from 'usegit';// After merging into main, delete the feature branchawait deleteBranch('feature/new-dashboard');
Before switching or deleting branches, check their status:
import { listBranches, currentBranch, branchExists, status} from 'usegit';async function branchInfo() { // Get current branch const current = await currentBranch(); console.log(`Current branch: ${current}`); // List all local branches const branches = await listBranches(); console.log('Available branches:', branches); // Check if a branch exists const exists = await branchExists('feature/login'); console.log('feature/login exists:', exists); // Check working tree status const statusResult = await status(); console.log(statusResult);}branchInfo();