Top 20 Git Commands and Examples

There are hundreds of Git commands available to use for one reason or another. However, in this article, you will find the top 20 most used Git commands, along with a short description and an example of usage for each.

If you would like to view all other Git command examples you can see them all by going here.

*Please Note: These Git commands are in no particular order.

Top 20 Git Commands

 git config

This sets the configuration values for your username, email, gpg key, preferred diff algorithm, file formats and more:[ht_message mstyle=”info” title=”” show_icon=”” id=”” class=”” style=”” ]git config –global user.name “Your Username Here”
git config –global user.email “[email protected]”[/ht_message]

git init

This initializes a git repository, and creates the initial .git directory in a new or already existing project:[ht_message mstyle=”info” title=”” show_icon=”” id=”” class=”” style=”” ]git init

Initialized empty Git repository in /home/username/GIT/.git/[/ht_message]

git clone

This command creates a Git repository copy from a remote source. The command will also add the original location as a remote location so you are able to fetch from it again and push to it if you have permissions:[ht_message mstyle=”info” title=”” show_icon=”” id=”” class=”” style=”” ]git clone git@github.com:user/test.git[/ht_message]

git add

This will add file changes that are in your working directory to your index:[ht_message mstyle=”info” title=”” show_icon=”” id=”” class=”” style=”” ]git add[/ht_message]

git rm

This will remove files from your index and your working directory so they will not be tracked:[ht_message mstyle=”info” title=”” show_icon=”” id=”” class=”” style=”” ]git rm filename[/ht_message]

git commit

This Git command takes all of the changes written in the index, creates a new commit object pointing to it, and sets the branch to point to that new commit:[ht_message mstyle=”info” title=”” show_icon=”” id=”” class=”” style=”” ]git commit -m ‘committing added changes’
git commit -a -m ‘committing all changes, equals to git add and git commit’[/ht_message]

git status

This Git command shows the status of files in the index versus the working directory. It will list out files that are untracked (only in your working directory), modified (tracked but not yet updated in your index), and staged (added to your index and ready for committing):[ht_message mstyle=”info” title=”” show_icon=”” id=”” class=”” style=”” ]git status

# On branch master #
# Initial commit #
# Untracked files: #
# (use “git add …” to include in what will be committed) #

README[/ht_message]

git branch 

This lists existing branches, including remote branches if ‘-a’ is provided. It will create a new branch if a branch name is provided: [ht_message mstyle=”info” title=”” show_icon=”” id=”” class=”” style=”” ]git branch -a * master remotes/origin/master[/ht_message]

git merge

This will merge one or more branches into your current branch. It also automatically creates a new commit if there are no conflicts:[ht_message mstyle=”info” title=”” show_icon=”” id=”” class=”” style=”” ]git merge newbranchversion[/ht_message]

git reset

This command will reset your index and working directory to the state of your last commit. Effectively taking you back: [ht_message mstyle=”info” title=”” show_icon=”” id=”” class=”” style=”” ]git reset –hard HEAD[/ht_message]

git tag

This Git command tags a specific commit with a simple, human readable handle that never moves: [ht_message mstyle=”info” title=”” show_icon=”” id=”” class=”” style=”” ]git tag -a v2.0 -m ‘this is version 2.0 tag'[/ht_message]

git pull

This will fetch all the files from the remote repository and merge them with your local one: [ht_message mstyle=”info” title=”” show_icon=”” id=”” class=”” style=”” ]git pull origin[/ht_message]

git push

This Git command will push all the modified local objects to the remote repository and advances its branches:[ht_message mstyle=”info” title=”” show_icon=”” id=”” class=”” style=”” ]git push origin master[/ht_message]

git remote

This shows all the remote versions of your repository: [ht_message mstyle=”info” title=”” show_icon=”” id=”” class=”” style=”” ]git remote origin[/ht_message]

git log

This command will show a list of commits on a branch, and include the corresponding details:[ht_message mstyle=”info” title=”” show_icon=”” id=”” class=”” style=”” ]git log commit

847ttg41e8a0d768fb37ff7adohs6754b61a99a0abe Author: User Date: Wed June 11 08:37:07 2014 +0400 first commit [/ht_message]

git diff

This command will generate patch files or statistics of differences between paths or files in your git repository, index, or your working directory: [ht_message mstyle=”info” title=”” show_icon=”” id=”” class=”” style=”” ]git diff[/ht_message]

git archive

This command will create a tar or zip file that includes the contents of a single tree from your repository: [ht_message mstyle=”info” title=”” show_icon=”” id=”” class=”” style=”” ]git archive –format=zip master^ README >file.zip[/ht_message]

git gc

This is a garbage collector Git command. It will collect garbage from your repository and optimize the repository as well. You should run this periodically: [ht_message mstyle=”info” title=”” show_icon=”” id=”” class=”” style=”” ]git gc

Counting objects: 7, done.
Delta compression using up to 3 threads.
Compressing objects: 100% (7/7), done.
Writing objects: 100% (9/9), done.
Total 9 (delta 1), reused 0 (delta 0)[/ht_message]

git fsck

This will perform an integrity check of the Git file system and identify corrupted objects: [ht_message mstyle=”info” title=”” show_icon=”” id=”” class=”” style=”” ]git fsck[/ht_message]

git prune

This Git command will remove objects that are no longer pointed to by any object in any reachable branch. Like pruning a tree of useless branches: [ht_message mstyle=”info” title=”” show_icon=”” id=”” class=”” style=”” ]git prune[/ht_message]

Understanding how to use Git commands is important if you are going to be using Git a lot. Hopefully some if these more popular Git commands are easy to use and understand.

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.