What is a Git Repository?


Introduction

In the field of distributed version control systems (DVCS), Git is the most widely used system for tracking changes over time and collaborating with other developers. Whether you're a team of software developers working on an enterprise project or a single programmer working on your local files, Git is an extremely powerful tool for software development.

Creating a Git repository is the first step after installing Git. The goal of this article is to explain what a Git repository is, how it works, and how to create and configure one.

What is a Git Repository

A Git repository stores the history of changes made to your codebase by a collection of files and folders. As a developer, I have found this to be extremely useful since it allows me to keep a single view of the project codebase, back up a backup copy of the entire project history, easily retrieve older versions of the whole codebase or individual files, debug code, find out who wrote a specific change and a lot more.  This is extremely useful for development teams. 

Using Github an existing repository can be cloned and continued to be developed or a new repository can be created for an existing project that has not been tracked with version control.

Types of Git repositories

There are two types of GIT repositories namely −

  • Bare

  • Non-Bare Repositories

Let us explore them one by one

Non-bare repository

In a non-bare repository, you'll find .git/ along with the working tree, which is a snapshot of the files you can edit directly. Changes are edited and committed here. Git init creates a repository that is not bare, i.e., a normal repository.

Non-bare repositories can be created by using the following command −

$ git init –bare

Bare repository

In a bare repository, only the .git/ part can be edited, no files can be directly edited. A bare repository's name ends in .git by convention to emphasize this. In a bare repository, we never edit anything. A bare repository can be stored on GitHub, GitLab, etc. To store your private repository, you can also create a bare repository on your computer/server.

To create a non-bare repository, we use the following command −

$ git init

Git Repositories: How to get one

Git repositories can be obtained in two ways −

  • Initializing a Git repository from an existing directory.

  • Copying or cloning an existing Git repository

Method 1: Initializing a Repository

Using the Git Bash terminal window, navigate to the directory of your project to create a Git repository −

$ cd [directory path]

Where −

[directory path]: This is your project's directory.

When you arrive at the project directory, create a Git repository using −

$ git init

Once a repository has been created, a subdirectory called .git is created that contains the files that Git needs to track project changes. Once you commit the first change in Git, the repository starts tracking the project.

Method 2: Creating a clone of existing repositories

In order to obtain a local development clone of a project that has already been set up in a central repository, users usually use the clone command. In general, cloning is done once, like git init. A developer manages their local repository once he or she has obtained a working copy.

$ git clone <repo url>

A remote repository can be cloned or copied with git clone. A repository URL is passed to git clone. Several different network protocols and URL formats are supported by Git.

How to work with repositories

By using the git add and git commit commands, you will be able to save your changes to your repository. By using the git add command, changes to files in the working directory are added to the staging area of Git. The staging area (also referred to as staging index, as that's what it's called in the documentation) can be viewed as a temporary area where your changes are held before they are committed to the repository.

Your local Git repository is updated when the git commit command is executed. It's like saving a new revision or changeset. As shown below, you must use git add and git commit sequentially −

$ touch index.html
$ git add index.html
$ git commit -m "Have added the index.html file"

Our example shows us how to create a file called index.html, add it to our staging area, and then import it using the syntax of git add <filename>. In order to commit a change, we use the -m flag along with a descriptive commit message that states "Initial Commit". It is possible to view the changes that have been recorded in Git's log by using the command git log once they have been committed.

Conclusion

In this article, we learned what is a Git repository and how to initialize a Git repository using git init or copy a remote repository to your local machine by using git clone.

Updated on: 14-Dec-2022

834 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements