Introduction
In this lab, you will take your first steps into version control with Git. You will learn how to turn a standard directory into a Git repository, which allows you to track changes to your files over time.
This is the moment when an ordinary folder becomes a versioned project. Git does that by creating a hidden .git directory that stores history, metadata, and references.
That hidden directory is the repository's internal database. The visible files are your project content, while .git is the machinery Git uses to remember versions and relationships between commits.
The process involves three main stages:
- Initializing: Setting up the repository.
- Staging: Preparing files to be saved.
- Committing: Saving a snapshot of your files to the repository history.
By the end of this lab, you will have a local Git repository with your first committed file.
Create a project directory project_x
In this step, you will create a new directory to hold your project files. In Git, a repository is usually contained within a single directory.
First, open your terminal. You are currently in the /home/labex/project directory. We will create a new directory named project_x using the mkdir command.
Run the following command:
mkdir project_x
Now, navigate into this new directory using the cd command:
cd project_x
It is important to be inside the directory where you want to initialize Git. You can verify your current location using the pwd command:
pwd
You should see /home/labex/project/project_x as the output.
Initialize git inside it using git init
In this step, you will turn your ordinary directory into a Git repository. This process is called "initializing".
Run the following command inside your project_x directory:
git init
You should see output similar to:
Initialized empty Git repository in /home/labex/project/project_x/.git/
This command creates a hidden subdirectory named .git. This hidden directory contains all the necessary metadata and object database for Git to track your project. You don't need to modify anything inside .git, but it's good to know it's there.
So git init does not "upload" anything or create history by itself. It simply prepares the directory so Git can begin tracking future snapshots locally.
You can verify its existence by listing all files, including hidden ones:
ls -a
You should see .git in the list.
Create a file readme.txt
In this step, you will create a file to track. An empty repository isn't very useful, so let's add some content.
We will create a simple text file named readme.txt. You can use the echo command to create the file and add some text to it simultaneously.
Run the following command:
echo "This is my first git project" > readme.txt
This command creates readme.txt with the content "This is my first git project".
Verify that the file was created:
ls
You should see readme.txt listed.
Check the repository status using git status
In this step, you will check the state of your repository. Git doesn't automatically track every file you create; you have to tell it to.
The git status command is one of the most useful commands in Git. It tells you which files are tracked, which are modified, and which are staged for commit.
Run the command:
git status
You should see output similar to this:
On branch master
No commits yet
Untracked files:
(use "git add <file>..." to include in what will be committed)
readme.txt
nothing added to commit but untracked files present (use "git add" to track)
Notice that readme.txt is listed under Untracked files. This means Git sees the file but is not yet tracking changes to it.
This introduces an important Git distinction: a file can exist in your working directory without being part of version control yet. Git is aware of it, but it has not been included in the repository history.
Add the file to caching area (staging) using git add
In this step, you will move your file to the "staging area" (also known as the index). The staging area is where you prepare a snapshot of a set of changes before committing them to the official history.
The staging area is one of Git's most important concepts. It sits between your working directory and the repository history, letting you decide exactly which changes belong in the next commit.
To start tracking readme.txt and stage it, use the git add command:
git add readme.txt
Now, let's check the status again to see what happened:
git status
The output should now look like this:
On branch master
No commits yet
Changes to be committed:
(use "git rm --cached <file>..." to unstage)
new file: readme.txt
Notice that readme.txt is now under Changes to be committed. This means it is staged and ready to be committed.
This is why Git is often described as a three-state workflow: files in the working tree, changes in the staging area, and snapshots in commit history.
Commit the file using git commit -m
In this step, you will permanently save the changes currently in the staging area to the repository's history. This action is called a "commit".
A commit saves a snapshot of the project and requires a message describing what changed. We use the -m flag to provide this message directly in the command line.
Run the following command to commit your changes:
git commit -m "Initial commit"
You should see output confirming the commit:
[master (root-commit) 1a2b3c4] Initial commit
1 file changed, 1 insertion(+)
create mode 100644 readme.txt
Now, check the status one last time:
git status
The output should say:
On branch master
nothing to commit, working tree clean
This indicates that your working directory matches the latest commit in the repository. You have successfully saved your first version!
Summary
In this lab, you have successfully initialized a local Git repository. You learned how to:
- Create a project directory.
- Initialize Git using
git init. - Create a new file.
- Check the repository status with
git status. - Stage a file using
git add. - Commit the file to the repository history using
git commit.
These are the fundamental commands you will use daily when working with Git. You can now track changes, save versions, and manage your project's history.



