vaisakh About Categories Tags

Git Intro: Basic Commands

14 February 2017

Git is a Version Control System (VCS), a system that keeps track of changes made to files.Git is also known as Source Code Management(SCM).

Let’s assume that you already know how to install Git or you already have Git installed on your machine.If you don’t, I recommend you to go to the official site (https://git-scm.com/downloads) and install Git first.

In this post, we will discus some of the basic commands of Git scm. To setup up git properties globally in your machine,

$git config --global user.name "Vaisakh VM"
$git config --global user.email "my@mail.com"

To setup Git properties specific to a single git repository,

$git config  user.name "Vaisakh VM"
$git config  user.email "my@mail.com"

Git has a working directory, the stage(also called index) and the history in the form of commits.

git workflow

Let’s look at some of the basic commands Git provides.

git add adds files from working directory to the staging area.

git add <filename>  -- adds single file to the staging area.
git add .  -- adds current directory to the staging area. 

git commit saves a snapshot of the stage(index) as a commit.

git commit -m "commit message" 

git commit --amend creates a new commit with the same parent as 
the current commit.
Use this to edit a commit message.

git reset unstages files.

git reset -- files, unstages files.that is, it copies files 
from latest commit to the staging area.

Use this command to "undo" a git add files.You can also do git reset 
to unstage everything.

git checkout

git checkout -- copies files from  the stage to the working directory.
Use this to undo local changes.
Also used to switch branches.

git diff

git diff  shows the difference between staging and working directory.

git diff --staged  shows the changes between HEAD(latest commit on 
current branch) and staging directory.

git diff HEAD -- shows deltas between HEAD and workding directory.

git cherry-pick

git cherry-pick copies a commit and creates a new commit on the current 
branch with the same message and patch as another commit.

git cherry-pick commit-id

There are useful Git client tools available.Use sourcetree if you are using mac or gitk on windows and linux machines.To learn more about Git, consider Pro Git (https://git-scm.com/book).