Tutorial 1 : Getting started with Git

Gaurav Patil
3 min readJul 17, 2022

--

Photo by Gabriel Heinzer on Unsplash

We will cover following topics in this tutorial :

Git and Github

Step 1 : Creating a git repository

Step 2 : Checking the status of a file

Difference between working directory and local repository

Step 3 : Adding the file into repository

Staging area and commit history

Git and Github :

Git - version control system

Version control - tracking and managing changes to software code files

Thus Git allows many developers to work together by keeping track of the changes in a set of code files.

Github — cloud-based service that allows to manage Git repositories

cloud — servers that are accessed over the Internet

server — a computer program or device that provides a service to another computer program and its user (client)

First create your Github account.

Create a folder on desktop. I have created a folder named “trial” on my PC’s desktop. Open command prompt and write command given below :

cd <your folder path>I ran following command:cd C:\Users\Gaurav\OneDrive\Desktop\trial

Create a sample.html file and store it in this folder.

<html>
<body>
<h1>This is a heading.</h1>
</body>
</html>

Step 1 : Creating a git repository

Run following command in command prompt :

git init

output :

Initialized empty Git repository in C:/Users/Gaurav/OneDrive/Desktop/trial/.git/

This command creates a git repository in that folder. You can check .git folder created here by unhiding the hidden files in this folder.

Repository — collection of code files of a project

Why does this show empty repository even if ‘sample.html’ file is already present in the folder ?

Answer can be found by checking the status as shown below.

Step 2 : Checking the status of a file

Let’s run following command in command prompt:

git status

Output :

On branch masterNo commits yetUntracked files:
(use “git add <file>…” to include in what will be committed)
sample.html
nothing added to commit but untracked files present (use “git add” to track)

If you run it on your machine, it will show sample.html file in ‘red’ color meaning it is not in the repository.

Actually this file is in working directory(workspace or Project folder) but not in the repository.

Difference between working directory and local repository :

working directory= workspace = project folder

‘trial’ folder contains — working directory & local repository

  • .git folder is our local repository
  • anything except .git folder is working directory (sample.html)

Local repository contains — staging area and commit history

Now let’s first add this html file in our git repository then we will dive deeper into staging area and commit history.

Step 3 : Adding the file into repository

Run following command in command prompt:

git add sample.htmlgit commit -m "sample file added"

Output:

[master (root-commit) fc9e515] sample file added
1 file changed, 5 insertions(+)
create mode 100644 sample.html

Thus we have added sample.html file in the git repository.

Let’s check status again in command prompt:

git status

Output :

On branch master
nothing to commit, working tree clean

It means there is no file remaining to be added in the git repository since sample.html is in the repository now.

Staging area and commit history :

Local repository contains — staging area and commit history

The git command `git add sample.html` adds the sample.html file to the staging area .

This command copies the file from the working directory to the staging area and it is not moved. So this file can also be seen in the trial folder since it is not moved.

The git command git commit -m "sample file added" takes a snapshot of your repo at a specific point in time. It also allows us to write message (like “sample file added”) to let others know what changes one has made in the repository. This is a part of commit history.

Congratulations !!! You have gained introductory knowledge about using git.

We said Git tracks changes in the code files. But how does that happen ? Take a deep breathe and move to the next tutorial to know more about tracking and managing the changes to code files.

--

--

Gaurav Patil
Gaurav Patil

Written by Gaurav Patil

Machine Learning Engineer drawing insights from mathematics.

No responses yet