Skip to main content

Overview

The init() function creates an empty Git repository or reinitializes an existing one. This is equivalent to running git init from the command line.
By default, useGit creates repositories with main as the initial branch name, following modern Git best practices.

Syntax

function init(options?: InitOptions): Promise<string>

Parameters

options
InitOptions
default:"{}"
Configuration options for initializing the repository

Returns

result
Promise<string>
A promise that resolves with the output message from Git, typically indicating the repository was initialized successfully.

Usage Examples

Basic Initialization

Initialize a new Git repository with default settings:
import { git } from 'use-git'

await git.init()
// Initializes repository with 'main' as the initial branch

Initialize with Master Branch

Use master as the initial branch name:
import { git } from 'use-git'

await git.init({ 
  '--initial-branch': 'master' 
})

Create a Bare Repository

Create a bare repository (useful for remote repositories):
import { git } from 'use-git'

await git.init({ 
  flags: ['--bare'] 
})

Quiet Mode

Initialize without verbose output:
import { git } from 'use-git'

await git.init({ 
  flags: ['--quiet'] 
})

Custom Template Directory

Use a custom template directory:
import { git } from 'use-git'

await git.init({ 
  '--template': '/path/to/templates' 
})

SHA-256 Repository

Create a repository using SHA-256 hashing:
import { git } from 'use-git'

await git.init({ 
  '--object-format': 'sha256' 
})
SHA-256 repositories require Git version 2.29 or later and may have compatibility issues with older Git clients.

Multiple Options

Combine multiple initialization options:
import { git } from 'use-git'

await git.init({ 
  '--initial-branch': 'main',
  '--object-format': 'sha1',
  flags: ['--quiet']
})
  • clone - Clone an existing repository
  • createGit - Create a Git instance with custom working directory

Git Documentation

For more information about git init, see the official Git documentation.

Source Code

Location: src/lib/init/init.ts:11

Build docs developers (and LLMs) love