Friday, August 7, 2015

Basic Git commands

                   Git  ( Distributed revision control )     

    Git is a version control system that is used for software development and other version control tasks. As a distributed revision control system it is aimed at speed,[8] data integrity, and support for distributed, non-linear workflows.

Git was created by Linus Torvalds in 2005 for development of the Linux kernel, with other kernel developers contributing to its initial development. Its current maintainer is Junio Hamano.

As with most other distributed version control systems, and unlike most client–server systems, every Git directory on every computer is a full-fledged repository with complete history and full version-tracking capabilities, independent of network access or a central server

 Type of Source Control Models
  • Master repository
  • Commits effect everyone immediately
 Centralized - Check-in/Checkout
  • VSS & TFS (prior to TFS 2010)
  • Always connected to server before editing
  • Limited offline support
Centralized - Edit/Commit
  • TFS 2010 & Above, SVN
  • Connect to the server when changes are ready
  • Better offline support
  • No Offline access to history
Distributed Version Control
  • Git, Mercurial,Bazaar
  • Commits are local
  • Version of commit is maintained
  • No checkout, mirror the repository
  • Sever failure can be replaced by client
  • Every clone is the full backup of data
  • Easy collaboration with different group
  1. Installation of git can be found online.
  2. Command line interface is  better than GUI.These are the list of command line interface
Basic commands in Git
init         : Create a repository
status     : Gives status of the local repository
              : Does not show you if it is not committed or pushed
              : Shows us non committed changes
add        : Used to add the file to the repository
commit  : Commits to our local repository
               save of current working directory
log         : See the changes when committed
push      : Changes that are committed locally would be moved to a central repository
pull       : Pull changes down from another repository
Merge    : Used to merge the changes

These are the list of commands that would be helpful.
Add new file in git 
touch webpage.html

//create a new repository on the command line
echo "# MyDiary" >>
git init
git add
git add -A
git commit -m "first commit" git remote add origin git push -u origin master or push an existing repository from the command line
 //If it is a first commit in a new machine it would ask for user credentials
git config --global "Your Name"
git config --global
git remote add origin
git push -u origin master
//sometime on error we might have to use
 git push --set-upstream origin master

Error refusing to merge
C:\Development\Angular2\APM [master ]> git pull
fatal: refusing to merge unrelated histories
C:\Development\Angular2\APM [master ]> git pull origin --allow-unrelated-histories

Remove all files in the local repository

git rm -r *

we also need to run the following command
git commit -m 'Delete all files '

Forced Push
git push origin master --force

Add users
to add users to  our repository Go to admin page ( ) and in the Collaborators tab you can add as many as you want ( since the free ones are public repos)

Get Specific commit
  • We need to clone the whole project 
 git clone


Check out specific commit
 git checkout 7191432c1d1e56439e224f2233009fad15b047e7
  • Push the changes to production . We need to checkout the main master
git checkout master


1.Click on commits in github
2.Select browse code on the right side of each commit
3.Click on download zip , which will download source code at that point of time of commit

Workflow for a commit. When a new commit is made new reference to a commit is made and the master along with the head moves.


  • By default git creates a branch known as master, when we create a new project. 
  • Branches is a reference to a commit
  • Head would have the reference to the current branch. 
  • Git normally puts branches under a directory known as refs and sub directory called as heads

 sub directory has all the branches. In this example we have 2 branches known as dev  and master

Create specific branch in git
$ git checkout -b [name_of_your_new_branch]

List of branches
PS C:\Development\Wakegenie\webapp\src> git branch
* dev

List of remote branches
PS C:\Development\Wakegenie\webapp\src> git branch -r

Delete local branch

git branch -d feature/customerGroups_S

Clone a specific branch
 git clone -b feature/end

Hard reset to previous commit
git reset --hard HEAD~1

Reset specific head or commit 
git reset --hard 573cb6b

After reset we need a forced push
 git push --force
Note: This would force the branch to the specified head in the repository


We need to checkout the branch which we need to merge . Ex we need to merge dev with branch

PS C:\Development\NodeProjects\vanilanode> git checkout master
Switched to branch 'master'
Your branch is up-to-date with 'origin/master'.
PS C:\Development\NodeProjects\vanilanode> git branch
* master
PS C:\Development\NodeProjects\vanilanode> git merge dev
Updating bb8135a..64f3636
 basics/JSONStringify.js | 12 ++++++++++++
 basics/splitJoin.js     | 47 +++++++++++++++++++++++++++++++----------------
 2 files changed, 43 insertions(+), 16 deletions(-)
 create mode 100644 basics/JSONStringify.js
PS C:\Development\NodeProjects\vanilanode>

Merge Tool

We can configure merge tool for resolving conflicts.
  • One of the recommended merge tool is meld you can find the installation at 
  • After installing we need to configure git to use meld This should be done in gitconfig file you can find it under C:\Users\Prath (Users directory) 
  •  In the config file we need to add
[difftool "meld"]
   path=c:/Program Files (x86)/Meld/meld/Meld.exe

Merge from dev to master

(on branch development)$ git merge master
(resolve any merge conflicts if there are any)
git checkout master
git merge development (there won't be any conflicts now)
Yum installation

Upgrade or install git using yum in cloudera installation
//Go to the root folder 
su -i
//Install or upgrade git using yum
# yum install git

Find the source repository

git config --get remote.origin.url