Throws if the target branch already exists (when force is false) with message: Cannot rename branch "[from]" to "[to]": a branch named "[to]" already exists. Use { force: true } to overwrite it.
import { setBranchUpstream } from 'usegit';// Set upstream for a feature branchawait setBranchUpstream('feature/login', 'origin/feature/login');
// Set up a local branch to track a remote branchawait setBranchUpstream('develop', 'origin/develop');// Now git push/pull will use origin/develop by default
// Set upstream to a different remoteawait setBranchUpstream('main', 'upstream/main');// Track from upstream instead of origin
// Create and configure a new branchimport { createBranch, setBranchUpstream } from 'usegit';const branchName = 'feature/user-profile';await createBranch(branchName);await setBranchUpstream(branchName, `origin/${branchName}`);console.log(`Branch ${branchName} is now tracking origin/${branchName}`);
Setting an upstream allows you to use git push and git pull without specifying the remote and branch name each time. It also enables Git to show how many commits ahead or behind your branch is.
import { resetBranchToHead } from 'usegit';// Reset a branch to current HEADawait resetBranchToHead('feature/login');
// Move a branch pointer to match current position// Useful when you want to update a branch referenceawait resetBranchToHead('release/v1.0');console.log('Branch pointer updated to current HEAD');
// Common workflow: update a branch to match current workimport { resetBranchToHead, currentBranch } from 'usegit';const current = await currentBranch();console.log(`Current branch: ${current}`);// Reset another branch to point to the same commitawait resetBranchToHead('backup-branch');console.log('backup-branch now points to the same commit');
This operation uses the --force flag and will move the branch pointer regardless of its current state. This can cause commits to become unreachable if the branch is moved backwards. Use with caution.
This function does not change your working directory or current branch. It only updates the specified branch’s pointer to match HEAD. To switch branches, use Git checkout functions.
import { createBranch, setBranchUpstream, currentBranch } from 'usegit';const featureName = 'feature/payment-integration';// Create the branchawait createBranch(featureName);// Switch to it (using git checkout - not shown)// ...// Set up upstream trackingawait setBranchUpstream(featureName, `origin/${featureName}`);const current = await currentBranch();console.log(`Now on ${current} with upstream configured`);
import { renameBranch, branchExists } from 'usegit';async function safeBranchRename(oldName: string, newName: string) { // Check if target name is available if (await branchExists(newName)) { throw new Error(`Branch ${newName} already exists`); } // Perform the rename await renameBranch(oldName, newName); console.log(`Successfully renamed ${oldName} to ${newName}`);}await safeBranchRename('feature/old-name', 'feature/new-name');
import { copyBranch, resetBranchToHead } from 'usegit';// Create a backup before making changesawait copyBranch('main', 'main-backup');// ... make changes ...// If needed, restore from backupawait resetBranchToHead('main-backup');