In this tutorial, nosotros will learn How to Create Git Repository . Nonetheless, before you proceed with this tutorial it is very important that you lot have gone through the previous set up of tutorials on GIT here:

What is a Git Repository?

The purpose of Git  is to manage a project, or a set of files, equally they change over fourth dimension.Git stores this information in a data structure called a repository . In short Git Repository is a collection of all project files along with their history . Information technology is a virtual storage of your project where you go on all the resources/files of the project along with a special folder called .git. The .git folder in a git repository is used by GIT  programs to store information near the repository like Logs , Position of Head , and more. It allows you lot to save versions of your code, which can exist accessed, tracked, and managed.

At this commodity, we don't know much about these terms and hence we will keep our discussion to a minimum about the .git folder. For the time being, just remember that every GIT  repository will have a hidden .git binder to enable GIT programs to track changes in the repository.

Different ways to Create Git Repository

Nosotros can Create Git Repository using i of the three approaches enlisted below:

  1. Create a bare repository
  2. Initialize repository in an existing project directory
  3. Clone a remote repository from Github

We'll be using Git command line tool to create repositories and also for learning all the Git operations in this serial of tutorials. Use of Git command line tool volition help you get familiar with the GIT Commands . Using Git command line tool gives you more flexibility than Git Clients or Git GUIs considering they provide only a subset of GIT Features . So, to harness the consummate ability of Git similar a pro, Git command line tools are the recommended way to utilize GIT.

So, let'due south get started and see how we can create a Git repository in the three ways mentioned in a higher place. The very first thing is to find the Git command line tool on the system. I promise y'all have already gone through the Git installation process described in the tutorial here:

Create a Blank Git Repository

Creating a Bare Git Repository for a new project is three-footstep process:

  1. Create a New Project/Folder
  2. Scan to New Project
  3. Initialize Git Repository for the Project

Once y'all have Git installed, just search of git cmd   in your system search bar. You will go the Command line tool listed every bit shown in the below image.

Search for Git CMD

Note : that this is Windows 8 System search bar, you will have a unlike ane based on the Bone yous have.

Open up the Git command line tool, y'all will run across a command line window open up as shown in the image below.

Navigate to folder for storing all local git repositories

Pace one: Create a New Project/Folder - Now that we have our command line tool open, let us create a project folder . Creating a project folder with a adept proper noun is very important. If yous have a Good and Relevant name of your project information technology will become easier for you to identify and relate to the project when yous come back to it in the future.

Note : Usually people create a Root Project folder with the visitor name similar toolsqa, amazon, flipkart, etc, and within this root folder they go on all the different projects of the same company.

Command to create a folder on a Windows and Mac system is mkdir <folderName>. Where folder name is the projection name. Let us proper noun our first project LocalGit . With this proper name, the control becomes: mkdir LocalGit

Create a directory to store all local git repositories

Navigate to this folder past using the command cd LocalGit, both on Windows and Mac systems.

Navigating to directory created for storing git repositories

At present yous are inside the repository folder where we'll create our Git repositories using the iii approaches mentioned higher up.

Bare Git repository means an empty directory with simply a hidden .git folder. Let united states of america name the project as BareGitRepo . Ensure that you're in the LocalGit folder before proceeding with following steps. Enter command mkdir BareGitRepo.

Create a directory to store bare repository

Annotation : So now yous accept your Root Folder (LocalGit) ready and within that the Project (BareGitRepository)

Step 2: Browse to New Project - Navigate to the project created in the previous step using the command cd BareGitRepo.

Navigate to directory created to initialize bare repo

Step 3: Initialize Blank Git Repository for the Project - Enter the command git init this command is used to Create Git Repository. Consequently, the execution of this control creates a hidden .git folder therein. Or in other words, an empty Git Repository is initialized . You'll find a bulletin stating that an empty Git repository is created.

Create Git Repository

Note : git init is a standard GIT command and it initializes the directory with a .git folder used for tracking versions of project artifacts.

Let united states of america view the contents of the newly initialized Git repository by executing the command dir /ah.

View content of bare repository

After the execution of dir , you volition come across empty projection folder equally expected from a Bare Git repository . You lot can now add projection files to this repository and they'll be tracked by GIT.

However, if you want to see the .git folder being reported in command line use the control dir /a:hd. This will bear witness hidden folders in the directory and you will notice .git folder at that place.

Create Git Repository for an Existing Project

Nosotros'd also like to track an existing project by using Git. In this example, we'll initialize a Git repository in an existing project directory. There is no rocket science in creating git Repository for an existing project, it is as same as creating a git repository for a new project with the merely difference of stride i is non required:

  1. Create a New Project/Folder
  2. Browse to Existing Project
  3. Initialize Git Repository for the Project

Step two: Browse to Existing Project - Navigate to the directory containing your projection artifacts. In this case, let us consider that the proper name of the project is SampleProject at the location C:\Users\admin1\LocalGit\SampleProject , move to a similar location on your system. One tin view the content of the directory by using the command dir.

Content of existing project

Annotation : I already accept few files in the SampleProject for a demo purpose, so it is not an empty binder.

Step three: Initialize Git Repository for the Projection - Initialize the git repository in this project by using the same control used while creating a blank repository i.e. git init.

Git init in existing project directory

View the content of the directory and note that a .git binder has been created therein.

View Project Directory after Git Init command

Afterwards Git initializing, the project is tracked past GIT .

Cloning a Remote Repository from GitHub

When you lot desire to contribute to an existing projection hosted on GitHub or a similar online Git service provider, you lot've to Clone their repository from the remote server like Github, GitLab, etc. For case, the remote repository of Apache POI (Java library to read from and write to Excel files) is hosted at https://github.com/apache/poi

Definition of word Clone in the context of Git is to create a local copy of the Remote repository. In this case, remote Repository is hosted at https://github.com/apache/poi and nosotros will Clone it on our local system.

Here are the steps to clone (download and track the changes) this repository.

Step ane: Fetch and copy the URL, every bit mentioned in the image below, of the Apache POI repository at GitHub . That is, https://github.com/apache/poi.git

Remote Apache POI Repository

Stride ii : In Git CMD, navigate to the folder for storing all the Git repositories locally. That is C:\Users\admin1\LocalGit in this case.

Pace 3: Create a directory called RemoteCloneRepo to store the source code of Apache POI repository locally by using the command mkdir RemoteCloneRepo Navigate inside this newly created directory past using the control cd RemoteCloneRepo

Create a directory to clone remote repo

Step 4 : To clone the repository, enter the command git clone https://github.com/apache/poi.git

Execute git clone command

Note : git clone <repoURL>  is a standard GIT command to clone an existing remote repository.

Step v: Cloning a repository depends on the size of the repository. Usually, information technology takes a while for a large repository. Yous'll take to wait until all the files are checked out.

Now you can brand changes to the repository. Git will runway all the changes.

Execute git clone command

In this tutorial, we looked at the three means to create a Git repository. In the adjacent tutorial, we'll acquire about the typical Life-Bicycle of files in a Git repository. We will also learn almost different Git commands that will aid united states movement our Git repositories into different stages of a typical Git life-wheel.