Skip to content

git init

Description

The git init command is used to generate a new, empty Git repository or to reinitialize an existing one. With the help of this command, a .git subdirectory is created, which includes the metadata, like subdirectories for objects and template files, needed for initializing the repository.

The git init usage

Using git init is the simplest way to set up version-controlled projects, as it handles repository creation and initialization automatically.

  • To initialize a working Git repository, simply cd into your project directory and run the git init command in your terminal.

git init

bash
git init
  1. Converts the current directory into a Git repository to track project changes. Alternatively, specify a path to create a new .git subdirectory in that location.

git init <directory>

bash
git init &lt;directory&gt;

If you run git init on a directory that already contains a .git subdirectory, it will safely reinitialize without overwriting the existing configuration.

The git init bare repositories

Central or remote repositories should be created with the --bare flag. The --bare flag creates a repository without a working directory, preventing direct file editing and commits. It effectively marks the repository as a storage facility. Bare repositories are used for git push and git pull, but you should never commit directly to them. Central repositories must be bare because pushing to a non-bare repository can overwrite changes. While local repositories are non-bare, central ones are always bare. The most common use for git init --bare is creating a remote central repository.

Bare

Template Directory

Templates are used to initialize a new repository with a .git subdirectory. The template directory will be one of the mentioned below:

  • the argument which is given with the --template option,
  • the contents of the $GIT_TEMPLATE_DIR environment variable,
  • the init.templateDir configuration variable,
  • the default template, which usually resides in a /usr/share/git-core/templates directory.

Default templates demonstrate how to use template features. Custom templates can define default directories and files that are automatically copied into the new .git subdirectory.

Configuration of git init

The git init &lt;directory&gt; command accepts an optional &lt;directory&gt; argument. If omitted, the command runs in the current directory. If the specified directory does not exist, Git creates it automatically. Additional options like --initial-branch and --shared allow you to set the default branch name and configure group permissions for shared access, respectively. Here is the full list of options:

FlagDescription
-q or --quietPrints only errors and warnings. All other output is concealed.
--bareCreates a bare repository.
--template=&lt;templatedirectory&gt;Specifies from which templates the directory must be used.
--separate-git-dir=&lt;git dir&gt;Replaces the .git directory with a text file containing the path to <git dir>. When used on an existing repository, the .git directory is moved to the specified path, and a file containing that path is created in its place.
`--shared[=(falsetrue
--initial-branch=&lt;name&gt;Sets the name for the initial branch in the newly created repository.

Practice

What are the functions and options of the 'git init' command?

Dual-run preview — compare with live Symfony routes.