Git Init



The git init command is used to create a new Git repository. It can also be used to convert an existing project to a Git repository.

Git init command creates a .git subfolder inside the current working directory.

The .git subfolder contains a collection of files, a history of changes, and any special configuration.

Git init command is normally the first command you will run in a new project.


How to use git init

It's easy to create a new version-controlled project using the git init command. All you have to do is to cd into your project directory and run git init, and you will have a fully working Git repository.

Here some common usage of Git Init :

  • Transforming the current directory into a Git repository by specifying no argument to git init. When you run the git init command, it will create a subfolder in the current directory and start tracking your project files.
    $ git init
    
  • Creating a new repository Git bypassing the name of the folder you want to create to the git init command. It will create a new subdirectory with the same name giving as an argument, and it will contain just .git subdirectory.
    $ git init name_of_your_project
    

If you run git init on a project directory that already contains a .git subdirectory, it will not override the existing .git folder and its configuration.


Git init vs. Git clone

You can easily be confused between git init and git clone. They can both be used to create a new git repository. However, the git init command is used when you want to start a new repository locally. The git clone is used when you want to clone an already existing remote repository.


Bare repository: git init --bare

In some situations, when you're working with Git, you need to create a repository without a working directory.

Repositories without a working directory store files, but you cannot directly edit files and commit changes in the repository.

You would create a bare repository to git push and git pull from, but you cannot directly commit to it.

Using a bare repository is useful if you want to share a repository. Central repositories should always be created as bare repositories.

Conventionally, repositories initialized with the --bar flag end in .git.

You can use the following commands to initialize a remote repository :

$ ssh user@host
$ cd /path/of/your-repo
$ git init --bare your-project-name.git


ExpectoCode is optimized for learning. Tutorials and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using this site, you agree to have read and accepted our terms of use, cookie and privacy policy.
Copyright 2020-2021 by ExpectoCode. All Rights Reserved.