Let's Discuss!

Call Us:
zenesys-u.s.-contact +1 (323) 565-1141
zenesys-u.s.-contact +91-120-4281621
zenesys contact us
Top Git Commands with Examples

Top 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

How Git works?

Git stores projects in repositories.

Commits are made to the project and they tell Git that you are satisfied with the
new or changed code you created.

New code/changes are committed on branches. Most of the work is committed to
other branches and then merged with the master branch.

All this is stored in the same directory as the project but in a sub-folder called .git.

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 user.name "username"
git config --global user.email "youremail"

2) Creating new local repository

Create a folder in your local machine and then run the below command within the

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 the 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 need to be added or commited.
git status

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

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 you 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

git clone: https://Rahulmuthu80@bitbucket.org/Rahulmuthu80/latest.git

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 https://Rahulmuthu80@bitbucket.org/Rahulmuthu80/latest.git

11) Tracking Changes

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

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 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

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

git tag -l v1*

17) Merging branches

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

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

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

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

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:-

There are no comments yet.

Leave message