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

git pull origin DP-2941(Branch name)
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)
Handling merge conflicts
One of the best features of git is its ability to easily merge multiple changes by different people.
Say you and a friend have both made changes to the same file at the same time. When you pull your friend’s changes, git will often be able to combine them without any problem.
Sometimes, though, after you do
$ git pull myfriend master
You’ll get a message like
CONFLICT (content): Merge conflict in
Automatic merge failed; fix conflicts and then commit the result.
If you open the offending file in a text editor, you’ll find an indication of the bits that are different, something like this:
<<<<<<< HEAD
A line in my file.
A line in my friend's file
>>>>>>> 031389f2cd2acde08e32f0beb084b2f7c3257fff
Edit the bits from <<<<<<< to >>>>>>>, to make the file just as you want it.
Then do git addgit commit, and git push.

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

Switch to an existing branch
git checkout SignalDP-2941

Delete a local branch
git branch -D feature/batch

Resolve pre-receive hook declined issue
pre-receive hook declined was as a result of the big file. Basically validating the push. To resolve it, I removed the last commit using:
 git reset --soft HEAD~1

Remove temp and unnecessary files
git gc

Discard local changes
At this point there are 3 options to undo the local changes you have:
  • Discard all local changes, but save them for possible re-use later:
    git stash
  • Discarding local changes (permanently) to a file:
    git checkout -- <file>
  • Discard all local changes to all files permanently:
    git reset --hard
Before executing git reset --hard, keep in mind that there is also a way to just temporary store the changes without committing them using git stash. This command resets the changes to all files, but it also saves them in case you would like to apply them at some later time.
Entirely replace master branch in git

here seotweaks is the master

git checkout seotweaks
git merge -s ours master
git checkout master
git merge seotweaks