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
Configuration options for initializing the repository
Array of Git flags to modify initialization behaviorOptions:
"--quiet" - Only print error and warning messages
"--bare" - Create a bare repository without a working directory
The name of the initial branch in the newly created repository.Options:
"main" (default)
"master"
Specify the directory from which templates will be used. Template files allow you to customize the initial repository structure.
Specify the hash algorithm to use for the repository.Options:
"sha1" (default) - Traditional SHA-1 hash
"sha256" - Modern SHA-256 hash (requires Git 2.29+)
Instead of initializing the repository as a directory to $GIT_DIR or ./.git/, create a text file there containing the path to the actual repository.
Returns
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