What is Git?

Title Text

which means...

(almost) everything is local

everything is fast

every clone is a backup

work offline

How Git works?

Snapshot 1

Commit

Takes snapshot of your current work state

Snapshot 1

Commit

Commit

Snapshot 2

Snapshot 1 

Commit 1

cd3 - sha

Snapshot 2 

Commit 2

bc9 - sha

Uniquely Identify a work state

Install Git

  
  $ sudo apt-get install git

Boom! You are ready to go

git init

  • Initializes a git repository at current directory
  • Starting point of git history and version control
  • Creates and fills .git folder

Next Steps

Set config

Set name


   $ git config --global user.name "Ankur Garg"

Set email


  $ git config --global core.editor "vim"

Set editor


   $ git config --global user.email "ankurgarg101@gmail.com"

Check config


  $ git config --global --edit

Check status

 
  $ git status

git add

Add a file to staging area


   $ git add .

Adds all file in repo to staging area


  $ git add filename

Adds mentioned file to staging area

The path to commit

git commit

Creates a snapshot of staged files


  $ git commit -m 'Describe what you changed here, this is called commit message'

Amend commit

Change the previous commit description or content if changes are not pushed


  $ git commit --amend

git diff

Shows modifications made with respect to previous snapshot/commit


   $ git diff

git log

Shows summary of previous snapshots


  $ git log

HEAD

HEAD refers to the current snapshot/commit

So HEAD is a pointer that points to snapshot we are at in repository 

HEAD^

Refers to previous commit 

HEAD~n

n is an integer and this refers to  nth last commit

$ git log

commit 19f2a02d7caa83824dd774133e301a516cd0cd24
Author: Ankur Garg <ankurgarg101@gmail.com>
Date:   Wed Jan 28 14:16:40 2015 +0530

    Edits config

commit 51cd5f0a7cd9183a7b56958b1e400b495f103ffc
Author: Ankur Garg <ankurgarg101@gmail.com>
Date:   Wed Jan 28 14:16:15 2015 +0530

    Add main helper

commit 00d06580e52a54376c68440fbbfce5e09e35e81c
Author: Ankur Garg <ankurgarg101@gmail.com>
Date:   Wed Jan 28 13:47:33 2015 +0530

    Add index.md

Edits config - HEAD

Add main helper - HEAD^

Add index.md - HEAD~2

git reset

Dangerous!


  $ git reset --mixed HEAD

Restores current state of project to a desired state

Unstage files from index and reset pointer to HEAD


  $ git reset --soft HEAD

Moves HEAD to specified commit reference, index and staging are untouched


 $ git reset --hard HEAD

Removes all changes and reset pointer to HEAD

git reset

$ git status -s
 M README
 M hello.rb
$ git add .
$ git status -s
M  README
M  hello.rb
$ git reset HEAD -- hello.rb 
Unstaged changes after reset:
M hello.rb
$ git status -s
M  README
 M hello.rb

git reset --soft

$ git status -s
M  hello.rb
$ git commit -am 'hello with a flower'
[master 5857ac1] hello with a flower
 1 files changed, 3 insertions(+), 1 deletions(-)
$ git status
# On branch master
nothing to commit (working directory clean)
$ git reset --soft HEAD~
$ git status -s
M  hello.rb

git reset --hard

$ git status
# On branch master
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
# modified:   README
#
# Changes not staged for commit:
#   (use "git add <file>..." to update what will be committed)
#   (use "git checkout -- <file>..." to discard changes in working directory)
#
# modified:   README
#
$ git reset --hard HEAD
HEAD is now at 5857ac1 hello with a flower
$ git status
# On branch master
nothing to commit (working directory clean)

git branch

Shows the list of branches

master is the default branch in a new repo

git branch branchname

Creates a branch with specified branchname

git checkout branchname

Shifts to specified branch

git checkout -b branchname

Creates and checkouts a branch

git merge

Merges two branches into each other

