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?
- What is Git Repository?
- How to Use the “git init” Command For Initializing an Empty Repository in Git?
- How to Use the “git init” Command For Reinitializing Repository in Git?
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:
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:
Step 2: Create New Repository
Next, create a new Git directory by running the “mkdir” command:
Then, use the provided command to change the directory from root to the newly created local repository:
Step 3: Initialize Git Repository
Lastly, execute the “git init” command along with the repository name and initialize it:
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:
From the given output, we have selected the “test1” repository:
Then, move to the selected Git repository by using the “cd” command:
Finally, execute the “git init” command to reinitialize the repository:
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.