Git Init: A Guide for Beginners
Often, when you’re working with git, you will want to create a new repository on your local machine. This new repository will be configured with all the files you need to work with Git, and will be independent from any other repositories on your computer.
That’s where the git init command comes in. The git init command is used to create and initialize a new Git repository.
This tutorial will explore, with reference to examples, how to use the git init command to create a new git repository. By the end of this tutorial, you’ll be an expert at using the git init command.
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. The command can either be used to convert an existing project into a Git repository or to initialize an empty repository for a project that has not yet been created. The git init command is usually the first command you’ll run if you are starting a new project and are using Git for version control.
What Happens When You Use Git Init
When you run git init, a folder called .git will be created in the current working project directory. 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, which 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.
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 the folder in which your project is located and run the git init command.
Here’s the syntax to create a git repo using the git init command:
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, the .git folder will be created within our
demo-project folder, instead of in our current working directory.
The git init command can be run 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
Sometimes, when you’re working with Git, you will want to create a repository without a working directory. This means that the repository you create can be accessed and can store files, but files cannot be edited and commits cannot be made.
Using a bare repository is useful if you are using Git as a storage mechanism. For instance, if you’re working on a project that uses a set of resources which do not need to be changed, those resources could be stored in a bare repository.
This makes sense because the contents of the repository do not need to be changed, but developers on the project may need access to the data stored in the repository.
In general, only central repositories are bare. Then, that repository will be cloned by local machines who want a copy of the data stored in the bare repository.
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 will be 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 will be 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
Let’s break down this example. 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 which 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.
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 when the repository is being initialized.
- –bare: Creates a bare repository (as we discussed earlier).
- –template: Specifies the directory in which the .git template is used (this is an advanced use case).
- –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.
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!