Master Git commits with useGit - from basic commits to amends, fixups, and custom authors
Commits are the building blocks of your Git history. This guide covers everything from basic commits to advanced operations like amending, fixups, and custom authorship.
import { commit } from 'usegit';await commit( 'fix: resolve memory leak in event listeners', 'The component was not properly cleaning up event listeners on unmount, causing memory to accumulate over time. This adds proper cleanup in the useEffect hook.');
Use descriptive commit messages that explain the “why” not just the “what”. Your future self (and teammates) will thank you!
Stage and commit all tracked files in one operation:
import { commitAll } from 'usegit';// Stage and commit all modified tracked filesawait commitAll('chore: update configuration files');// With descriptionawait commitAll( 'refactor: simplify authentication flow', 'Removes unnecessary abstraction layers and consolidates auth logic into a single service');
commitAll() only stages modified tracked files. It does not include new untracked files. Use add() first if you need to include new files.
import { add, commitAmend } from 'usegit';// Make additional changesawait add(['src/index.ts']);// Amend the previous commitawait commitAmend('feat: add user profile page with avatar support');
Add files to the last commit without changing the message:
import { add, commitAmend } from 'usegit';// Stage forgotten filesawait add(['tests/profile.test.ts']);// Amend without providing a new messageawait commitAmend();
Create commits with a Signed-off-by trailer (common in open source):
import { commitSignoff } from 'usegit';// Commit with automatic signoffawait commitSignoff('docs: update installation guide');// With descriptionawait commitSignoff( 'feat: implement OAuth2 authentication', 'Adds support for OAuth2 flow with Google and GitHub providers');
This produces a commit message like:
feat: implement OAuth2 authenticationAdds support for OAuth2 flow with Google and GitHub providersSigned-off-by: Your Name <[email protected]>
import { commitWithAuthor } from 'usegit';// Commit with custom authorawait commitWithAuthor( 'feat: initial code import', 'Alice Johnson <[email protected]>');// With descriptionawait commitWithAuthor( 'fix: resolve edge case in validation', 'Bob Smith <[email protected]>', 'This fix handles the case where input is an empty array');
import { commit } from 'usegit';await commit( 'docs: add API documentation', 'Complete API reference with examples', { '--author': 'Jane Doe <[email protected]>' });
Create a commit marked for autosquashing during rebase:
import { commitFixup } from 'usegit';// Create a fixup commit for a specific commitawait commitFixup('abc1234');
Fixup commits are designed to be automatically squashed during an interactive rebase with the --autosquash option. They’re useful for fixing up commits in a feature branch before merging.
import { add } from 'usegit';// Stage related changes togetherawait add(['src/auth/', 'tests/auth/']);
3
Review staged changes
import { diffStaged } from 'usegit';const staged = await diffStaged();console.log('About to commit:', staged);
4
Create the commit
import { commit } from 'usegit';await commit( 'feat(auth): implement JWT token refresh', 'Adds automatic token refresh when tokens are close to expiration. Includes retry logic and error handling.');
import { add, commit, commitAmend } from 'usegit';// Scenario 1: Forgot to add a fileawait commit('feat: add payment integration');// Oops, forgot to add the test file!await add(['tests/payment.test.ts']);await commitAmend('feat: add payment integration'); // Same message, adds the file// Scenario 2: Typo in commit messageawait commit('feat: add paymet integration'); // Typo!await commitAmend('feat: add payment integration'); // Fixed// Scenario 3: Need to update the descriptionawait commitAmend( 'feat: add payment integration', 'Integrates Stripe API for credit card processing. Includes webhook handling for payment events.');