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
cdinto your project directory and run thegit initcommand in your terminal.
git init
git init- Converts the current directory into a Git repository to track project changes. Alternatively, specify a path to create a new
.gitsubdirectory in that location.
git init <directory>
git init <directory>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.

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
--templateoption, - the contents of the
$GIT_TEMPLATE_DIRenvironment variable, - the
init.templateDirconfiguration variable, - the default template, which usually resides in a
/usr/share/git-core/templatesdirectory.
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 <directory> command accepts an optional <directory> 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:
| Flag | Description |
|---|---|
-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> | 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[=(false | true |
--initial-branch=<name> | Sets the name for the initial branch in the newly created repository. |
Practice
What are the functions and options of the 'git init' command?