Skip to main content

renameBranch

Rename an existing branch. Includes validation to prevent accidental overwrites.

Signature

function renameBranch(
  from: string,
  to: string,
  opts?: { force?: boolean }
): Promise<string>
Source: /home/daytona/workspace/source/src/lib/branch/renameBranch.ts:18-29

Parameters

from
string
required
The current name of the branch to rename
to
string
required
The new name for the branch
opts
object

Returns

result
Promise<string>
Success message from Git

Errors

Error
Error
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.

Examples

import { renameBranch } from 'usegit';

// Rename a branch
await renameBranch('old-name', 'new-name');
// Force rename to overwrite existing branch
await renameBranch('old-name', 'existing-name', { force: true });
// Handle rename errors
try {
  await renameBranch('feature/v1', 'feature/v2');
  console.log('Branch renamed successfully');
} catch (error) {
  console.error('Rename failed:', error.message);
  // Error: Cannot rename branch "feature/v1" to "feature/v2": 
  // a branch named "feature/v2" already exists.
}
// Rename with validation
import { renameBranch, branchExists } from 'usegit';

const oldName = 'feature/old-api';
const newName = 'feature/new-api';

if (await branchExists(newName)) {
  console.log('Target branch already exists. Use force option to overwrite.');
} else {
  await renameBranch(oldName, newName);
}
Using force: true will overwrite the target branch if it exists, potentially losing that branch’s commit history.

setBranchUpstream

Set the upstream tracking branch for a local branch. This configures which remote branch a local branch tracks.

Signature

function setBranchUpstream(
  branchName: string,
  upstream: string
): Promise<string>
Source: /home/daytona/workspace/source/src/lib/branch/setBranchUpstream.ts:13-16

Parameters

branchName
string
required
The name of the local branch
upstream
string
required
The upstream branch in the format remote/branch (e.g., "origin/main" or "upstream/develop")

Returns

result
Promise<string>
Success message from Git confirming the upstream was set

Examples

import { setBranchUpstream } from 'usegit';

// Set upstream for a feature branch
await setBranchUpstream('feature/login', 'origin/feature/login');
// Set up a local branch to track a remote branch
await setBranchUpstream('develop', 'origin/develop');

// Now git push/pull will use origin/develop by default
// Set upstream to a different remote
await setBranchUpstream('main', 'upstream/main');

// Track from upstream instead of origin
// Create and configure a new branch
import { 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.

resetBranchToHead

Reset a branch to point to HEAD. This forcefully moves a branch pointer to the current HEAD position.

Signature

function resetBranchToHead(name: string): Promise<string>
Source: /home/daytona/workspace/source/src/lib/branch/resetBranchToHead.ts:13

Parameters

name
string
required
The name of the branch to reset

Returns

result
Promise<string>
Success message from Git

Examples

import { resetBranchToHead } from 'usegit';

// Reset a branch to current HEAD
await resetBranchToHead('feature/login');
// Move a branch pointer to match current position
// Useful when you want to update a branch reference
await resetBranchToHead('release/v1.0');

console.log('Branch pointer updated to current HEAD');
// Common workflow: update a branch to match current work
import { resetBranchToHead, currentBranch } from 'usegit';

const current = await currentBranch();
console.log(`Current branch: ${current}`);

// Reset another branch to point to the same commit
await 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.

Use Cases

Setting Up a New Feature Branch

import { createBranch, setBranchUpstream, currentBranch } from 'usegit';

const featureName = 'feature/payment-integration';

// Create the branch
await createBranch(featureName);

// Switch to it (using git checkout - not shown)
// ...

// Set up upstream tracking
await setBranchUpstream(featureName, `origin/${featureName}`);

const current = await currentBranch();
console.log(`Now on ${current} with upstream configured`);

Renaming a Branch Safely

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');

Creating a Backup Branch

import { copyBranch, resetBranchToHead } from 'usegit';

// Create a backup before making changes
await copyBranch('main', 'main-backup');

// ... make changes ...

// If needed, restore from backup
await resetBranchToHead('main-backup');

Build docs developers (and LLMs) love