Introduction to GIT






Hope you didn't walk into the wrong room ?!

I'm Vishnu.

I work @ HappiestMinds.

Version Control

What is "version control", and why should you care? Version control is a system that records changes to a file or set of files over time so that you can recall specific versions later.

Why Git?

Its a distributed Version control System

You can have it running on both your local Machine even without a server.
Or have a fully distributed cloud repository where many developers can collaborate.

Small Footprint

It is one of the first version Control that was built during the Internet ERA.
Unlike other VCS it can run on very resource constrained environment.

Simple

Meaning its simple to get started, But there is a massive battery of material you can learn.
But even after 5 years of using git once in few months there is still something new that I learn.

Powerful

Even though Its simple to get started.
As we go through the session you would understand how much you could do with git.

Setting up Git

Download Git


$ git --version
git version 2.9.2
                    

Using Git

GUI ?

I'll show you some of those tools. But !!

Git is a command line tool!!

Git command line is a first class citizen.
And the GUI's just leverages the command line tools.

Create a Repository


$ git init learnGit
Initialized empty Git repository in /home/vishnu/learnGit/.git/

$ tree .git
.git
├── branches
├── config
├── description
├── HEAD
├── hooks    // Lifecycle Management Instructions
│   ├── applypatch-msg.sample
│   ├── commit-msg.sample
│   ├── post-update.sample
│   ├── pre-applypatch.sample
│   ├── pre-commit.sample
│   ├── prepare-commit-msg.sample
│   ├── pre-push.sample
│   ├── pre-rebase.sample
│   └── update.sample
├── info
│   └── exclude
├── objects
│   ├── info
│   └── pack
└── refs
    ├── heads
    └── tags
                    

$ git status
On branch master

Initial commit

nothing to commit (create/copy files and use "git add" to track)
                    

$ echo "This is a sample Repo" >> README.MD

$ git status
On branch master

Initial commit

Untracked files:
  (use "git add ..." to include in what will be committed)

	README.MD

nothing added to commit but untracked files present (use "git add" to track)
                        

Git Tracks Content !

Each Time you type git status or refresh a GUI.
It is in fact examining the current working directory and all the working directories beneath it to see if any files have been modified or it has never seen it before.

Making your first commit


$ git add README.MD

$ git status
On branch master

Initial commit

Changes to be committed:
  (use "git rm --cached ..." to unstage)

	new file:   README.MD
                        

Commiting the first Change


$ git commit -m "My First Commit"
[master (root-commit) dc6f057] My First Commit
 1 file changed, 1 insertion(+)
 create mode 100644 README.MD

$ git status
On branch master
nothing to commit, working tree clean
                    

Making more changes


$ echo "Well Lets add few more lines to the file 1" >> README.MD
$ echo "Well Lets add few more lines to the file 2" >> README.MD
$ echo "Well Lets add few more lines to the file 3" >> README.MD
$ echo "Well Lets add few more lines to the file 4" >> README.MD
$ echo "Well Lets add few more lines to the file 5" >> README.MD

$ git status
On branch master
Changes not staged for commit:
  (use "git add ..." to update what will be committed)
  (use "git checkout -- ..." to discard changes in working directory)

	modified:   README.MD

no changes added to commit (use "git add" and/or "git commit -a")
                            

$ git diff
diff --git a/README.MD b/README.MD
index fec51a2..73dcf5e 100644
--- a/README.MD
+++ b/README.MD
@@ -1 +1,6 @@
 This is a sample Repo
+Well Lets add few more lines to the file 1
+Well Lets add few more lines to the file 2
+Well Lets add few more lines to the file 3
+Well Lets add few more lines to the file 4
+Well Lets add few more lines to the file 5
(END)
                    

$ cat > sample.txt
Hey this is a sample Text File

                    

$ git status
On branch master
Changes not staged for commit:
  (use "git add ..." to update what will be committed)
  (use "git checkout -- ..." to discard changes in working directory)

	modified:   README.MD

Untracked files:
  (use "git add ..." to include in what will be committed)

	sample.txt

no changes added to commit (use "git add" and/or "git commit -a")
                                

$ git diff
diff --git a/README.MD b/README.MD
index fec51a2..73dcf5e 100644
--- a/README.MD
+++ b/README.MD
@@ -1 +1,6 @@
 This is a sample Repo
+Well Lets add few more lines to the file 1
+Well Lets add few more lines to the file 2
+Well Lets add few more lines to the file 3
+Well Lets add few more lines to the file 4
+Well Lets add few more lines to the file 5
(END)
                    

Git WorkFlow

Ok now How do I share the code?

Git Remote !!

Remote

A remote URL is Git's fancy way of saying "the place where your code is stored." That URL could be your repository on GitHub, or another user's fork, or even on a completely different server. You can only push to two types of URL addresses:

  1. An HTTPS URL like https://github.com/user/repo.git
  2. An SSH URL, like git@github.com:user/repo.git


$ git remote add origin git@github.com:cygen/learnGit.git

$ git push origin master
Counting objects: 13, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (9/9), done.
Writing objects: 100% (13/13), 1.04 KiB | 0 bytes/s, done.
Total 13 (delta 2), reused 0 (delta 0)
remote: Resolving deltas: 100% (2/2), done.
To git@github.com:cygen/learnGit.git
 * [new branch]      master -> master

Lets add a new line from the github UI to the README File

Make some modifications locally


$ echo "Well Lets add few more lines to the file 5" >> README.MD
$ git add .
$ git commit -m "Adding line 5"

$ git push origin master

$ git push origin master
To git@github.com:cygen/learnGit.git
 ! [rejected]        master -> master (fetch first)