git rebase

Forwards commits of a branch to head?

$ git checkout master
$ git rebase branchname

Suppose there were 3 commits originally A, B and C:

Then developer Santa created commit D, and developer Banta created commit E:

Obviously, this conflict should be resolved somehow. For this, there are 2 ways:

Merge

Rebase

We create commit R, in which actual file content is identical to that of merge commit M above. But, we get rid of commit E, like it never existed. So simply HEAD is moved forward to include E in R

When do you need branches?

  • Try out an idea

  • Issue fixes

  • Adding a feature

  • Collaborating with others

Remotes

These are copies of a repo somewhere on internet

Remotes

Default remote is named as origin by git

git clone

Used to get a local copy of repository from a remote

$ git clone git@github.com:sdsmdg/cognizance.git

$ git clone http://github.com/sdsmdg/cognizance.git

Can use various protocols like SSH, HTTP to clone

git fetch

Gets latest commits from a remote

(Default to origin)

$ git fetch

$ git fetch remote_name

Can use various protocols like SSH, HTTP to clone

git pull

Get latest commits from a remote and merge them into current branch

(Defaults to origin)

$ git pull
Updating 853a38b..30eb5d8
Fast-forward
 config/config.php | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

$ git pull remote_name

git push

Push your commits to a remote. Commits must be in straight line from the latest commit at server

(Defaults to origin and current branch)

$ git push
Counting objects: 25, done.
Delta compression using up to 2 threads.
Compressing objects: 100% (25/25), done.
Writing objects: 100% (25/25), 2.43 KiB, done.
Total 25 (delta 4), reused 0 (delta 0)
To git@github.com:ankurgarg101/muzi-android.git
 * master -> master

$ git push remote_name branch_name

push rejections

Sometimes when your local repo is not in sync with remote, remote will reject your push. So always pull before push and solve any merge conflicts

$ git push origin master

To git@git.sdslabs.co.in:muzi.git
 ! [rejected]        origin/master -> origin/master (non-fast-forward)
error: failed to push some refs to 'git@git.sdslabs.co.in:muzi.git'
hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. Merge the remote changes (e.g. 'git pull')
hint: before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

$ git pull
Updating 853a38b..30eb5d8
Fast-forward
 config/config.php | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

$ git push

Updated origin/master -> origin/master

git remote

Used to handle various remotes

$ git remote -v
$ git remote add remotename some_url_here

List all remotes

Adds a remote

$ git clone git@git.sdslabs.co.in:muzi
Cloning into 'muzi'...
remote: Counting objects: 10058, done.
remote: Compressing objects: 100% (4088/4088), done.
remote: Total 10058 (delta 6184), reused 9482 (delta 5739)
Receiving objects: 100% (10058/10058), 28.49 MiB | 3.04 MiB/s, done.
Resolving deltas: 100% (6184/6184), done.
Checking connectivity... done.

$ ls
muzi

$ cd muzi

$ ls
admin  ajax  api.php  chrome  config  css  docs  favicon.ico  Gruntfile.js  images  js  layout.html  node_modules  
package.json readme.mkd requests.php  schema  social  stats  swf  tests  vendor  widget  zune

$ git remote -v
origin	git@git.sdslabs.co.in:muzi (fetch)
origin	git@git.sdslabs.co.in:muzi (push)

$ git remote add gh git@github.com:apsdehal/muzi.git

$ git remote -v

gh	git@github.com:apsdehal/muzi.git (fetch)
gh	git@github.com:apsdehal/muzi.git (push)
origin	git@git.sdslabs.co.in:muzi (fetch)
origin	git@git.sdslabs.co.in:muzi (push)

Resources

git init

 

git clone

 

git add

 

git commit

 

git branch

 

git checkout

 

git remote

git merge

 

git rebase

 

git fetch

 

git pull

 

git log

 

git diff

 

git push

14

Git lecture

By Ankur Garg

Git lecture

  • 1,047