Skip to main content

Welcome to a Grasp lesson

Create a free account to keep studying and create your own study path.

Lesson 33

Git Repository Initialization and Structure

Recommended resources

Hello! Welcome to the first lesson in our new module, Version Control with Git.

In the previous module, we focused on setting up and managing the underlying infrastructure, culminating in deploying a live Nginx web server. We've built the "stage." Now, it's time to learn how to manage the "performance"—the code and configuration files that run on that infrastructure. This is where version control becomes absolutely essential.

Today's lesson addresses the learning outcome: Initialize Git repositories and understand the structure of the .git directory. We will start from the very beginning: understanding what version control is, why it's a cornerstone of modern software development and DevOps, and how to create your first Git repository. By the end of this lesson, you'll know how to turn any project folder into a trackable repository and have a clear map of the hidden .git directory where all the magic happens.

1. What is Version Control and Why Do We Need It?

Imagine you're writing a complex script. On Monday, it works perfectly. On Tuesday, you add a new feature, and it breaks. How do you get back to Monday's working version? Or, imagine you and a colleague are both editing the same configuration file. How do you merge your changes without overwriting each other's work?

These are the core problems that a Version Control System (VCS) solves. A VCS is a tool that tracks and manages changes to files over time. It allows you to:

  • Keep a complete history of all changes.
  • Revert to previous versions of your project at any time.
  • Collaborate with multiple people on the same project simultaneously and efficiently.
  • Understand who made what change, when, and why.

Git is the world's most popular Distributed Version Control System (DVCS). The "distributed" part is key. Unlike older centralized systems where everyone had to connect to a single master server, Git gives every developer a full copy of the project's history on their local machine. This makes it faster and more resilient.

To get a solid grasp of these foundational concepts, please watch the following video.

Day-9 | Git and GitHub | What is GIT ? | What is Version Control ? | #devops #2023 #github #gitlab

This video from Abhishek.Veeramalla provides an excellent explanation of what version control is, the problems it solves, and the fundamental difference between centralized and distributed version control systems.

Please watch from the beginning until 12:41. Focus on understanding: The two main problems VCS solves: sharing code and versioning (00:33 - 05:43). The difference between a Centralized VCS (like SVN) and a Distributed VCS (like Git) (05:43 - 12:41).


2. Git vs. GitHub: A Crucial Distinction

Before we go further, it's important to clarify a common point of confusion.

  • Git is the version control software itself. It's a command-line tool that you install and run on your local machine to track your project's history.
  • GitHub (as well as GitLab, Bitbucket, etc.) is a web-based hosting service for Git repositories. It provides a central place to store your code online, enabling collaboration through features like pull requests, issue tracking, and more.

In simple terms: Git is the tool; GitHub is the service that hosts projects using that tool. You can use Git perfectly fine without ever using GitHub, but they are incredibly powerful when used together.

The next segment of the video you just started explains this distinction well.

Day-9 | Git and GitHub | What is GIT ? | What is Version Control ? | #devops #2023 #github #gitlab

Let's continue with the same video to clear up the difference between Git and GitHub.

Watch the segment from 12:41 to 15:59. The speaker clearly illustrates how GitHub builds on top of the core Git technology.


3. Creating Your First Git Repository

Now for the hands-on part. To start tracking a project with Git, you first need to initialize a repository. This is done with the git init command. This command tells Git, "I want you to start watching this folder for changes."

It does this by creating a special, hidden subdirectory named .git right inside your project folder. This single folder contains the entire history and configuration for your repository.

Before you begin:
You need to have Git installed. The video below briefly mentions how to find installation instructions. You can find them at the official Git website. Since you're using a Linux environment, it's as simple as running sudo apt update && sudo apt install git.

Now, let's see git init in action.

Master Git Init: Unlock the Secrets of the Hidden .git Folder in 20 Minutes

This video from Hitesh Choudhary provides a concise and practical demonstration of using git init and shows the immediate effect it has.

