The Git init command creates a new Git repository. The init command sets up all the configuration files you need to work with git in a folder called .git/. You only need to run the git init command once.
When you’re working with git on a new project, you will want to create a new repository on your local machine. That’s where the git init command comes in. The git init command creates and initializes a new Git repository.
This tutorial will explore, with reference to examples, how to use the git init command to create a new local git repository. By the end of this tutorial, you’ll be an expert at using the git init command.
Git Init
In order to work with code using Git, you need to store your code in a Git repository. Repositories, or repos, are storage containers for a project where you can save different versions of your code.
There are two ways to start working with Git. First, you can clone an existing repository using git clone. This will copy all the code and history from an existing project to your local machine. Second, you can create a new repository using git init, which will have its own versioning system and history.
The git init command creates an empty Git repository. init can be used to convert an existing project into a Git repository. The init command can also initialize an empty repository for a new project.
What Happens When You Use Git Init
When you run git init, a folder called .git is created in your current working directory (the folder you are viewing). This folder contains all the files and metadata used by the Git version control system. For instance, in this folder you will see a file called HEAD. The GIt HEAD file points to the Git commit which you are viewing on your local machine.
The git init command does not change the project in the folder in which you run the command. This is because all the main files git needs are stored within the .git directory that the git init command creates.
The git init command is the first command you’ll run if you are starting a new Git project.
How to Use Git Init
The git init command is easy to use. You don’t need to create a repository on a server to start working with a git repository. Instead, you only have to navigate into your project folder and run the git init command.
Here’s the syntax to create a git repo using the git init command:
git init
This command will initialize a new Git repository in the current working directory. So, before you run the command, make sure you are in the directory in which you want to initialize a repository.
Alternatively, you can specify the directory in which the new repository should be initialized. The syntax for doing so is as follows:
git init <folder>
Suppose we wanted to initialize a repository in a folder called demo-project
. We could do so using this code:
git init demo-project
When we run this command, a .git folder is created within our demo-project folder, instead of in our current working directory.
You can run the git init command in a folder which already has an existing git configuration. This is because git init does not override an existing configuration. So, if you accidentally run git init in an existing Git repository, nothing will happen.
Initialize a Bare Repository Using Git Init
Sometimes, when you’re working with Git, you will want to create a repository without a working directory. Repositories without a working directory store files. However, you cannot directly edit files in the repository. You must push commits with your changes.
Using a bare repository is useful if you are using Git as a storage mechanism. For instance, say you’re working on a project that uses a set of resources which do not need to be changed. You could store those resources in a bare GIt repository.
This makes sense because the contents of the repository do not need to be changed. However, developers on the project may need access to the data stored in the repository.
Bare repositories are common for Git mirrors. This is because bare repositories do not need a working tree. They just need the metadata and files associated with a repository. No working tree needs to exist because no people will change on the mirror. Contributors will change files on their local machine and push those changes to the mirror.
In general, only central repositories are bare. Developers will clone the repository if they want a copy of the data stored in the bare repository. You will probably not initialize a bare repository on your own computer.
Bare Repository Example
To create a bare repository using Git, you can use the following command:
git init --bare <folder>
The folder you specify is the directory in which the bare repository is created.
Let’s walk through a more realistic example of initializing a bare repository. Suppose you want to create a remote central repository which stores files a number of team members may need to access. This repository is created on another machine, so you’ll need to use SSH to set up the repo.
You could use the following code to initialize the repository:
ssh user@host cd /path/of/repo git init --bare project-name
First, we use the ssh command to access the server that contains the code for our project. Then we use the cd command to move into the folder in which the bare repository should be created.
We then use the git init –bare command to create a central storage repository. This repository contains the files in the directory to which we navigated. Now that we have created a remote repository, developers can clone the project to their local machines.
"Career Karma entered my life when I needed it most and quickly helped me match with a bootcamp. Two months after graduating, I found my dream job that aligned with my values and goals in life!"
Venus, Software Engineer at Rockbot
Configuration Options
The git init command has a number of configuration options which can be used to customize how your repository is initialized. These are as follows:
- –quiet: Prints only “critical” messages such as errors and warnings during repository initialization.
- –bare: Creates a bare repository (as we discussed earlier).
- –separate-git-dir: Creates a file containing the path to the directory you specify. This file creates a link to the .git directory, and is useful if you want the .git folder to be stored outside of the working directory of your project.
If you’re looking to learn more about these configuration options, check out the official Git documentation on the git init command.
Conclusion
The git init command allows you to initialize an empty Git repository in which the code for your project can be stored. Optionally, the command can be used with the bare flag to create a bare repository to which new code cannot be committed.
This tutorial walked through, with reference to examples, how to use the git init command to initialize an empty git repository. Now you have the knowledge you need to start using the git init command like a professional developer!
About us: Career Karma is a platform designed to help job seekers find, research, and connect with job training programs to advance their careers. Learn about the CK publication.