Last modified: July 10, 2024

This article is written in: 🇺🇸

What is Git?

Git is a robust and feature-rich version control system (VCS) created by Linus Torvalds, the creator of Linux. A VCS is a software tool that helps software developers manage changes to source code over time. It assists developers in tracking different versions of their code, enabling them to revert to a previous state if required, and efficiently collaborate with others on the same project. Key uses of Git include:

What is a repository?

A Git repository, often shortened to repo, is a special directory or a storage space where your project resides. It not only contains the project's files but also the metadata for the files like revisions and stage. All these information is stored in a subdirectory named .git.

A repository can be created, cloned (copied), or deleted as per project requirements. In terms of location, repositories can be local (on your computer) or remote (on a server or hosted service like GitHub). While a project typically has at least one local repository, it may have multiple remote repositories for backup, collaboration, or other purposes.

+-----------------+   +-----------------+   +-----------------+
|   Alice's Repo  |   |  Central Repo   |   |   Bob's Repo    |
|                 |   |  (e.g., GitHub) |   |                 |
|  o---o---o---o  |   |  o---o---o---o  |   |  o---o---o---o  |
+--------|--------+   +--------|--------+   +--------|--------+
         |                     |                     |
   Push/Pull           Push/Pull             Push/Pull
         |                     |                     |
+--------v--------+   +--------v--------+   +--------v--------+
| Local Workspace |   | Server Storage  |   | Local Workspace |
|                 |   |                 |   |                 |
|   o---o---o---o |   |   o---o---o---o |   |   o---o---o---o |
+-----------------+   +-----------------+   +-----------------+

One of Git's unique features is that it supports a distributed approach. Unlike centralized version control systems, Git does not necessitate a central repository. Each copy of the repository is a full-fledged version with complete history and version-tracking abilities. Furthermore, Git is designed to allow the "client" and "server" to be offline at different times, thereby enabling code sharing through varied means such as email or even physical media like CDs, a particularly useful feature in areas with unstable internet connections.

Initializing a New Repository

Creating a new Git repository is a straightforward process. Suppose you wish to create a new local repository named project_name in your current directory. You would use the following command in your terminal:

git init project_name

This command initiates a new Git repository and creates an empty project with the name project_name.

What happens behind the scenes?

While superficially a Git repository might seem like merely a directory housing your source code, it is in fact far more complex. When you initialize a repository, Git generates a unique .git subdirectory inside it.

+-------------------------+
|         Git Repo        |
|                         |
|  +----------+           |
|  | .git dir |           |
|  +----------+           |
|                         |
|  +----------+  +------+ |
|  | Code     |  | Docs | |
|  +----------+  +------+ |
|                         |
|  +----------+           |
|  | Config   |           |
|  +----------+           |
|                         |
|  +----------+           |
|  | Hooks    |           |
|  +----------+           |
|                         |
+-------------------------+

The .git directory transforms a simple directory into a fully functional Git repository. It holds all the vital Git objects and metadata that allow Git to proficiently track modifications and preserve the history of your project. If this .git directory is removed, your project directory will revert back to a regular directory, devoid of any Git tracking abilities.

The .git directory contains the following key elements:

Together, all these things make a kind of map that we can follow to see how a project has changed over time, using Git's powerful tools for keeping track of different versions.

Cloning an Existing Repository

To access an existing repository, you can download (or "clone") it using the git clone command followed by the URL of the repository. Cloning a repository creates a local copy of that repository on your machine, enabling you to work on the project locally.

For example, to clone a repository from GitHub, use the following command:

git clone https://github.com/djeada/git.git

By default, the repository is downloaded into a new directory in your current working directory. The new directory will have the same name as the repository. However, if you wish to clone the repository to a different location, you can specify a destination path after the URL:

git clone https://github.com/djeada/git.git /opt/projects

In the command above, the repository will be cloned into the /opt/projects directory.

Verifying a Git Repository

To check whether a directory is a Git repository, you can use the git status command. If the directory is a Git repository, you'll see information about the current status of the repository. However, if the directory is not a Git repository, Git will display an error message stating "fatal: Not a git repository."

Alternatively, you can check for the presence of the .git subdirectory which is characteristic of a Git repository. Use the ls command to list the files and directories in the current directory:

ls .git

If the .git directory is listed in the output, this indicates that your current directory is indeed a Git repository. If not, it's just a normal directory.

Table of Contents

  1. What is Git?
  2. What is a repository?
  3. Initializing a New Repository
  4. What happens behind the scenes?
  5. Cloning an Existing Repository
  6. Verifying a Git Repository