LinuxCommandLibrary

git-init

Initialize a new Git repository

TLDR

Initialize a new local repository

$ git init
copy

Initialize a repository with the specified name for the initial branch
$ git init [[-b|--initial-branch]] [branch_name]
copy

Initialize a repository using SHA256 for object hashes (requires Git version 2.29+)
$ git init --object-format sha256
copy

Initialize a barebones repository, suitable for use as a remote over SSH
$ git init --bare
copy

SYNOPSIS

git init [-b | --initial-branch=] [--bare] [--template=] [--separate-git-dir=] [--shared[=]] [--object-format=] []

PARAMETERS


    The directory where the Git repository should be created or reinitialized. If omitted, the current directory is used.

-b
    Specify the name for the initial branch in the newly created repository instead of the default name (e.g., 'main' or 'master'). This is an alias for --initial-branch.

--initial-branch=
    Similar to -b, specifies the name for the initial branch. This overrides the init.defaultBranch configuration.

--bare
    Create a 'bare' repository. This type of repository does not have a working directory, meaning no files are checked out. Bare repositories are typically used as central repositories on servers for sharing among multiple developers.

--template=
    Specify the directory from which templates will be used. These templates are copied into the new .git directory and can include custom hooks or Gitignore patterns.

--separate-git-dir=
    Create a Git repository in a specified directory, but store the actual .git directory contents in <git-dir> instead. This allows the working tree and the Git directory to be separate.

--shared[=]
    Set the repository permissions (e.g., group-writable). This is useful when multiple users need to interact with the same repository on a shared filesystem. Permissions can be 'false', 'true', 'umask', 'group', 'all', or octal digits.

--object-format=
    Specify the object format for the repository, such as 'sha1' (default) or 'sha256'. This determines the hash algorithm used for Git objects.

DESCRIPTION

The git init command creates a new empty Git repository or reinitializes an existing one. It is typically the first command you run when starting a new project that you want to put under version control. When executed, git init creates a subdirectory named .git within the current working directory (or the specified <directory>). This .git directory contains all the necessary repository infrastructure: objects, refs (pointers to commits like branches and tags), templates, hooks, and configuration files. It doesn't create any files in the working directory itself.

The command can also be used to reinitialize an existing repository. In this case, it doesn't remove the existing repository content, but rather adds missing parts or moves the .git directory if --separate-git-dir is used. For shared environments, the --shared option can be used to set permissions appropriately.

There are two main types of repositories created by git init: a standard repository with a working directory, and a bare repository, which consists only of the .git directory content and is primarily used as a central repository for collaboration (e.g., on a server).

CAVEATS

When reinitializing an existing repository, git init is a safe operation; it will not overwrite or destroy existing repository content, only add missing parts or move the .git directory if specified. However, using --bare on an existing non-bare repository can lead to unexpected behavior if not handled carefully, as it effectively removes the working tree context. Always ensure you understand the implications of creating bare repositories, especially when dealing with existing data.

DEFAULT BRANCH NAME

Traditionally, git init created a default branch named master. However, in recent Git versions and due to industry trends, the default name has largely shifted to main. You can configure the desired default branch name globally using git config --global init.defaultBranch <name>, or specify it directly for a specific repository using the --initial-branch option during initialization.

BARE VS. NON-BARE REPOSITORIES

Understanding the difference between bare and non-bare repositories is crucial. A non-bare repository (the default) contains both the .git directory and a working directory where your project files reside. A bare repository, created with --bare, contains only the .git directory's contents and no working directory. Bare repositories are primarily used as remote repositories that developers clone from and push to, serving as a central hub without being a development environment itself.

HISTORY

The git init command has been a fundamental part of Git since its very first public release by Linus Torvalds. It is the cornerstone for starting any new Git-managed project. A notable recent development, starting around Git version 2.28 (mid-2020), is the shift in the default initial branch name. Historically, the default branch created by git init was master. However, due to ongoing discussions about inclusive naming, many projects and Git hosting services (like GitHub) have moved towards main as the default. Git now supports configuring this default via the init.defaultBranch configuration option, or by explicitly using the --initial-branch (or -b) option during initialization.

SEE ALSO

Copied to clipboard