Watch from 10:10 to 12:12. Pay attention to the output of the git status command before and after git init is run. This shows how a directory changes from being untracked to being a Git repository.

Let's do it yourself:

  1. Open your Linux terminal.
  2. Create a new directory for our practice project: mkdir my-first-repo
  3. Navigate into it: cd my-first-repo
  4. Check the Git status: git status. You'll get an error saying it's not a Git repository.
  5. Now, initialize it: git init
  6. Check the status again: git status. You'll now see a different message indicating you're on a branch and there are no commits yet.

You have just created your first local Git repository!


4. Inside the Engine Room: The .git Directory

So, what did git init actually do? It created a hidden .git directory. You can see it by running ls -la in your my-first-repo folder. This directory is the brain of your repository.

Warning: You should almost never modify files inside the .git directory manually. Doing so can corrupt your repository and lead to data loss. Always interact with it through Git commands.

Let's take a quick look inside this directory with the video, and then we'll dive into what each part does with a more detailed article.

Master Git Init: Unlock the Secrets of the Hidden .git Folder in 20 Minutes

Let's continue with the video to get a visual tour of the .git directory.

Watch from 12:19 to 14:28. The presenter shows the contents of the .git directory and gives the crucial advice to not edit it by hand.

Now that you've had a peek, let's understand the purpose of these files and folders. This knowledge is key to mastering Git.

How to initialize a new Git repository with the Git init command

This article from Graphite provides a clear and structured breakdown of the most important components within the .git directory. Reading this will give you the detailed 'notes' you need to understand how Git works internally.

Please read the section titled 'The .git directory structure'. Focus on understanding the role of these key items: HEAD: Points to your current location (branch or commit). config: Contains repository-specific settings. objects/: Stores all your data (commits, files, etc.). refs/: Stores pointers to your commits (like branches and tags). hooks/: A place for scripts that can automate tasks during the Git workflow.

Test your understanding!

You've just run git init in your project folder. A colleague asks you to send them the "Git repository." Which of these is the correct action?

  1. Zip the entire project folder, including the .git directory.
  2. Zip only the .git directory, since that's where Git stores everything.
  3. Tell them you can't, because the repository only exists on your machine for now.
Show answer

Option 3 is the most accurate. While you could zip the folder (Option 1), that's not how Git collaboration works. The repository created by git init is purely local. To share it, you would typically push it to a remote hosting service like GitHub, and your colleague would then clone it from there. We'll cover push and clone in a future lesson. Zipping just the .git directory (Option 2) is incorrect as it's missing the actual project files (the working tree).


5. git init vs. git clone

It's useful to distinguish git init from another command you'll see frequently: git clone.

  • git init: Creates a new, empty Git repository from scratch in an existing directory. You use this when you are starting a brand-new project.
  • git clone: Creates a copy of an existing Git repository, usually from a remote location like GitHub. You use this when you want to contribute to a project that already exists.

git init | Atlassian Git Tutorial

This brief section from an Atlassian tutorial clearly explains the difference between init and clone.

Read the section 'git init vs. git clone' to solidify your understanding of when to use each command.


Conclusion

Great work on taking your first step into version control with Git! This foundational knowledge is non-negotiable for anyone in a DevOps role.

Key Takeaways:

  • Version Control Systems (VCS) like Git are essential for tracking changes and collaborating on code and configuration.
  • Git is a distributed VCS, meaning every user has a full copy of the project's history.
  • git init is the command used to create a new, local Git repository inside a project directory.
  • This command creates a hidden .git directory, which contains all the repository's data, history, and configuration.
  • The .git directory is Git's internal database and should not be manually edited.

In our next lesson, we will build directly on what we did today. Now that we have an initialized repository, we need to learn how to tell Git which changes we want to save. We'll explore the "staging area" and use the git add and git commit commands to create our first "checkpoint" in the project's history.