Here’s a complete example showing a typical Git workflow:
1
Initialize the repository
import { init, add, commit, status } from 'usegit';// Create a new Git repositoryawait init();
2
Check repository status
// View the working tree statusconst statusOutput = await status();console.log(statusOutput);
3
Stage your files
// Add all files to stagingawait add('.');// Or add specific filesawait add(['src/index.ts', 'package.json']);
4
Create a commit
// Commit with a messageawait commit('feat: initial project setup');// Commit with message and descriptionawait commit( 'feat: add user authentication', 'Implements login and signup functionality with JWT tokens');
Before committing, you can check which files are staged and what changes they contain:
import { status, diffStaged } from 'usegit';// View status in short formatconst shortStatus = await status(undefined, { flags: ['--short', '--branch']});console.log(shortStatus);// View the actual changes that are stagedconst stagedChanges = await diffStaged();console.log(stagedChanges);
import { add } from 'usegit';// Stage a single fileawait add(['README.md']);// Stage multiple filesawait add(['src/index.ts', 'src/utils.ts']);// Stage a directoryawait add(['src/']);// Stage all files in current directoryawait add('.');
import { commit } from 'usegit';await commit( 'refactor: reorganize component structure', 'Moves components into feature-based directories for better organization. This improves code discoverability and maintains clearer separation of concerns.');
import { commitAll } from 'usegit';// Stage and commit all tracked files in one stepawait commitAll( 'chore: update dependencies', 'Bumps all packages to latest stable versions');
Use commitAll() when you want to commit all modified tracked files without explicitly running add() first. This is equivalent to git commit -a.