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:
- Creating snapshots or backups of your work, ensuring safety from accidental loss.
- Facilitating concurrent work on multiple versions of the same file, reducing conflict among team members.
- Merging diverged changes into a unified and cohesive version, maintaining code integrity.
- Keeping detailed records of who modified each piece of code and when they made these changes, enhancing accountability.
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:
- A blob symbolizes a file version. Each unique version of a file is stored as a separate blob, ensuring that changes are tracked at the file level.
- A tree is akin to a directory listing. It signifies a directory and encompasses blobs and other trees, which can be seen as subdirectories, allowing for a hierarchical representation of the file system.
- A commit object includes metadata for each change set. This metadata typically consists of the author, date, commit message, and pointers to the tree object that corresponds to the top directory of the stored snapshot. Additionally, commit objects may include pointers to parent commits if they exist, which helps in maintaining the history of changes.
- References, or refs, function as pointers to commit objects. These refs are used to track branches, tags, and other important points in the repository, allowing users to easily navigate and manage different states and versions of their project.
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.