Last updated: Apr 5, 2024
Reading time·3 min

When the git init command is run in a directory that already contains a git
repository, the message "Reinitialized existing Git repository in
/path/to/.git/" is printed.
Running the git init command in a folder that already contains a git
repository is safe and doesn't overwrite existing files.

The git init command creates an empty Git repository or reinitializes an existing one.
# Reinitialized existing Git repository in /home/borislav/Desktop/bobbyhadz_python/.git/ git init
If you don't already have a repository in the current folder, a .git directory
gets created.
The .git directory is used to store subdirectories for refs/tags,
refs/heads and template files.

If you already have a .git folder and rerun the git init command, none of
the existing files get overwritten.
git init command in an existing repository is to fetch updated template files.Template files are the starting files that are used for all Git projects.
For example, you could use a template file to set your default branch to main
instead of master.
Template files are global and are used as a source for all newly created git
repositories.
Running the git init command in an existing repository is also used to move
the repository to another location if the --separate-git-dir flag is
specified.
# ⛔️ NOTE: This command moves the repository to the given path git init --separate-git-dir /path/to/repository.git
When the
--separate-git-dir
flag is set, the repository is not initialized under $GIT_DIR or ./.git,
instead, a text file that contains the path to the repository is created.
If you run the git init command with the --separate-git-dir flag, the
repository gets moved to the specified path.
The text file serves as a filesystem-agnostic Git symbolic link to the repository.
You can run the git init command in quiet mode if you don't want to print the
output of the command.
git init --quiet

There is also a shorthand -q flag that does the same.
git init -q
When the -q flag is set, only error and warning messages are printed.
As previously noted, if you run the git init command in a folder that already
contains a git repository, none of the files in the .git directory are
overwritten.
This means that if you need to create a branch new Git repository, you have to
delete your .git folder and rerun the git init command.
.git folder and the associated Git repository. Only run this if you want to create a branch new repository and discard the old one.# 🚨🚨🚨 Only run this if you want to delete your Repository # ----------------------------------- # For macOS or Linux rm -rf .git # For Windows rd /s /q ".git"
After you delete the repository, you can rerun the git init command to create
a brand new .git directory.
git init
You can also specify the initial branch name when issuing the git init
command.
git init -b <your-branch-name>
The
-b
or --initial-branch flag is used to specify the name of the initial branch in
the newly created Git repository.
If you don't set the flag, the initial branch name will most likely default to
master or main.
You can learn more about the related topics by checking out the following tutorials: