Git Tutorial 4 – Create a new Git repository – git init Command

Introduction

As a part of GIT Tutorials End To End, we will learn to create or initialize a GIT repo using a very first command called “git init“.

Before you proceed please make sure that GIT is properly installed and configured. If git is not installed or you don’t know how to check then I recommend going through Install GIT On Windows – Step By Step.

Did you know that I have started a YouTube channel as well and I need your support to make it successful. Please do watch content then comment, like, share, and obviously subscribe.

Why git init?

Before learning about the command “git init” let’s understand why do we need this command. As of now with limited knowledge of git, we know that it is a version control system and we can keep a track of changes to a file or set of files. So git automatically will not start tracking changes to any file. We need to let git know where to track and what to track. Here git init comes into the picture.

If you create a new directory or existing directory and try to run a git command “git status” (Note – Just use this command as of now) then you will see the below error.

Follow steps as below:-

  1. Create an empty directory.
  2. Right-click and click on Git Bash Here.
  3. Type a git command as “git status”.

As the new directory is not a git directory it showed a fatal message. The same error message you will get if there is an existing directory with files in it.

What is git init?

The “git init” command creates a new empty git repository or initializes an existing directory as a git directory. Once the directory is a git directory you can run any git commands.

Let’s run “git init” command in Git bash.

You need to notice two points in the above result of the command:-

  1. Initialized empty Git repository in C:/Users/amomahaj/Desktop/NewFolderGitDemo/.git/
  2. (master)

The “git init” command creates a new hidden folder named “.git” in the same directory where it was run. The “.git” folder will save metadata of the new git repository. Once it becomes a git directory the default branch is master. Remember during the installation of git we had an option to change this default branch name.

Suppose you start a new business and open an office. After sometimes when your business grows you open more branches of yours business. The same logic is with git branches. Master is the default branch of git. We will learn more about branches later.

We will not see the content of the “.git” subdirectory as of now.

Interview Question

Can we rerun the “Git init” command again on a git initialized repo?

Answer – Yes we can and It will not override an existing “.git” configuration.

Please subscribe to my YouTube channel Retarget Common to learn from my video tutorials.

Below are important end to end tutorials for Testers:-

Selenium Tutorials

Rest Assured Tutorials

Postman Tutorials

TestNG Tutorials

If you have any doubt, feel free to comment below.
If you like my posts, please like, comment, share and subscribe.
#ThanksForReading
#HappyLearning

1 thought on “Git Tutorial 4 – Create a new Git repository – git init Command”

  1. About the Interview question u mentioned, Ur ans says “Yes we can and It will not override an existing “.git” configuration.”

    Then what does it do if we re-run the git init command ???

    I got Message “Reinitialized existing Git repository in “

Leave a Reply

Your email address will not be published. Required fields are marked *