Git Basics | git init

The git init command

git init is the command for creating a new Git repository. Use this command to convert an existing project that is not under version control into a Git repository, or to create and initialize a new empty repository. Because almost every other Git command can only be used inside an initialized repository, this is usually the first command you run when starting a new project.

Running git init creates a .git subdirectory at the project root containing all repository-related metadata. Aside from creating this .git directory, it does not change the existing project. Unlike SVN, Git does not create a .git folder in each subdirectory.

Usage

git init

Converts the current directory into a Git repository. Running this command creates a .git folder in the current directory and lets you start version control for the project.

git init <directory>

Creates an empty Git repository in the specified directory. Running this command creates a new folder named <directory> that includes a .git subdirectory.

git init --bare <directory>

Creates and initializes a Git repository without a working directory. Shared repositories should be created with the --bare flag. See the note below. When the --bare flag is specified, .git is automatically appended to the repository directory name. For example, the bare version of a repository named my-project is stored in a folder named my-project.git.

Notes

Compared with SVN, git init provides a very simple way to create a new version-controlled project. With Git, you do not need to manually create repository folders, import files, or check out a working copy. Running git init in the project folder is enough to create a fully functional Git repository.

However, in most projects, git init is used only once when creating the central repository. Individual developers usually do not use git init when creating a local repository, which is the working copy of the central repository. Developers generally use git clone to create a copy of an existing repository on their local machine.

Bare repository

Specifying the --bare flag creates a repository without a working directory, so files cannot be edited or changes applied in that repository. If you push a branch to a non-bare repository, changes may be recorded incorrectly, so a central repository should always be created as a bare repository. You can think of --bare as a way to mark the repository as storage space rather than a development environment. In practice, central repositories are bare repositories in almost all Git workflows, while developers’ local repositories are not bare.

Git Tutorial: Bare Repositories

Example

Because git clone is more convenient for creating a working copy of a central repository, the usual use case for git init is mainly creating the first central repository:

ssh @
cd path / above / repo
git init --bare my-project.git

First, log in to the server where you want to create the central repository using SSH.
Next, move to the directory where the project will be stored.
Finally, create the central repository with the --bare flag.
Developers can then use the clone my-project.git command to create a working copy on their development machines.