Top 22 Git Commands Explained With Examples

In this blog post , I have explained 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.

Git Commands

1)Telling Git Who you are.

Configure the author name and email address to be used with the commits.

git config --global "rahulk"
git config --global

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.

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 staging area using git add, You can commit those changes but not to the remote repository.

git commit -m "commit message"

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 needs to added or commited.

git status

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

6)To list all the local branches in the current repository

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

git branch

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

To delete a branch,

git branch -d branchname

8)Switching between branches

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

git checkout branch

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

git checkout -b branchname

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

If the remote git repository which you are cloning to the local working directory is not public , Then it asks for 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

11)Tracking Changes

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

git diff

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 commited , 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 master development

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 specified commit and the metadata of a commit

git show commitID

15)To tag a specified commit

This command is used to give a tag to a specified 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 : Lets 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 respository

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

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

To move the HEAD to a specified commit.(To hardreset files to HEAD on git)

git reset --hard commitID


We have learnt the most useful git commands.

Hope you find this article helpful , Thanks for reading.

Please do check out my other articles.