Git Init
The git init
command creates a new Git repository. The git init
command can be used to convert an existing, unversioned project to a Git repository or initialize a new, empty repository. Most other Git commands are not available outside of an initialized repository, so this is usually the first command you'll run in a new project.
Executing git init
creates a .git
hidden subdirectory in the current working directory. That directory stores all of the objects and refs that Git uses and creates as a part of your project's history. This hidden .git directory is what separates a regular directory from a Git repository.
Git Init Common Usage
git init
: Transform the current directory into a Git repository. For an existing project to become a Git repository, navigate into the targeted root directory. Then, rungit init
. This adds a.git
subdirectory to the current directory and makes it possible to start recording revisions of the project.Example:
git init
Output:
Initialized empty Git repository in /Users/gitopia/hello-world/.git/
git init <directory>
: Create an empty Git repository in the specified directory. Running this command will create a new subdirectory called containing nothing but the.git
subdirectory.Example:
git init hello-world
Output:
Initialized empty Git repository in /Users/gitopia/hello-world/.git/
git init --bare <directory>
: Create a new bare repository. The --bare flag creates a repository that doesn’t have a working directory, making it impossible to edit files and commit changes in that repository.Example:
git init --bare hello-world
Output:
Initialized empty Git repository in /Users/gitopia/hello-world/.git/
You would create a bare repository to git push and git pull from, but never directly commit to it. Central repositories should always be created as bare repositories because pushing branches to a non-bare repository has the potential to overwrite changes.
You can learn more about the
git init
command and its options in git-scm's documentation.