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.
Options courantes
| Option | Description |
|---|---|
--bare | Créer un dépôt nu sans arborescence de travail, adapté à l'hébergement sur un serveur. |
--initial-branch=<name> / -b | Définir le nom de la branche initiale (défaut main). |
--template=<dir> | Utiliser un répertoire de modèles personnalisé pour les hooks et la config. |
--shared[=<perms>] | Permettre au dépôt d'être partagé par un groupe Unix. |
--separate-git-dir=<dir> | Stocker les données Git réelles dans un répertoire séparé, en laissant un .git fichier qui pointe vers lui. |
-q, --quiet | Supprimer la sortie informationnelle. |
Exemples
git init
# Initialiser un dépôt dans le répertoire courant
git init my-project
# Créer un nouveau répertoire et initialiser un dépôt à l'intérieur
git init --bare /srv/git/myproject.git
# Créer un dépôt nu à héberger sur un serveur
git init -b trunk
# Initialiser avec la branche initiale nommée "trunk" au lieu de « main »
Erreurs fréquentes
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.
Commandes liées
git clone, git config, git remote