What is the git init command?

A Git repository is a virtual storage place for your project that allows you to save versions of your code so that you can access them when needed.

The git init command is used to initialize a new git repository or reinitialize an existing one.

The git init command transforms the current directory into a Git repository, i.e., a .git directory with subdirectories for objects, refs/heads, refs/tags, and template files. It will also create a new master branch.

Running git init in an existing repository is safe, meaning that it will not overwrite things that are already there. The primary reason for rerunning git init is to pick up newly added templates (or to move the repository to another place if the --separate-git-dir option is specified).

Example

To create a folder called myproject and transform it into​ a Git repository, we would need to execute the following commands:

mkdir myproject
cd myproject
git init

To create a file, run the cat or touch command, as follows:

touch code.txt

Then, add files to the repository using the git add command, as follows:

git add code.txt
git status

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved