Git

Git-init Command in Git | Explained

The Git repository is a project that has multiple files and directories. When users work on Git, they perform different tasks inside the repositories. To do so, users are required to initialize the Git repository first. The “git init” command can be used for initializing the newly created repository and converting the already existing project to a Git repository. Additionally, multiple Git commands are not available in the uninitialized repository.

This blog will discuss:

What is the “git init” Command?

To initialize a newly created or reinitialize an existing Git repository, the “git init” command can be used. It transforms the current working directory into a Git repository. When the Git directory is initialized, the “.git” folder is created inside the directory with subdirectories, such as “objects”, “template”, “refs/tags”, and “ref/heads” files.

Syntax

The general syntax of the “git init” command is stated below:

git init

What is Git Repository?

The “Git repository” is a virtual storage place for users’ projects that enables them to store project source code versions that can be accessed whenever they need.

How to Use the “git init” Command For Initializing an Empty Repository in Git?

To initialize an empty repository in Git by utilizing the “git init” command, check out the provided steps:

  • Navigate to the Git root directory.
  • Create a new Git local repository.
  • Go to the newly created local repository.
  • Run the “git init <repo-name>” command.

Step 1: Move to Git Root Directory

Initially, execute the “cd” command along with the path of the Git root directory and move to it:

cd "C:\Users\nazma\Git"

Step 2: Create New Repository

Next, create a new Git directory by running the “mkdir” command:

mkdir perk

Then, use the provided command to change the directory from root to the newly created local repository:

cd perk

Step 3: Initialize Git Repository

Lastly, execute the “git init” command along with the repository name and initialize it:

git init perk

As you can see, the newly created empty repository has been initialized successfully:

How to Use the “git init” Command For Reinitializing Repository in Git?

We can use the “git init“ command for reinitializing the existing repository. To do so, first list all existing Git repositories by running the “ls” command:

ls

From the given output, we have selected the “test1” repository:

Then, move to the selected Git repository by using the “cd” command:

cd test1

Finally, execute the “git init” command to reinitialize the repository:

git init

According to the below-given output, the existing repository has been initialized successfully:

That’s all! We have briefly discussed the “git init” command in Git.

Conclusion

The “git init” command is used for initializing a newly created or reinitializing an already existing Git repository. Users can initialize the empty as well as reinitialize the existing repositories in Git. To do so, go to the Git root directory and generate a new Git local repository. Then, go to the newly created repository and execute the “git init <repo-name>” command. This blog described the usage of the “git init” command in Git.

About the author

Maria Naz

I hold a master's degree in computer science. I am passionate about my work, exploring new technologies, learning programming languages, and I love to share my knowledge with the world.