What is a Git Repository?

July 15, 2021

Introduction

Git is an open-source software used for tracking project changes and revisions across different teams. Git saves different versions of projects in a folder known as a Git repository.

In this tutorial, we will go over what a Git repository is, how to create one, and how to work with repositories using Git commands.

What is a Git repository?

Prerequisites

What is a Git Repository?

A Git repository tracks and saves the history of all changes made to the files in a Git project. It saves this data in a directory called .git, also known as the repository folder.

Git uses a version control system to track all changes made to the project and save them in the repository. Users can then delete or copy existing repositories or create new ones for ongoing projects.

Types of Git Repository

There are two types of Git repositories, based on user permissions:

Bare Repositories

Software development teams use bare repositories to share changes made by team members. Individual users aren't allowed to modify or create new versions of the repository.

Non-Bare Repositories

With non-bare repositories, users can modify the existing repository and create new versions. By default, the cloning process creates a non-bare repository.

How to Get a Git Repository

There are two ways of obtaining a Git repository:

  1. Turning an existing directory into a Git repository (initializing).
  2. Cloning a Git repository from an existing project.

Initialize a Repository

To initialize a Git repository in an existing directory, start by using the Git Bash terminal window to go to your project's directory:

cd [directory path]

Where:

  • [directory path]: The path to your project directory.

If you are using Windows 10, the directory path might look like:

cd C:/Users/aleksandarko/git_example
Use Git Bash to go to your project directory

Once you navigate to the project directory, initialize a Git repository by using:

git init
Initializing a Git repository with git init

Initializing a repository creates a subdirectory called .git that contains the files Git needs to start tracking the changes made to the project files. The repository only starts tracking project versions once you commit changes in Git the first time.

Clone a Repository

Use the git clone command to clone an existing repository and copy it to your system:

git clone [url] [directory]

Where:

  • [url]: The URL of the Git repository you want to clone.
  • [directory]: The name of the directory you want to clone the repository into.
Cloning a git repository from a URL

Note: If you don't provide the directory name, Git will copy the name of the original repository folder. You can also clone tags to avoid copying the entire repository.

How to Work with a Repository

Git provides various commands to create different versions of a project:

Configuring Repositories

To add an author name to all the commits in the current repository:

git config --global user.name "[your_name]"

To add an email address to all the commits by the current user:

git config --global user.email "[email_address]"

To create a shortcut (alias) for commonly used Git commands:

git config --global alias.[alias_name] [git_command]

To make it easier to track repository changes by enabling Git's automatic command line coloring:

git config --global color.ui auto

Note: If you omit the --global option, the commands above will only apply to the current local repository. You can achieve the same effect by replacing the --global option with --local.

To set a default text editor for Git:

git config --system core.editor [text_editor]

To open the global Git configuration file:

git config --global --edit

Saving Changes

Making changes to the Git repository is done by using git add and git commit commands. The git add command adds files to the staging area, while the git commit command applies the staged changes to the repository.

The git add command uses the following syntax:

git add [file/directory name]

Note: Add multiple files or directories to the staging area by listing their names after the git add command.

Add files to the staging area using the git add command

To add all the files in the current directory to the staging area:

git add --all

Git also lets you add all files in the current directory ending with the same extension. For instance, add all .txt files to the staging area with:

git add *.txt

Note: You can also unstage files in Git before committing them to the project.

Once you've added all the necessary files to the staging area, commit the changes to the repository by using:

git commit -m "Committing files to the repository"

Where:

  • -m: Allows you to attach a message to the command, in this case "Committing files to the repository." Use messages to describe the changes being committed.
Commit changes to the Git repository

Enabling Collaboration

Git allows you to copy and share repositories with other developers by using the git push and git pull commands.

The git push command lets you share all the commits and files in the current repository with one or more remote repositories. The git push command uses the following syntax:

git push [remote repository] [branch name]

Where:

  • [remote repository]: The name of the remote repository you want to share your commits and files with.
  • [branch name]: The name of the branch of your local repository you want to share.

Use the git pull command to copy the content of a remote repository and merge it with your local copy:

git pull [remote repository]

Note: Learn how to remove a Git remote from a repository once you no longer need it.

Conclusion

After reading this tutorial, you should have a Git repository of your project set up and ready to use. You should also have a basic understanding of how Git repositories work.

For more information on Git commands, download phoenixNAP's Git command cheat sheet.

Was this article helpful?
YesNo
Aleksandar Kovačević
With a background in both design and writing, Aleksandar Kovacevic aims to bring a fresh perspective to writing for IT, making complicated concepts easy to understand and approach.
Next you should read
How to Update Git
May 8, 2024

This tutorial provides an overview of different ways you can update Git on Linux, Windows, and MacOS systems.
Read more
How To Resolve Merge Conflicts in Git
June 16, 2021

When working with multiple people or on a big project, version control is a must-have. Joining information from multiple sources sometimes yields merge conflicts. Learn about how they happen and how to resolve merge conflicts.
Read more
How to Delete a Git Branch Remotely and Locally
October 13, 2020

This article provides a quick overview of basic commands and processes necessary for deleting both local and remote Git branches.
Read more
Git Revert: How to Undo Last Commit
March 3, 2022

Git has a lot of features that help you manage your project and easily access previous commits. This article uses simple commands to show you how to revert to an earlier commit quickly.
Read more