error: failed to push some refs to 'git@github.com:cygen/learnGit.git'
hint: Updates were rejected because the remote contains work that you do
hint: not have locally. This is usually caused by another repository pushing
hint: to the same ref. You may want to first integrate the remote changes
hint: (e.g., 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

$ git pull origin master
remote: Counting objects: 3, done.
remote: Compressing objects: 100% (2/2), done.
remote: Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (3/3), done.
From github.com:cygen/learnGit
 * branch            master     -> FETCH_HEAD
   4c38046..649a41f  master     -> origin/master
Auto-merging README.MD
CONFLICT (content): Merge conflict in README.MD
Automatic merge failed; fix conflicts and then commit the result.

$ cat README.MD                                                                                               1 ↵
This is a sample Repo
Well Lets add few more lines to the file 343
<<<<<<< HEAD
Well lets add few more lines to the file 5
=======
Well Lets add few more lines to the file 4
>>>>>>> 649a41f2714efbb121981b0f5d6fcbc407d8332f

$ cat README.MD                                                                                               1 ↵
This is a sample Repo
Well Lets add few more lines to the file 343
Well Lets add few more lines to the file 4
Well lets add few more lines to the file 5

$ git add .

$ git commit -m "Merged 649a41"
[master 539c1fb] Merged 649a41

$ git push origin master
Counting objects: 6, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (4/4), done.
Writing objects: 100% (6/6), 544 bytes | 0 bytes/s, done.
Total 6 (delta 2), reused 0 (delta 0)
remote: Resolving deltas: 100% (2/2), completed with 1 local objects.
To git@github.com:cygen/learnGit.git
   649a41f..539c1fb  master -> master

Git Network Graph

Basic Branching and Merging

Branch

A branch in Git is simply a lightweight movable pointer to one of these commits. The default branch name in Git is master. As you initially make commits, you're given a master branch that points to the last commit you made. Every time you commit, it moves forward automatically.

Creating and checking out a new branch


$ git branch
  issue2
* master

$ git branch issue2

$ git Branch
  issue2
* master

$ git checkout issue2
Switched to branch 'issue2'

$ git Branch
* issue2
  master

Making some changes to the repo


$ cat README.MD
This is a sample Repo

Well Lets add few more lines to the file 343
Well Lets add few more lines to the file 4
Well lets add few more lines to the file 5


These changes are from issue2

Commiting and push to a new branch


$ git add .

$ git commit -m "added new line from issue2"
[issue2 7b7b53e] added new line from issue2
 1 file changed, 4 insertions(+)

$ git push origin issue2
Counting objects: 3, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 310 bytes | 0 bytes/s, done.
Total 3 (delta 1), reused 0 (delta 0)
remote: Resolving deltas: 100% (1/1), completed with 1 local objects.
To git@github.com:cygen/learnGit.git
 * [new branch]      issue2 -> issue2

Merging the changes


$ git checkout master
Switched to branch 'master'

$ git merge issue2
Updating 539c1fb..7b7b53e
Fast-forward
 README.MD | 4 ++++
 1 file changed, 4 insertions(+)

$ git log
commit 7b7b53ec0f8f3c459271aed7fa09c669bf67f879
Author: vishnu667 
Date:   Wed Dec 14 13:00:56 2016 +0530

    added new line from issue2

commit 539c1fbe4144b49ddcee765ce10f39d81e17c5a9
Merge: ec9ef1e 649a41f
Author: vishnu667 
Date:   Wed Dec 14 03:36:40 2016 +0530

    Merged 649a41

commit ec9ef1edecce243853e8a8644abb14a20dc4e018
Author: vishnu667 
Date:   Wed Dec 14 03:31:50 2016 +0530

    Adding line 5

commit 649a41f2714efbb121981b0f5d6fcbc407d8332f
Author: Vishnu Prasad S 
Date:   Wed Dec 14 03:25:22 2016 +0530

    Update README.MD

Making more changes on master


$ cat README.MD
This is a sample Repo

For you to practice git

Well Lets add few more lines to the file 343
Well Lets add few more lines to the file 4
Well lets add few more lines to the file 5


These changes are from issue24

$ git add .
$ git commit -m "Adding Description"

$ git log --pretty=oneline --abbrev-commit
46ef342 Added Description
7b7b53e added new line from issue2
539c1fb Merged 649a41
ec9ef1e Adding line 5
649a41f Update README.MD
4c38046 Reverted Back commit
85d0247 changed line 3
23c06c1 new commit
dc6f057 My First Commit

Before Pushing


After Pushing

Social Coding ...

Forking

A fork is simply a copy of the repository

Fork by itself is not a part of Git but services built over git provides them

To fork a repo simply click on the

My Forked copy of the repo



$ git remote
origin

$ git remote add mine git@github.com:vishnu667/learnGit.git

$ git remote
mine
origin

$ git fetch mine
From github.com:vishnu667/learnGit
 * [new branch]      issue2     -> mine/issue2
 * [new branch]      master     -> mine/master

$ git branch
  issue2
* master

$ git branch -a
  issue2
* master
  remotes/mine/issue2
  remotes/mine/master
  remotes/origin/issue2
  remotes/origin/master

$ git checkout -b styling

$ cat README.MD
## This is a sample Repo

For you to practice git

* Well Lets add few more lines to the file 343
* Well Lets add few more lines to the file 4
* Well lets add few more lines to the file 5


####These changes are from issue24

$ git commit -m "Changing some styles"

$ git push mine styling
Counting objects: 3, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 297 bytes | 0 bytes/s, done.
Total 3 (delta 1), reused 0 (delta 0)
remote: Resolving deltas: 100% (1/1), completed with 1 local objects.
To git@github.com:vishnu667/learnGit.git
 * [new branch]      styling -> styling

Thank you