Git Guide

Muhammad Sarmad Qadeer
6 min readOct 6, 2021
Feature Image

Git is a Version Control Software. It helps you to track changes and maintain versions of your project/assignment or document.

Lets Start Learning Git

We will learn git through the command line.

before doing anything else just make sure that git is installed on your PC by typing the following command in the command-line interface:

git --version

If it gives any version number then it means git is installed else install it.

After that navigate to the folder which you want to track or of which you want to create versions. The folder may be empty or have some files in it.

Empty Folder

Right-click on the folder and click on the Git Bash Here option.

A shell screen opens. Type git init in it.

1. git init

git init

With this command, you are actually giving the responsibility of maintaining versions of our folder (Git Demo in this example) to git.

with git init you are saying git to please take care of this folder for you like you say something to your friend or someone close to do for you.

When this command is executed a hidden .git folder is created in that specific folder for which we executed this command.

Working Directory

Repository

.git folder is known as a repository in git terms. This repository keeps track of changes and stores the versions of our folder(Git Demo). We don’t need to do anything to the .git repository. If we delete the .git repository all the versions that we created just got deleted.

Working directory

After the creation of the .git repository in our folder(Git Demo), our folder is no longer a common folder now we call this a working directory. The working directory contains:

  • All the files or folders on which we are working.
  • A .git repository.

A .git repository has two sections:

1- Staging area:

The staging area sets the path for the next commit.

A commit is a snapshot or you call it a version of your project at a specific point in time. If you create a commit of your project at a specific point in time then that commit holds the version of your project at that time.

The staging area makes the preparation for the next commit because of this it contain that versions of files that you want to include in your next commit.

2- Commits section:

commits section holds the commits.

2. git add

git add filename

This command is used to add files to the staging area.

  • If you wanna add a single file to the staging area: git add filename
  • If you wanna add multiple files to the staging area: git add filename filename ...
  • If you wanna add all files in the working directory to the staging area: git add .

Sometimes you don't want to add all the files in your next commit in those circumstances you use git add filename to add files to the staging area but in most cases, you will only need git add . command.

3. git commit

git commit -m "message for your commit"

This command is used to create a commit of your files that are in the staging area. With the -m flag you provide the message to your commit which helps you to remember/recognise what was this commit/version about.

4. git status

git status

This command shows you the status of the working directory and the staging area. It tells you about:

  • which files or folders are untracked
  • which files or folders are updated and not staged
  • which files or folders are ready for the next commit

5. git log

git log
git log --oneline

This command shows you your commits history or you call it your versions history.

what is commit?

A commit is a snapshot or a version of your project at a specific point in time. A commit holds information about:

  1. The Author who committed/made that commit.
  2. The time when that commit occurred.
  3. The content of that specific commit.
  4. A reference to its parent commit.

The first-ever commit of your project does not have any parent else that each commit points any parent commit. The current commit on which you are standing becomes the parent of your next commit.

Concept of Branch in Git

A branch is nothing but a pointer or a reference to a commit. Each commit when created has a unique id assigned to it. And we humans are not good at memorising those ids. So to point out the commit in a human-friendly way a concept of the branch came into the picture.

A branch holds the id of the latest commit in order to point it out and each commit points to its parent commit. And this becomes a linked list of commits. Through branch, we move to the latest commit and through that, we can move back to the previous commits.

In git branches are mandatory. When we do the first commit by default a branch named master is created and points to our first commit.

Purpose of creating a branch

Basically, it is not mandatory for you to create a branch. you can do all your work in your master branch. But it is a good practice to create a new branch when you want to add a new feature or to fix a bug.

we can have more than 1 branch in git.

Creating a branch:

6. git branch

git branch <branch-name>

A branch is nothing but just a pointer to a commit. With the execution of the above command, a pointer with some name is created and starts pointing to the current commit on which we are standing at the point of executing this command.

If we create new branches so it means we can have multiple branches. So on which branch we are committing our changes?

We are committing our changes on our HEAD.

HEAD

A HEAD is nothing but just a pointer to a current on which we are working. HEAD holds the name of the branch in order to point it out. So a branch whose name is in the HEAD variable is the current branch on which we are working. So a commit that we will do will be on that specific branch whose name is in the HEAD.

7. git checkout

check out means to observe something.

If you want to move to any commit/version to see what is in that version you will use the git checkout command.

git checkout <commit-hash/branch-name>

As a branch is just a pointer to a commit so you can switch a branch with the git checkout command.

With this command, you are just changing the position of the HEAD without affecting any commit.

When you check out any commit that specific commit is loaded into your working directory.

8. git reset

git reset resets the HEAD to a specified state.

git reset is used when we want to undo any changes and move back to any previous commit and discard all the commits in between,

Then why it is called a branch?

…to be continued

--

--

Muhammad Sarmad Qadeer

Full-Stack Web Developer • Front-End Expert • Opensource Contributor @GitHub • Developer Advocate