Git

What is the Difference Between “git init” and “git init –bare”

In Git, developers create and work on multiple repositories. They initialize repositories to track changes to a new codebase or convert the existing codebase to a Git repository. Moreover, initializing a repository also provides a backup of the code. To initialize the Git repository, the “git init” and “git init –bare” commands can be used, however, both have different purposes.

This write-up will explain:

Primary Difference Between “git init” and “git init –bare”?

The “git init” is a command in Git utilized to initialize a new Git repository in a current working directory. It creates a “.git” folder that contains Git’s stored data. On the other hand, the “git init –bare” command initializes the bare repository in the directory. It does not have a working tree. It is usually used to create a central repository where multiple developers push their code and pull changes from it.

How to Utilize the “git init” Command?

To view the usage of the “git init” command, follow the below-provided steps.

Step 1: Switch to the Local Directory

First, redirect to the specific local directory:

cd "C:\Git\Repo"

Step 2: Initialize Git Repository

To create or make a new empty Git repository in the current directory, write out the below-stated command:

git init

It can be observed that the empty Git repository has been initialized:

Step 3: View Repository Content

Now, list the current repository’s content including hidden files:

ls -a

In the below image, the “.git” folder can be seen in the current directory:

How to Utilize the “git init –bare” Command?

Check out the following steps to see the usage of the “git init –bare” command:

Step 1: Redirect to Desired Directory

First, switch to the particular directory:

cd "C:\Git\bare1"

Step 2: Initialize Bare Repository

To create or make a new bare repository in the current directory, run the provided command:

git init --bare

Here, the “–bare” option is used to make a repository without working directory.

It can be seen that the bare repository has been initialized:

Step 3: View Bare Repository Content

Display the content of the bare repository including the hidden files:

ls -a

In the below output, no “.git” folder can be seen in the bare repository:

We have efficiently explained the comparison between “git init” and “git init –bare” commands.

Conclusion

The “git init” and “git init –bare” are commands in Git used to create/make a new Git repository, but they have different purposes. The “git init” is used to initialize the new Git repository in the directory while creating a “.git” folder in it. In contrast, the “git init –bare” initializes a bare Git repository without creating a working directory. This write-up compared the “git init” and “git init –bare” commands in Git.

About the author

Laiba Younas

I have done bachelors in Computer Science. Being passionate about learning new technologies, I am interested in exploring different programming languages and sharing my experience with the world.