Understanding Git Commands

Apart from knowing programming languages and IDE’s, a developer should also have knowledge of VCS to manage his code effectively.
In this article, we will be specifically targeting Git on using it and understand various git commands visually.

Git is Distributed Version Control System(VCS). Now let’s try to break down the term “Distributed Version Control System”.
Version Control System or VCS is the system which stores history(versions) of files over time and
Distributed means that every local git repository of your project can act as the remote repository for others to do pull or push.

Let us understand some git commands which are used to move the files from working area to staging area until committing and pushing the files to the remote repo and at the end of the article there is flow diagram to understand and remember git commands visually.

I have categorized git commands into four sections based on functionality they provide:-

  1. Initializing repository.
  2. Getting repository info.
  3. Git operations.
  4. Getting operations Info.

Initializing Git repository

git init

initializes current directory as git repo(local) which means that every change in the directory will be versioned by git.
When to use – If you do not have remote repo than use git init otherwise use git clone.
usage –  git init 
In the screenshot below, directory Geek As Service is initialized as the git repo.

result of git init in image
git init output
git clone

Clones/Downloads remote repository hence local intialized.
usage git clone https://www.github.com/geek-as-service/git-commands 

Getting repository info

git remote -vShows list of remote URLs for your local repository.
Now, If you had used git clone for creating local repo, then remote URL will be present in the output.
but, if you had used git init then no remote URL will be listed. git remote add command can be used to add remote URL to local repo.
usage git remote -v 
In the screenshot below, the output shows remote URLs with name “origin” for pushing and pulling the changes.

result of git remote in image
git remote -v shows remote URL for local repo
git status

git status shows the status of local repo. It lists newly created(untracked files), files with changes(modified files) and changes ready to committed(files in the staging area). It also lists unmerged files if there was conflict.Some terms which will be used more frequentlyUntracked Files – Files which are not tracked by git.
Tracked Files – Files which are being tracked by git for changes. If the change is found in the file then it will show the file as modified file but if there is no change in the file since the last commit then the file will not be listed in the output of git status.
Staged File – Tracked files which have some changes and these changes have been approved using git add and can be committed using git commit.
usage –   git status 
git status –short –branch 
In the screenshot below, git status command was executed with two flags “short” and “branch” which shows the status in a single line for each file. As you can see, “first file” is in staging area while “second file” is modified file and “third file” is untracked file.

result of git status --short --branch in image
git status –short –branch shows state of each file

Moving on to next set of commands which allows us to do the following operations

  1. add files.
  2. commit changes.
  3. pull new changes from a remote repo to the local repo.
  4. push local repo to a remote repository.
  5. add remote URL to local repo.

Git Operations

git add

git add will move the modified file to the staging/indexing area. git add is also used to start tracking untracked files.
usage git add PATH_OF_FILE 
In the screenshot below, git add moves “first file” to the staging area.

result of git add in image
git add moves untracked, tracked into staging/index area.
git commit

Commit’s the indexed/staged changes to local repo.
usage git commit 
 git commit -m “SINGLE LINE MESSAGE” 
In the screenshot below, “first file” was in staging area but after running git commit it has been committed.

result of git commit in image
git commit commits all those files which were staged/indexed.

git pullAfter committing changes, you need to synchronize your local repo with remote repo. To do that you pull all the new changes made by others in remote repo using above command.
usage  git pull 
 git pull origin master 
git pull origin master command pulls changes from remote URL “origin” and branch “master”.

git pushPushes your local repo to remote repo, and hence your changes become available for others.
usage  git push 
git push origin master  

git remote add NAME_OF_REMOTE REMOTE_URLThis command is used to add remote URL to local repo.
usage git remote add origin URL 
In the screenshot below, we are adding remote URL “https://github.com/geek-as-service/git-commands” by name “origin”.

result of git remote add in image
git remote add output

Next set of commands focus on knowing more about what had been done and what is going in local repository, i.e.

  1. list of commits.
  2. changes/files which have been committed but not pushed.
  3. changes in the particular old commit.

Getting Info regarding Git Operations

git logShows the history of commits.
Each commit has commit hash, author, date and commit message. Commits are shown in chronological order(latest commit on top).
usage –   git log 
In the screenshot below, git log shows two commits. Commit hash is jibberish written as “commit e7f2b69c6daf9d3417f…….”. This hash is useful when using git show.

result of git commit in image
2 commits are shown in git commit output
git show COMMIT_HASH

Shows all the changes done in particular commit. We can either use first five characters of commit hash or use complete commit hash.
usage –  git show e55ce 

result of git show in image
git show Shows changes in all files in particular commit.

git show  REMOTE_NAME/BRANCH_NAME..BRACH_NAMEShows all the changes which you have committed but have not pushed.
usage –  git show origin/master..master 
In Screenshot below, the complete command is explained. “..” is difference between origin/master” and “master”

git show origin explained.
git show origin explained.

git diffShows all the changes in tracking area which you have not staged or indexed. git status just shows the list of files but git diff shows the exact changes in each file. git diff only shows files which have been modified and have not been staged. It also does not shows untracked files.
usage –  git diff
In the screenshot below, git diff shows all the changes which are in tracking with detail that whether these changes have been added or removed.

result of git diff in image
git diff shows the changes in working area.

git diff –cachedShows all the changes in staging area i.e changes which are ready to be committed. It shows the modified changes which have been staged.
usage –  git diff –cached 

result of git diff --cached in image
git diff –cached shows changes which have been staged.

And Now is the Git flow chart to remember git commands.


The above image list all commands to move from untracked files to tracking area and staging area and then back to tracking area, untracked area and last committed state of file.

4 thoughts on “Understanding Git Commands

    1. Thanks for visiting the article.
      Hope following commands help:-
      1. git branch – will list all the branches at local(branch name with * is the active branch).
      2. git checkout BRANCH_NAME – will switch you to BRANCH_NAME branch.
      3. git checkout -b BRANCH_NAME – it will create BRANCH_NAME branch and switch to this newly created branch.
      4. git branch -D BRANCH_NAME – it will delete the BRANCH_NAME branch from local.
      5. git merge BRANCH_NAME – it will merge the code from BRANCH_NAME branch to current active branch.

      push and pull commands works in same way as mentioned in above article.

Leave a Reply

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