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 generating a new Git repository.

Watch a course Git & GitHub - The Practical Guide

The git init usage

Using git init is the simplest way of setting up version-controlled system projects, as there is no need to generate a repository, input files etc.

  • In order to get a working Git repository, you only need to cd into your project subdirectory and run git init command into your terminal.
    git init
  • Transform the directory into your Git repository, to record the project changes.

    Create a new Git repository in a particular directory to generate a new .git subdirectory.

    git init <directory>

After running gin init on your project directory, which contains a .git subdirectory, you can run it on the same project directory once again, and it will not override the existing .git configuration.

The git init bare repositories

The shared repositories must be created with the --bare flag. The --bare flag is used to create a repository that has not working directory and does not allows editting files and commit changes in the directory. --bare is a way to mark a repository as a storage facility. Bare repository must be initialized to git push and git pull from, but never directly commit to it. Central repositories must be created as bare repositories because pushing branches to a non-bare repository can overwrite changes. The central repository is always bare, while the local repositories are non-bare. The most common use for git init --bare is creating a remote central repository.


Template Directory

Templates initialize a new repository with .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,
  • theinit.templateDir configuration variable,
  • the default template, which usually resides in a /usr/share/git-core/templates directory.

The default templates are an example of utilizing template features. Templates can be created for default directories and files that will be copied to the already defined.git subdirectory.

Templates initialize a new repository with .git subdirectory. Templates can be created for default directories and files that will be copied to the already defined .git subdirectory. The default templates are an example of utilizing template features.

Configuration of git init

The git init < directory> configurations have a <directory> argument. The command is run inside the provided <directory>. In case of the absence of this directory, a new one will be created. Here is the full list of options:

-Q or --QUIET Prints only errors, and warnings . All other output is concealed.
--BARE Creates a bare repository.
--TEMPLATE=<TEMPLATEDIRECTORY> Specifies from which templates the directory must be used.
-SEPARATE-GIT-DIR=<GIT DIR> Creates a text file which contains the path to <git dir>. The file acts as a link to the.git directory. When calling git init --separate-git-dir on an existing repository, the .git dir will be moved to the specified <git dir> path.
--SHARED[=(FALSE|TRUE|UMASK|GROUP|ALL|WORLD|EVERYBODY|0XXX)] Specifies that Git repository is to be shared among several users allowing them to push/pull to that repository.

Practice Your Knowledge

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

Quiz Time: Test Your Skills!

Ready to challenge what you've learned? Dive into our interactive quizzes for a deeper understanding and a fun way to reinforce your knowledge.

Do you find this helpful?