Skip to main content

listBranches

List all local branches in the repository.

Signature

function listBranches(): Promise<string[]>
Source: /home/daytona/workspace/source/src/lib/branch/listBranches.ts:14

Returns

result
Promise<string[]>
Array of local branch names

Examples

import { listBranches } from 'usegit';

const branches = await listBranches();
console.log(branches);
// ['main', 'feature/login', 'feature/dashboard']
// Check if a specific branch exists in local branches
const branches = await listBranches();
const hasFeature = branches.includes('feature/login');

if (hasFeature) {
  console.log('Feature branch exists locally');
}

listAllBranches

List all branches (local and remote) with configurable output format.

Signature

function listAllBranches<T extends BranchListFormat>(
  format?: T
): Promise<BranchListResult<T>>
Source: /home/daytona/workspace/source/src/lib/branch/listAllBranches.ts:15-17

Parameters

format
BranchListFormat
default:"flat"
Output format for the branch list
  • "flat" - Returns a simple array of all branch names
  • "json" - Returns an object with separate arrays for local/remote branches
  • "csv" - Returns a CSV string with type and name columns

Returns

result
Promise<BranchListResult<T>>
Branch list in the specified format:

Examples

import { listAllBranches } from 'usegit';

// Get flat array of all branches
const allBranches = await listAllBranches('flat');
console.log(allBranches);
// ['main', 'feature/login', 'remotes/origin/main']
// Get structured JSON format
const branches = await listAllBranches('json');

console.log('Local branches:', branches.local);
console.log('Remote branches:', branches.remote);
console.log('Default branch:', branches.head);

// Output:
// Local branches: ['main', 'feature/login', 'develop']
// Remote branches: ['origin/main', 'origin/develop', 'origin/feature/payment']
// Default branch: 'origin/main'
// Get CSV format for export
const csv = await listAllBranches('csv');
console.log(csv);

// Output:
// type,name
// local,main
// local,develop
// remote,origin/main
// head,origin/main
// Default format (flat)
const branches = await listAllBranches();
// Returns string[]

listRemoteBranches

List all remote-tracking branches.

Signature

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

Returns

result
Promise<string[]>
Array of remote branch names in the format remote/branch (e.g., "origin/main")

Examples

import { listRemoteBranches } from 'usegit';

const remoteBranches = await listRemoteBranches();
console.log(remoteBranches);
// ['origin/main', 'origin/develop', 'upstream/main']
// Find all branches on a specific remote
const remoteBranches = await listRemoteBranches();
const originBranches = remoteBranches.filter(b => b.startsWith('origin/'));

console.log('Branches on origin:', originBranches);
// ['origin/main', 'origin/develop', 'origin/feature/login']

branchExists

Check if a local branch exists in the repository.

Signature

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

Parameters

name
string
required
The name of the branch to check

Returns

result
Promise<boolean>
true if the branch exists locally, false otherwise

Examples

import { branchExists } from 'usegit';

const exists = await branchExists('main');
console.log(exists); // true

const missing = await branchExists('nonexistent-branch');
console.log(missing); // false
// Conditional branch creation
import { branchExists, createBranch } from 'usegit';

const branchName = 'feature/new-feature';

if (await branchExists(branchName)) {
  console.log(`Branch ${branchName} already exists`);
} else {
  await createBranch(branchName);
  console.log(`Created branch ${branchName}`);
}
This function only checks local branches. It does not check remote-tracking branches. For remote branches, use listRemoteBranches and check the array.

currentBranch

Get the name of the current branch.

Signature

function currentBranch(): Promise<string>
Source: /home/daytona/workspace/source/src/lib/branch/currentBranch.ts:14

Returns

result
Promise<string>
The name of the current branch

Examples

import { currentBranch } from 'usegit';

const branch = await currentBranch();
console.log(`Currently on branch: ${branch}`);
// Currently on branch: main
// Verify current branch before operations
import { currentBranch, deleteBranch } from 'usegit';

const current = await currentBranch();
const branchToDelete = 'feature/old';

if (current === branchToDelete) {
  console.error('Cannot delete the current branch. Switch branches first.');
} else {
  await deleteBranch(branchToDelete);
}
// Display branch in CLI output
const branch = await currentBranch();
console.log(`[${branch}] Running tests...`);
// [feature/login] Running tests...

getDefaultBranch

Get the repository’s default branch (the branch that remote HEAD points to).

Signature

function getDefaultBranch(): Promise<string | undefined>
Source: /home/daytona/workspace/source/src/lib/branch/getDefaultBranch.ts:8

Returns

result
Promise<string | undefined>
The name of the default branch (e.g., "main" or "develop"), or undefined if it cannot be determined

Examples

import { getDefaultBranch } from 'usegit';

const defaultBranch = await getDefaultBranch();
console.log(`Default branch: ${defaultBranch}`);
// Default branch: main
// Fallback to main if default cannot be determined
const defaultBranch = await getDefaultBranch() ?? 'main';
console.log(`Using branch: ${defaultBranch}`);
// Create feature branch from default branch
import { getDefaultBranch, createBranch } from 'usegit';

const base = await getDefaultBranch();
if (base) {
  console.log(`Creating feature branch from ${base}`);
  await createBranch('feature/new-feature');
} else {
  console.error('Could not determine default branch');
}
This function extracts the branch name from the remote HEAD reference. It returns undefined if the repository has no remotes or if HEAD is not set.

Build docs developers (and LLMs) love