Top 22 Git Commands With Examples

Top 22 Git Commands With Examples

What is Git?

Git is an open source distributed version-control system for tracking changes in source code during software development. It is designed for coordinating work among programmers, but it can be used to track changes in any set of files. Its goals include speed, data integrity, and support for distributed, non-linear workflows.

How Git works?

  1. Git stores projects in repositories.
  2. Commits are made to the project and they tell Git that you are satisfied with the new or changed code you created.
  3. New code/changes are committed on branches. Most of the work is committed to other branches and then merged with the master branch.
  4. All this is stored in the same directory as the project but in a sub-folder called .git. 
  5. To share the code with your colleagues you push the changes to the repository. To get the new code from your colleagues, you pull changes from the repository.

Functions of version Control System

● Allows multiple users / developers to work on a project simultaneously
● It doesn’t overwrite the work/changes of other’s
● Maintains a history of every change / versions.

Why Git is Popular?

Free and Open Source

As it is an open source, you can download its source code and also perform changes according to your requirements.


Git provides better performance when it comes to version control system.Branching, committing, merging are all optimized to perform better than most of the version control systems.

● Security

Git uses a common cryptographic hash function called secure hash function (SHA1).This algorithm manages the files , versions and folders securely so that the work is secure and not corrupted.

● Distributed

Git is distributed in nature , which means the code on the git repository can be cloned or copied to the Developer’s systems so that he / she can work only on it.

● Branching Model

Git has different branching models so that you can have multiple branches which are independent of each other and takes only a few seconds to create , merge and delete branches.

Different Types of Version Control System

● Centralized version control system
● Distributed version control system

Git Commands with Examples

1) Telling Git Who You Are

Configure the author name and email address to be used with the commits.
git config --global "youremail"
git config --global "username"

Configure author name & email address

2) Creating New Local Repository

Create a folder in your local machine and then run the below command within the directory.
git init
The above command will create a repository in your local system.

Creating New Local Repository

and If you run ls -la in the current directory , You can find a folder .git is created.

3) Adding One or More Files to Git

To add a file to the Staging area
git add filename

To add more files to staging area
git add *

Staging area are the files that are going to be the part of the next commit.

4) Commiting Changes

Commit is used for saving changes. After adding files to the staging area using git add, You can commit those changes but not to the remote repository.
git commit -m "commit message"

Commiting Changes

To commit any files that you have added with git add and also commit any files you have changed since then.
git commit -a

The above command only works for tracked files (files which are added to git using .git command).

5) Checking Status of Git

Lists all the files that you've changed that still need to be added or commited.
git status

The above command will list all the files/folders that are not yet committed.

Checking Status of Git

6) To List All the Local Branches in the Current Repository

The command lists all the local branches in the current repository.
git branch

local branches in the current repository.

7) To Create a New Branch

This command will create a new branch.
git branch branchname

To check whether the branch is created or not , Run git branch

create a new branch

To delete a branch,
git branch -d branchname

delete  branch

8) Switching Between Branches

If you are working on different branches and If you want to switch between them.
git checkout branch

switch to branch

If you append -b to the above command , It will create a new branch and also switches to it.
git checkout -b branchname
Switch to new branch

9) Copying Remote Repository to Local Working Directory

To create a clone or copy of the targeted remote repository to the local working directory.
git clone:

copy targeted remote repository

If the remote git repository which you are cloning to the local working directory is not public , Then it asks for a password.

10) Fetch & Merge Remote Repository Changes to Local

The command will fetch for any changes in the remote repository and merge it with the local repository.
git pull

Fetch & Merge Remote Repository

Also Read: Implementing CI/CD pipeline on Azure

11) Tracking Changes

git diff command is used to track the difference between the changes made on the file.
git diff

Tracking Changes

The above command is used to find the differences between commits, branches, files, working trees, etc.
For example : The command will pick a file from the locally working directory and check for the changes.

It performs 2 checks , One to compare HEAD to staging area and one to compare staging area to work-tree.

12) Check Differences Between Staging Area and The Latest Version

git diff --staged

Lets say , You have a file which is already committed , Now you have made changes in that file and staged it.

To check the differences between the branches

The command will show the differences between the two branches.
git diff newbranch master

differences between branches.

13) Check the Version History of Current Branch

The command is used to list all the version history of the current branch. git log command lists the commits made in the branch in reverse order.
git log


To check the version history of a particular file,
git log --follow filename

14) To Check log messages and textual difference of a commit

git show command is used to view the changes on the specific commit and the metadata of a commit
git show commitID

15) To Tag a Specific Commit

This command is used to give a tag to a specific commit.
git tag v1.1 commitID

16) To List the Tags

using the below command , You can list the tags.
git tag


If you want to list the tags with a specific tag pattern , append -l to the above command.
git tag -l v1*

17) Merging Branches

This command is used to merge the specified branch's history into the current branch.

For example : Let's say your current branch is newbranch and you want to merge it with master , If you run the below command all the changes in the master branch will be reflected in the newbranch.
git merge master

18) Connecting Local Repository to the Remote Repository.

Using the below command , You can connect your local git repository to the remote git repository

git remote add origin

19) Pushing Local Branch to Remote Repository

if you have worked on a branch locally and you want to push that particular branch to the remote git repository, run the below command.

git push --set-upstream origin localbranchname


You May Also Like: Provisioning RDS Instances using Terraform

20) Stashing Git files

Using this command You can temporarily store all the modified tracked files.
git stash save


To restore the recently stashed files.
git stash pop


git stash list


To delete the stash
git stash drop stash@{0}


21) Removing a file from Git

The below command will delete the file from the staging area (index) 
git rm filename --cached

if you want to delete the file from git and as well as the file system,use the below command.
git rm filename

22) Git Reset

If you have staged a file and you want to unstage it ? , Use the below command. 
git reset filename

To reset all the changes after the specified commits , But the changes are locally preserved.
git reset commitID



We have learnt the most useful git commands with the examples. Keep checking out our website for more helpful articles!

Recent Comments:-

Nice Post... Check out some new common errors for git
6/9/2021 10:31:17 AM

Leave message