Essential Git Command’s

Essential Git Command’s

Sugan
SuganJul 12, 2020

Hello developers,in this post we are going to look into some of the essential (basic) git command that makes your dev life better. Before we dive into to the Git command’s let first have a quick look at what is git..? & why we use git..?. If you understand what is git and why we use git, You will automatically come to know how it plays a important role in development.

What is Git..?

Git – version control system

Git is a free, open source distributed version-control system for tracking changes in source code during development. It is designed for coordinating work among programmers, but it can be used to track changes in any set of files. It was created by Linus Torvalds in 2005 to develop Linux Kernel. In simple Words it is used to manage or record the history of our source code

Things to Know

Git is written in
C, Shell, Perl, Tcl, Python

Why we use git..?

In my point of view git is mainly used for the below listed purposes:

  1. It is used to record history of your work.
  2. It allows you to create a remote copy which will help you to access from wherever you want and through any machine.
  3. It can be used to restore previous versions of files.
  4. It is used to compare and analyse code.

Briefly about why we use git with a real time example,In the below picture Bob is a developer and Marry is one of his loyal client they have met to discuss about there new project, below the picture you will find requirement of marry.

client metting
credits-Business vector created by teravector – www.freepik.com

marry need Trello app with Add ,Edit and Delete options

Then Bob started to working on the Project .After a couple of day marry came to Bob and said she wants only Add and Delete feature. Now Bob will Remove the code for edit feature. After few weeks Bob completes the project and now marry again wants the Edit feature back for some reason now what Bob will do…? As a developer its Bob’s duty to full fill the client need. So Bob will again write the code he needed for Edit feature .Its takes extra time to do so.

If Bob use git, as i said we will have the history of our source code so it will be easy for him to get the Edit feature back by going back to history,it saves Bob’s time.

This is how git makes the dev life better.(It’s one of the use case of git )

What if Bob had a team🤔

In this paragraph i will quickly go through another use full feature of git .The above mentioned bob story is for single developer working on a project. After few years Bob hired few members now they will also work on the projects, in the previous case there is only one developer so there will be no problem but now the teammates also work on the same project. As Bob uses git now he can create remote repo(which is called Master) where he can give access to his team member.so they can also start working on the same projects by cloning the project to their local machines. once they complete their task they can push it back to the central repo by doing this everyone can will aware of everyone’s work.

Now Every One In The Team know who did what, when, and why. 

Git Commands

Let’s Now talk about the Git Commands that you will be using frequently while you are working with git.

Git Config:

Git Configurations commands are used to register our information with git ,which can be used later.

git config –global user.name “[name]”
git config –global user.email “[email address]”

The above mentioned command are used to sets the author name and email address respectively.

Git Init

git init
This command is used to start a new repository.

Git Clone

git clone 'url'
This command is used to obtain a repository from an existing URL.

Git Add

 git add [file]  
This command adds a file to the staging area.

 git add *  
This command adds one or more to the staging area.

Git Commit

git commit -m “[ Type in the commit message]”
This command records or snapshots the file permanently in the version history.

git commit -a  
This command commits any files you’ve added with the git add command and also commits any files you’ve changed since then.

Git Status

git status  
This command lists all the files that have to be committed.

Git Log

git log  
This command is used to list the version history for the current branch.

Git Branch

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

git branch [branch name]  
This command creates a new branch.

git branch -d [branch name]  
This command deletes the feature branch.

Git Checkout

git checkout [branch name]  
This command is used to switch from one branch to another.

git checkout -b [branch name]  
This command creates a new branch and also switches to it.

Git Merge

git merge [branch name]  
This command merges the specified branch’s history into the current branch.

Git Remote

git remote add [variable name] [Remote Server Link]  
This command is used to connect your local repository to the remote server.

Git Push

git push   
This command sends the committed changes of local branch to your remote repository.

git push [variable name] [branch]  
This command sends the branch commits to your remote repository.

 git push –all [variable name] 
This command pushes all branches to your remote repository.

Git Fetch

git fetch

This command is used to download contents from a remote repository to Local

Git Pull

git pull   
This command fetches and merges changes on the remote server to your working directory.

git pull --prune
This command fetches and merges changes on the remote server to your working directory and also delete remote refs that are no longer in use on the remote repository.

Git Stash

git stash
This command temporarily stores all the modified tracked files.

git stash pop
This command restores the most recently stashed files.

git stash list  
This command lists all stashed changesets.

 git stash drop  
This command discards the most recently stashed change set.

Git rm

git rm [file] 
This command deletes the file from your working directory and stages the deletion.

Git Tag

git tag [commitID] 
This command is used to give tags to the specified commit.

Git Show

git show [commit]  
This command shows the metadata and content changes of the specified commit.

Git Reset

git reset [file]
This command unstages the file, but it preserves the file contents.

git reset [commit]  
This command undoes all the commits after the specified commit and preserves the changes locally.

git reset –hard [commit]
 This command discards all history and goes back to the specified commit.

Git Diff

 git diff  
This command shows the file differences which are not yet staged.

 git diff –staged 
This command shows the differences between the files in the staging area and the latest version present.

git diff [first branch] [second branch]  
This command shows the differences between the two branches mentioned.

Conclusion

To learn more about git check out the link below

https://git-scm.com/doc

The above listed are some of the essentials of git command if you want to know more about git command checkout the link given below

https://git-scm.com/docs

Prev Post

How to rebase in git

Next Post

SASS

Related Articles