Synopsis
git init [--bare] [--initial-branch=<name>] [--template=<dir>] [directory]
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 you want to start tracking a project with Git. The command creates a hidden .git directory containing all the metadata Git needs: the object database, references, configuration, hooks, and the index. Without this directory, Git has no place to store history.
Running git init in an existing repository is safe — it will not overwrite existing objects or refs. Instead, it will reinitialize templates and ensure the directory structure is intact. By default, modern Git creates a repository with the branch name configured by init.defaultBranch, which is usually main on new installations.
Beyond the default behavior, git init respects the core.hooksPath setting if you keep a centralized hooks directory, and the init.templateDir config to copy custom hooks and exclude files into every new repository. On filesystems where case sensitivity matters, core.ignoreCase is set automatically based on detection at init time. Teams that frequently bootstrap new services often script the post-init steps: setting the default branch, adding a license file, configuring the gitignore template, and setting up a remote — all immediately after running git init. Understanding what files .git/ contains (HEAD, config, objects/, refs/, hooks/) demystifies later operations and makes recovery from accidents much easier.
Common Options
| Option | Description |
|---|---|
--bare | Create a bare repository with no working tree, suitable for hosting on a server. |
--initial-branch=<name> / -b | Set the name of the initial branch (default main). |
--template=<dir> | Use a custom template directory for hooks and config. |
--shared[=<perms>] | Allow the repository to be shared by a Unix group. |
--separate-git-dir=<dir> | Store the actual Git data in a separate directory, leaving a .git file pointing to it. |
-q, --quiet | Suppress informational output. |
Examples
git init
# Initialize a repository in the current directory
git init my-project
# Create a new directory and initialize a repo inside it
git init --bare /srv/git/myproject.git
# Create a bare repository to host on a server
git init -b trunk
# Initialize with the initial branch named "trunk" instead of "main"
Common Mistakes
A frequent mistake is running git init in your home directory by accident, which turns your entire home folder into a repository. Always check your current directory before running it. Another pitfall is initializing a repo inside an existing repo's working tree — this creates nested repos that can confuse tools and humans alike. If you accidentally created an unwanted repo, simply remove the .git directory to undo it.
Related Commands
git clone, git config, git remote