SlideShare a Scribd company logo
1 of 65
Download to read offline
Git in 90 Minutes
1By Vimukthi Randika
What’s GIT
2
Open source Version
Control System
Designed and developed
by Linus Torvalds
for Linux kernel
GNU General Public
License v2 Initial release, 7
April 2005
Last sable release 1.9,
14 Feb 2014
Written in C, Bourne
Shell, Tcl, Perl
For OSs, Linux,
Windows, POSIX, OS X
Why GIT
3
Distributed
Full control over
local copy
Fast and can be
done offline
Not have a single
point of failure
Adjustable
workflows
Standalone
Collaboration
Backups
Why GIT
4
Branching
Frictionless context
switching
1/3 s vs 13 s
(GIT vs SVN)
Enable parallel
work
Easy merging
Lightweight
Why GIT
5
Staging
Purified History Partial commits
Logically separate
change sets
Edit commits
Workflow
Remote Repo
Local
Master
Branch A
Branch B
Branch C
6
Initializing Repository
git init
git init <directory>
git init --bare <directory>
git clone <repo>
git clone <repo> <directory>
Remote Repo
Local Master
$ git clone
https://github.com/vimukthir/gitTutorial.git
project1
7
GIT Branch Management
Master branch keep untouched
To maintain stability
To build without error
8
Need for Branching ?
Branch for each feature
Bug fixing
R&D
Formulate workflow
Centralized
Feature Branch
Git flow
Forking
GIT Branch Management
git branch
git branch <branch>
git branch -d <branch>
git branch -D <branch>
git branch -m <branch>
Create branches
git checkout <existing-branch>
Switch branches
Branch A
Branch B
Branch C
Local
Master
$ git branch BranchA
$ git checkout BranchA
-------------------------
$ git checkout –b BranchA
9
GIT Branch Management
98ca9 34ac3 f30ab
master
feature
HEAD
$ git branch feature
10
GIT Branch Management
98ca9 34ac3 f30ab
master
feature
HEAD
98ca9 34ac3 f30ab
master
HEAD
ws4y
feature
$ git checkout feature $ git commit -a -m 'made a change’
11
GIT Staging Area
12
File Status Life cycle
Untracked
Tracked &
Unmodified
Modified
Staged
Committed
Add file
Remove file
Edit file
Stage fileUn-stage file
Revert edit file
Revert commit
Commit file
Commit file
Garbage
New file
Revert commit
Send to Garbage
Revert changes
13
Add file
File Status Life cycle
Untracked
Tracked &
Unmodified
Modified
Staged
Committed
git add <file>
git reset <file>
Garbage
New file
git add <file>
git add <directory>
git add –p
git reset <file>
$ git add Test.java
$ git reset Test.java
14
File Status Life cycle
Untracked
Tracked &
Unmodified
Modified
Staged
Committed
Edit file
git checkout <file>
Garbage
git checkout <file>
# Doing some modifications to Test.java and
# you need to revert all the changes
$ git checkout Test.java
15
File Status Life cycle
Untracked
Tracked &
Unmodified
Modified
Staged
Committed
git add <file>git reset <file>
git reset
Garbage
git reset --hard git add -p
git reset <file> git add -i
git reset git add <file>
# Test.java is already tracked file and we
# need to add it to stage area after Doing
# some modifications.
$ git add Test.java
# Now you want to remove it from stage area
$ git reset Test.java
# Or we want to throw all the changes in the
# stage area to the garbage
$ git reset --hard
git reset --hard
16
File Status Life cycle
Untracked
Tracked &
Unmodified
Modified
Staged
Committed
git commit -m <message>
Garbage
# You can commit the Test.java either from
stage area or un-stage area.
$ git commit Test.java –m “Adding Test file”
git commit
git commit -m "<message>"
git commit –a
git commit --amend
git commit --amend --no-edit
17
git commit
git commit <file> -m <message>
git commit -a
File Status Life cycle
Untracked
Tracked &
Unmodified
Modified
Staged
Committed
Garbage
# Reset stage area and keep workspace untouched
$ git reset HEAD~1
# Reset both stage area and workspace
$ git reset –-hard HEAD~1
git reset HEAD~1
git reset --hard HEAD~1
git revert HEAD
# Revert both stage
# area and workspace
$ git revert HEAD~1
18
File Status Life cycle
Untracked
Tracked &
Unmodified
Modified
Staged
Committed
Garbage
New file
git clean -f
# Delete changes which haven’t added to
stage area
$ git clean -f
19
Reset vs Revert
commit
BP
H
BP
H
NC
reset
BP
H
revert
Y
BP
H
Y
20
Git Status
21
Untracked
Tracked &
Modified Staged
Git Status
(brnchA ) $ git status
# on branch branchA
# changes to be committed :
# (use “git reset HEAD <file>…” to unstage)
#
# modified : Test1.java
# deleted : Test2.java
# new file : 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 : Test3.java
#
# untracked files :
# (use “git add <file>…” to include in what will be committed)
#
# spring.properties
22
Merging
23
Merging mechanisms
Fast forward merging
Tree way merging
Rebase
Merging Mechanisms: Fast-forward
98ca9 34ac3 f30ab
master
d45th
branchA
d45th98ca9 34ac3 f30ab
master
branchA
$ git checkout master
$ git merge branchA
Updating f30ab ..d45th
Fast-forward
Test.java | -
1 file changed, 1, deletion(-)
git merge <branch>
git merge --no-ff <branch>
24
Merging Mechanisms: 3 way
98ca9 34ac3 f30ab
master
d45th
branchA
sd235 1w34t
d45th
98ca9 34ac3 f30ab
master
branchA
sd235 1w34t
rt12u
$ git checkout master
$ git merge branchA
Auto-merging Test.java Merge made by the
‘recursive’ strategy
Test.jsva | 1 +
1 file changed, 1 insertion(+)
25
Merging Mechanisms : Rebase
98ca9 34ac3 f30ab
master
d45th
branchA
sd235
98ca9 34ac3 f30ab
master
d45th
sd235
branchA
sd235
$ git checkout branchA
$ git rebase master
First, rewinding head to replay your work
on top of it... Fast-forwarded branch2 to
master.
26
Merging Scenarios: Cherry Pick
98ca9 34ac3 f30ab
master
d45th
branchA
sd235 1w34t
$ git checkout master
$ git cherry-pick sd235
27
98ca9 34ac3 f30ab
master
d45th
branchA
sd235 1w34t
rt56h (sd235)
$ git cherry-pick sd235..1w34t
Git Commands - remote
git remote
git remote –v
git remote add <name> <url>
git remote rm <name>
git remote rename <old-name> <new-name>
# Adding remote connection to the specified address. This connection
can be reffered as “john”
$ git init
$ git remote add john http://dev.example.com/john.git
$ git clone john
28
Git Commands - fetch
git fetch <remote>
git fetch <remote> <branch>
$ git branch -r
# origin/master
# origin/develop
# origin/some-feature
$ git fetch origin
a1e8fb5..45e66a4 master -> origin/master
a1e8fb5..9e8ab1c develop -> origin/develop
* [new branch] some-feature -> origin/some-feature
$ git checkout master
$ git merge origin/master
29
Git Commands - pull
git pull <remote> (git fetch + git merge)
git pull --rebase <remote> (git fetch + git rebase)
$ git checkout master
$ git pull --rebase origin
30
Git Commands - push
git push <remote> <branch>
git push <remote> --force
git push <remote> --all
git push <remote> --tags
$ git checkout master
$ git fetch origin master
$ git rebase -i origin/master
# Squash commits, fix up commit messages etc.
$ git push origin master
31
Always push to bear repositories
Bear repositories can be initialize with “git init -- bear” command
What special with bear repo is it doesn’t have workspace
Work Flow Summary
Workspace Staging Area Local Repo Remote Repoinit
clone
pull
fetch
add
commit
push
commit
reset
reset
revert
reset
initialize
update rebase
merge
changes
revert
dcommit
32
Workflow in Command line
$ git clone https://github.com/vimukthir/gitTutorial.git
project1
$ cd project1
$ git branch
$ *master
$ git branch –b feature1
$ git branch
$ *mater
$ feature1
$ git checkout feature1
$ git branch
$ master
$ *feature1
33
Workflow in Command line
$ git status
on branch branchA
#(use “git add <file>…” to update what will be committed)
#(use “git -- checkout <file>” to discard changes in working directory)
#
# modified : Test.java
$ git add Test.java
S git status
# on branch branchA
# changes to be committed :
# (use “git reset HEAD <file>…” to unstage)
# new file : Test.java
$ git commit Test.java
34
Workflow in Command line
$ git checkout master
S git merge branchA
Auto merging Test.java
CONFLICT (content): Merge conflict in Test.java
Automatic merge failed; fix conflicts and then commit the result.
<<<<<<<< HEAD
public void doTest() {
==============
public void test() {
>>>>>>>> branchA
$ git add Test.java
$ git commit Test.java –m “Resolving conflicts”
$ git rebase origin master
$ git push
35
Git Commands - log
git log
git log -n <limit>
git log –oneline
git log –stat
git log –p
git log --author="<pattern>“
git log --grep="<pattern>“
git log <since>..<until>
git log <file>
git log --graph --decorate --oneline
Workspace Staging
Area
Local Repo
Git log
36
Git diff --cached
Git diff --staged
Git status
Git diff HEAD
Git Commands - config
git config --global user.name <name>
git config --global user.email <email>
git config --global alias.<alias-name> <git-command>
Changing local repo configuration
# Add some SVN-like aliases
git config --global alias.st status
git config --global alias.co checkout
git config --global alias.br branch
git config --global alias.up rebase
git config --global alias.ci commit
37
Git Commands -Interactive Staging
38
Git Platform
Interactive Staging
status update revert patch diff add
Git Commands - Interactive Staging
Untracked
Tracked &
Unmodified
$ git add -i
staged unstaged path
1: unchanged +0/-1 Test.java
2: unchanged +1/-1 Test1.java
*** Commands ***
1: status 2: update 3: revert 4: add untracked
5: patch 6: diff 7: quit 8: help
What now>
39
What now> 2
staged unstaged path
1: unchanged +0/-1 Test.java
2: unchanged +1/-1 Test1.java
Update>>
Update>> 2
staged unstaged path
1: unchanged +0/-1 Test.java
* 2: unchanged +1/-1 Test1.java
Update>>
Git Commands -Interactive Patch
40
Workspace
1 2
3 4
2
4 4
Staging Area
Git Commands -Interactive Patch
Untracked
Tracked &
Unmodified
$ git add -p
diff –git Test.java
Index d26b0bd—93a2695 100644
--- a/Test.java
+++ b/Test.java
@@-8.7 +8,7
package Test;
public class Test {
+ public void method1() {
+ }
+ public void method2() {
+ }
}
Stage this hunk [y,n,q,a,d,/,e,?]? 41
Git Commands - stash
git stash
git stash apply
git stash apply {stash_id}
git stash list
git stash branch stashBranch
$ git status
# On branch master
# Changes to be committed:
# (use "git reset HEAD <file>..." to unstage)
#
# modified: Test.java
#
# Changes not staged for commit:
# (use "git add <file>..." to update what will be committed)
#
# modified: Test1.java
42
Git Commands - log
$
$ git stash
Saved working directory and index state 
"WIP on master: 049d078 added the Test.java"
HEAD is now at 049d078 added the Test.java
(To restore them type "git stash apply")
$ git status
# On branch master
nothing to commit (working directory clean)
$ git stash list
stash@{0}: WIP on master: 049d078 added the Test.java
$ git stash apply
# On branch master
# Changes not staged for commit:
# (use "git add <file>..." to update what will be committed)
#
# modified: Test.java
# modified: Test1.java
Git Commands - stash
43
Git Commands - stash
44
Workspace
Staging Area
Stash
BranchA
git stash
git apply
Workspace
Staging Area
StashBranch
git apply <StashBranch>
Git Commands - web
git instaweb
Web support
45
Commands Comparison (Git/SVN)
46
Git SVN
git clone <repo> svn checkout url
git pull svn update
git init
git add .
git commit
svnadmin create repo
svn import file://repo
git diff rev path svn diff -rrev path
git apply patch -p0
git status svn status
git checkout path svn revert path
git add file
git rm file
git mv file
svn add file
svn rm file
svn mv file
Commands Comparison (Git/SVN)
47
Git SVN
git commit -a svn commit
git branch branch
git checkout branch
svn copy <source> <destination>
svn switch <destination>
git branch svn list http://example.com/svn/branches/
git merge branch svn merge -r R1:R2 <source>
Git Workflows
48
Forking
Git flow
Feature Branch
Centralized
Github repo
Git remote repository
49
https://github.com
Git Architecture
50
.git
branches
hooks
info
logs
objects
refs
COMMIT_EDITMSG
config
description
HEAD
index
ORIG_HEAD
Contains a set of shell scripts which executes after user
commands
Store blob, Tree, commit & tag
Store heads, remotes & tags
Keep track of current branch in workspace
Keep track of staged changes
Store user defined configurations
Git Architecture
Git Objects
51
Object
Tree
Blob Commit
Tag
Git Architecture
How commit works
52
$ git commit Test.java Test1.java -m 'made a change’
blob
a906cb2a4a904a152e80877d4088654daad0c859
Test.java
blob
b456tb2a4a904a152e80877d4088654daad0c843
Test1.java
Path to Object = .git / a9 / 06cb2a4a904a152e80877d4088654daad0c859
Git Architecture
How commit works
53
blob
a906cb2a4a904a152e80877d4088654daad0c859
blob
b456tb2a4a904a152e80877d4088654daad0c843
tree
99f1a6d12cb4b6f19c8655fca46c3ecf317074e0
Git Architecture
How commit works
54
blob
a906cb2a4a904a152e80877d4088654daad0c859
blob
b456tb2a4a904a152e80877d4088654daad0c843
tree
99f1a6d12cb4b6f19c8655fca46c3ecf317074e0
commit
fdf4fc3344e67ab068f836878b6c4951e3b15f3d
Git Architecture
55
commitcommitcommit
fdf4fc3344e67ab068f836878b6c4951e3b15f3d
cac0cab538b970a37ea1e769cbbde608743bc96d
1a410efbd13591db07496601ebc7a059dd55cfe9
C C C
T
B B
T
B T
T
T T
HEAD
Git Architecture
56
$ git cat-file -p 99f1a6d12cb4b6f19c8655fca46c3ecf317074e0
100644 blob a906cb2a4a904a152e80877d4088654daad0c859 Test.java
100644 blob b456tb2a4a904a152e80877d4088654daad0c843 Test1.java
$ git cat-file -p a906cb2a4a904a152e80877d4088654daad0c859
public class Test {
String arg …
}
$ git cat-file -p fdf4fc3344e67ab068f836878b6c4951e3b15f3d
tree 99f1a6d12cb4b6f19c8655fca46c3ecf317074e0 author Vimukthi Randika
<vimukthi@gmail.com> 1243040974 -0700 committer Vimukthi Randika
<vimukthi@gmail.com > 1243040974 -0700 first commit
$ find .git/objects -type f
.git/objects/01/55eb4229851634a0f03eb265b69f5a2d56f341 # tree 2
.git/objects/1a/410efbd13591db07496601ebc7a059dd55cfe9 # commit 3
.git/objects/1f/7a7a472abf3dd9643fd615f6da379c4acb3e3a # test.txt v2
.git/objects/3c/4e9cd789d88d8d89c1073707c3585e41b0e614 # tree 3
.git/objects/83/baae61804e65cc73a7201a7252750c76066a30 # test.txt v1
.git/objects/95/85191f37f7b0fb9444f35a9bf50de191beadc2 # tag
.git/objects/ca/c0cab538b970a37ea1e769cbbde608743bc96d # commit 2
.git/objects/d6/70460b4b4aece5915caf5c68d12f560a9fe3e4 # 'test content'
.git/objects/d8/329fc1cc938780ffdd9f94e0d364e0ea74f579 # tree 1
.git/objects/fa/49b077972391ad58037050f2a75f74e3671e92 # new.txt
.git/objects/fd/f4fc3344e67ab068f836878b6c4951e3b15f3d # commit 1
$ git gc
Counting objects: 17, done.
Delta compression using 2 threads.
Compressing objects: 100% (13/13), done.
Writing objects: 100% (17/17), done.
Total 17 (delta 1), reused 10 (delta 0
Git Commands - pack
57
$ find .git/objects -type f
.git/objects/71/08f7ecb345ee9d0084193f147cdad4d2998293
.git/objects/d6/70460b4b4aece5915caf5c68d12f560a9fe3e4
.git/objects/info/packs
.git/objects/pack/pack-7a16e4488ae40c7d2bc56ea2bd43e25212a66c45.idx
.git/objects/pack/pack-7a16e4488ae40c7d2bc56ea2bd43e25212a66c45.pack
Git Commands - pack
58
O1 O2 O3 O4
commit flows
∆1 ∆2 ∆3 git gc
O4∆3∆2∆1
GIT SVN Integration
• Fedora
• Ubuntu
• Windows
Using cygwin and install GIT via its package manager
59
$ yum install git-svn
$ apt-get install git-svn
Installation
GIT SVN Integration
SVN Repo
Local
Master
Branch A
Branch B
Branch C
60
GIT SVN Integration
git svn clone <repo>
git svn clone <repo> <directory>
Download remote SVN repo to local machine
SVN Repo
Local Master
$ git svn clone
http://selene.aepona.com/internal/services/bdd-
testsuit/trunk bdd-testsuit
61
GIT SVN Integration
git svn rebase
git svn dcommit
Applying changes to SVN repo
SVN Repo
Local Master
$ git svn clone
http://selene.aepona.com/internal/services/bdd
- testsuit/trunk bdd-testsuit
# Making branches, adding, commiting, merging
$ git svn rebase
$ git svn dcommit
62
Q & A
63
References
64
http://git-scm.com/
https://www.atlassian.com/git
http://git-scm.com/book
Merging Mechanisms : Rebase
98ca9 34ac3 f30ab
master
d45th
$ git rebase -i HEAD~3
65
98ca9 1qw34
master
Renaming License folder
Moving Licenses different folder
Adding Licenses
origin/HEAD origin/master
Adding license to separate
folder
origin/HEAD origin/master

More Related Content

What's hot

Git - An Introduction
Git - An IntroductionGit - An Introduction
Git - An IntroductionBehzad Altaf
 
Git Tutorial For Beginners | What is Git and GitHub? | DevOps Tools | DevOps ...
Git Tutorial For Beginners | What is Git and GitHub? | DevOps Tools | DevOps ...Git Tutorial For Beginners | What is Git and GitHub? | DevOps Tools | DevOps ...
Git Tutorial For Beginners | What is Git and GitHub? | DevOps Tools | DevOps ...Simplilearn
 
Git flow Introduction
Git flow IntroductionGit flow Introduction
Git flow IntroductionDavid Paluy
 
Introduction to git hub
Introduction to git hubIntroduction to git hub
Introduction to git hubNaveen Pandey
 
The everyday developer's guide to version control with Git
The everyday developer's guide to version control with GitThe everyday developer's guide to version control with Git
The everyday developer's guide to version control with GitE Carter
 
Git basics to advance with diagrams
Git basics to advance with diagramsGit basics to advance with diagrams
Git basics to advance with diagramsDilum Navanjana
 
Learning git
Learning gitLearning git
Learning gitSid Anand
 
Treinamento git - Papos RBSDev
Treinamento git - Papos RBSDevTreinamento git - Papos RBSDev
Treinamento git - Papos RBSDevHélio Medeiros
 

What's hot (20)

Git - An Introduction
Git - An IntroductionGit - An Introduction
Git - An Introduction
 
Github
GithubGithub
Github
 
Aprendendo Git
Aprendendo GitAprendendo Git
Aprendendo Git
 
Introduction to Git
Introduction to GitIntroduction to Git
Introduction to Git
 
GitLab.pptx
GitLab.pptxGitLab.pptx
GitLab.pptx
 
Git Tutorial For Beginners | What is Git and GitHub? | DevOps Tools | DevOps ...
Git Tutorial For Beginners | What is Git and GitHub? | DevOps Tools | DevOps ...Git Tutorial For Beginners | What is Git and GitHub? | DevOps Tools | DevOps ...
Git Tutorial For Beginners | What is Git and GitHub? | DevOps Tools | DevOps ...
 
Git and git flow
Git and git flowGit and git flow
Git and git flow
 
Intro to Git and GitHub
Intro to Git and GitHubIntro to Git and GitHub
Intro to Git and GitHub
 
Git 101 for Beginners
Git 101 for Beginners Git 101 for Beginners
Git 101 for Beginners
 
Introducing GitLab
Introducing GitLabIntroducing GitLab
Introducing GitLab
 
CI/CD with Github Actions
CI/CD with Github ActionsCI/CD with Github Actions
CI/CD with Github Actions
 
Git l'essentiel
Git l'essentielGit l'essentiel
Git l'essentiel
 
Github
GithubGithub
Github
 
Git flow Introduction
Git flow IntroductionGit flow Introduction
Git flow Introduction
 
Introduction to git hub
Introduction to git hubIntroduction to git hub
Introduction to git hub
 
The everyday developer's guide to version control with Git
The everyday developer's guide to version control with GitThe everyday developer's guide to version control with Git
The everyday developer's guide to version control with Git
 
Git basics to advance with diagrams
Git basics to advance with diagramsGit basics to advance with diagrams
Git basics to advance with diagrams
 
Learning git
Learning gitLearning git
Learning git
 
Treinamento git - Papos RBSDev
Treinamento git - Papos RBSDevTreinamento git - Papos RBSDev
Treinamento git - Papos RBSDev
 
Git basics
Git basicsGit basics
Git basics
 

Viewers also liked

Having fun with Git
Having fun with GitHaving fun with Git
Having fun with GitAhmad Arif
 
Eclipse Community Survey Report 2013
Eclipse Community Survey Report 2013Eclipse Community Survey Report 2013
Eclipse Community Survey Report 2013Ian Skerrett
 
Why we ditched TFS and embraced Git, Github, TeamCity and Myget
Why we ditched TFS and embraced Git, Github, TeamCity and MygetWhy we ditched TFS and embraced Git, Github, TeamCity and Myget
Why we ditched TFS and embraced Git, Github, TeamCity and MygetDennis Doomen
 
Eclipse survey 2012 report [final]
Eclipse survey 2012 report [final]Eclipse survey 2012 report [final]
Eclipse survey 2012 report [final]Ian Skerrett
 
Git-flow workflow and pull-requests
Git-flow workflow and pull-requestsGit-flow workflow and pull-requests
Git-flow workflow and pull-requestsBartosz Kosarzycki
 
Git Ready! Workflows
Git Ready! WorkflowsGit Ready! Workflows
Git Ready! WorkflowsAtlassian
 

Viewers also liked (9)

Using Git
Using GitUsing Git
Using Git
 
Git Basic
Git BasicGit Basic
Git Basic
 
Git
GitGit
Git
 
Having fun with Git
Having fun with GitHaving fun with Git
Having fun with Git
 
Eclipse Community Survey Report 2013
Eclipse Community Survey Report 2013Eclipse Community Survey Report 2013
Eclipse Community Survey Report 2013
 
Why we ditched TFS and embraced Git, Github, TeamCity and Myget
Why we ditched TFS and embraced Git, Github, TeamCity and MygetWhy we ditched TFS and embraced Git, Github, TeamCity and Myget
Why we ditched TFS and embraced Git, Github, TeamCity and Myget
 
Eclipse survey 2012 report [final]
Eclipse survey 2012 report [final]Eclipse survey 2012 report [final]
Eclipse survey 2012 report [final]
 
Git-flow workflow and pull-requests
Git-flow workflow and pull-requestsGit-flow workflow and pull-requests
Git-flow workflow and pull-requests
 
Git Ready! Workflows
Git Ready! WorkflowsGit Ready! Workflows
Git Ready! Workflows
 

Similar to GIT_In_90_Minutes

Similar to GIT_In_90_Minutes (20)

Learning Basic GIT Cmd
Learning Basic GIT CmdLearning Basic GIT Cmd
Learning Basic GIT Cmd
 
Git Tricks — git utilities that make life git easier
Git Tricks — git utilities that make life git easierGit Tricks — git utilities that make life git easier
Git Tricks — git utilities that make life git easier
 
Git tutorial
Git tutorialGit tutorial
Git tutorial
 
Intro to Git DevOps Tally Presentation 101615
Intro to Git DevOps Tally Presentation 101615Intro to Git DevOps Tally Presentation 101615
Intro to Git DevOps Tally Presentation 101615
 
Introduction To Git Workshop
Introduction To Git WorkshopIntroduction To Git Workshop
Introduction To Git Workshop
 
Gittalk
GittalkGittalk
Gittalk
 
Git github
Git githubGit github
Git github
 
Git
GitGit
Git
 
Introducción a git y GitHub
Introducción a git y GitHubIntroducción a git y GitHub
Introducción a git y GitHub
 
Now i git it!!!
Now i git it!!!Now i git it!!!
Now i git it!!!
 
Pro git - grasping it conceptually
Pro git - grasping it conceptuallyPro git - grasping it conceptually
Pro git - grasping it conceptually
 
Git Concepts, Commands and Connectivity
Git Concepts, Commands and ConnectivityGit Concepts, Commands and Connectivity
Git Concepts, Commands and Connectivity
 
git - the basics
git - the basicsgit - the basics
git - the basics
 
Git
GitGit
Git
 
Git
GitGit
Git
 
Lets Git Together
Lets Git TogetherLets Git Together
Lets Git Together
 
Wokshop de Git
Wokshop de Git Wokshop de Git
Wokshop de Git
 
GIT Basics
GIT BasicsGIT Basics
GIT Basics
 
Get Good With Git
Get Good With GitGet Good With Git
Get Good With Git
 
Nge-GIT (Belajar Git Bareng)
Nge-GIT (Belajar Git Bareng)Nge-GIT (Belajar Git Bareng)
Nge-GIT (Belajar Git Bareng)
 

GIT_In_90_Minutes

  • 1. Git in 90 Minutes 1By Vimukthi Randika
  • 2. What’s GIT 2 Open source Version Control System Designed and developed by Linus Torvalds for Linux kernel GNU General Public License v2 Initial release, 7 April 2005 Last sable release 1.9, 14 Feb 2014 Written in C, Bourne Shell, Tcl, Perl For OSs, Linux, Windows, POSIX, OS X
  • 3. Why GIT 3 Distributed Full control over local copy Fast and can be done offline Not have a single point of failure Adjustable workflows Standalone Collaboration Backups
  • 4. Why GIT 4 Branching Frictionless context switching 1/3 s vs 13 s (GIT vs SVN) Enable parallel work Easy merging Lightweight
  • 5. Why GIT 5 Staging Purified History Partial commits Logically separate change sets Edit commits
  • 7. Initializing Repository git init git init <directory> git init --bare <directory> git clone <repo> git clone <repo> <directory> Remote Repo Local Master $ git clone https://github.com/vimukthir/gitTutorial.git project1 7
  • 8. GIT Branch Management Master branch keep untouched To maintain stability To build without error 8 Need for Branching ? Branch for each feature Bug fixing R&D Formulate workflow Centralized Feature Branch Git flow Forking
  • 9. GIT Branch Management git branch git branch <branch> git branch -d <branch> git branch -D <branch> git branch -m <branch> Create branches git checkout <existing-branch> Switch branches Branch A Branch B Branch C Local Master $ git branch BranchA $ git checkout BranchA ------------------------- $ git checkout –b BranchA 9
  • 10. GIT Branch Management 98ca9 34ac3 f30ab master feature HEAD $ git branch feature 10
  • 11. GIT Branch Management 98ca9 34ac3 f30ab master feature HEAD 98ca9 34ac3 f30ab master HEAD ws4y feature $ git checkout feature $ git commit -a -m 'made a change’ 11
  • 13. File Status Life cycle Untracked Tracked & Unmodified Modified Staged Committed Add file Remove file Edit file Stage fileUn-stage file Revert edit file Revert commit Commit file Commit file Garbage New file Revert commit Send to Garbage Revert changes 13 Add file
  • 14. File Status Life cycle Untracked Tracked & Unmodified Modified Staged Committed git add <file> git reset <file> Garbage New file git add <file> git add <directory> git add –p git reset <file> $ git add Test.java $ git reset Test.java 14
  • 15. File Status Life cycle Untracked Tracked & Unmodified Modified Staged Committed Edit file git checkout <file> Garbage git checkout <file> # Doing some modifications to Test.java and # you need to revert all the changes $ git checkout Test.java 15
  • 16. File Status Life cycle Untracked Tracked & Unmodified Modified Staged Committed git add <file>git reset <file> git reset Garbage git reset --hard git add -p git reset <file> git add -i git reset git add <file> # Test.java is already tracked file and we # need to add it to stage area after Doing # some modifications. $ git add Test.java # Now you want to remove it from stage area $ git reset Test.java # Or we want to throw all the changes in the # stage area to the garbage $ git reset --hard git reset --hard 16
  • 17. File Status Life cycle Untracked Tracked & Unmodified Modified Staged Committed git commit -m <message> Garbage # You can commit the Test.java either from stage area or un-stage area. $ git commit Test.java –m “Adding Test file” git commit git commit -m "<message>" git commit –a git commit --amend git commit --amend --no-edit 17 git commit git commit <file> -m <message> git commit -a
  • 18. File Status Life cycle Untracked Tracked & Unmodified Modified Staged Committed Garbage # Reset stage area and keep workspace untouched $ git reset HEAD~1 # Reset both stage area and workspace $ git reset –-hard HEAD~1 git reset HEAD~1 git reset --hard HEAD~1 git revert HEAD # Revert both stage # area and workspace $ git revert HEAD~1 18
  • 19. File Status Life cycle Untracked Tracked & Unmodified Modified Staged Committed Garbage New file git clean -f # Delete changes which haven’t added to stage area $ git clean -f 19
  • 22. Git Status (brnchA ) $ git status # on branch branchA # changes to be committed : # (use “git reset HEAD <file>…” to unstage) # # modified : Test1.java # deleted : Test2.java # new file : 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 : Test3.java # # untracked files : # (use “git add <file>…” to include in what will be committed) # # spring.properties 22
  • 23. Merging 23 Merging mechanisms Fast forward merging Tree way merging Rebase
  • 24. Merging Mechanisms: Fast-forward 98ca9 34ac3 f30ab master d45th branchA d45th98ca9 34ac3 f30ab master branchA $ git checkout master $ git merge branchA Updating f30ab ..d45th Fast-forward Test.java | - 1 file changed, 1, deletion(-) git merge <branch> git merge --no-ff <branch> 24
  • 25. Merging Mechanisms: 3 way 98ca9 34ac3 f30ab master d45th branchA sd235 1w34t d45th 98ca9 34ac3 f30ab master branchA sd235 1w34t rt12u $ git checkout master $ git merge branchA Auto-merging Test.java Merge made by the ‘recursive’ strategy Test.jsva | 1 + 1 file changed, 1 insertion(+) 25
  • 26. Merging Mechanisms : Rebase 98ca9 34ac3 f30ab master d45th branchA sd235 98ca9 34ac3 f30ab master d45th sd235 branchA sd235 $ git checkout branchA $ git rebase master First, rewinding head to replay your work on top of it... Fast-forwarded branch2 to master. 26
  • 27. Merging Scenarios: Cherry Pick 98ca9 34ac3 f30ab master d45th branchA sd235 1w34t $ git checkout master $ git cherry-pick sd235 27 98ca9 34ac3 f30ab master d45th branchA sd235 1w34t rt56h (sd235) $ git cherry-pick sd235..1w34t
  • 28. Git Commands - remote git remote git remote –v git remote add <name> <url> git remote rm <name> git remote rename <old-name> <new-name> # Adding remote connection to the specified address. This connection can be reffered as “john” $ git init $ git remote add john http://dev.example.com/john.git $ git clone john 28
  • 29. Git Commands - fetch git fetch <remote> git fetch <remote> <branch> $ git branch -r # origin/master # origin/develop # origin/some-feature $ git fetch origin a1e8fb5..45e66a4 master -> origin/master a1e8fb5..9e8ab1c develop -> origin/develop * [new branch] some-feature -> origin/some-feature $ git checkout master $ git merge origin/master 29
  • 30. Git Commands - pull git pull <remote> (git fetch + git merge) git pull --rebase <remote> (git fetch + git rebase) $ git checkout master $ git pull --rebase origin 30
  • 31. Git Commands - push git push <remote> <branch> git push <remote> --force git push <remote> --all git push <remote> --tags $ git checkout master $ git fetch origin master $ git rebase -i origin/master # Squash commits, fix up commit messages etc. $ git push origin master 31 Always push to bear repositories Bear repositories can be initialize with “git init -- bear” command What special with bear repo is it doesn’t have workspace
  • 32. Work Flow Summary Workspace Staging Area Local Repo Remote Repoinit clone pull fetch add commit push commit reset reset revert reset initialize update rebase merge changes revert dcommit 32
  • 33. Workflow in Command line $ git clone https://github.com/vimukthir/gitTutorial.git project1 $ cd project1 $ git branch $ *master $ git branch –b feature1 $ git branch $ *mater $ feature1 $ git checkout feature1 $ git branch $ master $ *feature1 33
  • 34. Workflow in Command line $ git status on branch branchA #(use “git add <file>…” to update what will be committed) #(use “git -- checkout <file>” to discard changes in working directory) # # modified : Test.java $ git add Test.java S git status # on branch branchA # changes to be committed : # (use “git reset HEAD <file>…” to unstage) # new file : Test.java $ git commit Test.java 34
  • 35. Workflow in Command line $ git checkout master S git merge branchA Auto merging Test.java CONFLICT (content): Merge conflict in Test.java Automatic merge failed; fix conflicts and then commit the result. <<<<<<<< HEAD public void doTest() { ============== public void test() { >>>>>>>> branchA $ git add Test.java $ git commit Test.java –m “Resolving conflicts” $ git rebase origin master $ git push 35
  • 36. Git Commands - log git log git log -n <limit> git log –oneline git log –stat git log –p git log --author="<pattern>“ git log --grep="<pattern>“ git log <since>..<until> git log <file> git log --graph --decorate --oneline Workspace Staging Area Local Repo Git log 36 Git diff --cached Git diff --staged Git status Git diff HEAD
  • 37. Git Commands - config git config --global user.name <name> git config --global user.email <email> git config --global alias.<alias-name> <git-command> Changing local repo configuration # Add some SVN-like aliases git config --global alias.st status git config --global alias.co checkout git config --global alias.br branch git config --global alias.up rebase git config --global alias.ci commit 37
  • 38. Git Commands -Interactive Staging 38 Git Platform Interactive Staging status update revert patch diff add
  • 39. Git Commands - Interactive Staging Untracked Tracked & Unmodified $ git add -i staged unstaged path 1: unchanged +0/-1 Test.java 2: unchanged +1/-1 Test1.java *** Commands *** 1: status 2: update 3: revert 4: add untracked 5: patch 6: diff 7: quit 8: help What now> 39 What now> 2 staged unstaged path 1: unchanged +0/-1 Test.java 2: unchanged +1/-1 Test1.java Update>> Update>> 2 staged unstaged path 1: unchanged +0/-1 Test.java * 2: unchanged +1/-1 Test1.java Update>>
  • 40. Git Commands -Interactive Patch 40 Workspace 1 2 3 4 2 4 4 Staging Area
  • 41. Git Commands -Interactive Patch Untracked Tracked & Unmodified $ git add -p diff –git Test.java Index d26b0bd—93a2695 100644 --- a/Test.java +++ b/Test.java @@-8.7 +8,7 package Test; public class Test { + public void method1() { + } + public void method2() { + } } Stage this hunk [y,n,q,a,d,/,e,?]? 41
  • 42. Git Commands - stash git stash git stash apply git stash apply {stash_id} git stash list git stash branch stashBranch $ git status # On branch master # Changes to be committed: # (use "git reset HEAD <file>..." to unstage) # # modified: Test.java # # Changes not staged for commit: # (use "git add <file>..." to update what will be committed) # # modified: Test1.java 42
  • 43. Git Commands - log $ $ git stash Saved working directory and index state "WIP on master: 049d078 added the Test.java" HEAD is now at 049d078 added the Test.java (To restore them type "git stash apply") $ git status # On branch master nothing to commit (working directory clean) $ git stash list stash@{0}: WIP on master: 049d078 added the Test.java $ git stash apply # On branch master # Changes not staged for commit: # (use "git add <file>..." to update what will be committed) # # modified: Test.java # modified: Test1.java Git Commands - stash 43
  • 44. Git Commands - stash 44 Workspace Staging Area Stash BranchA git stash git apply Workspace Staging Area StashBranch git apply <StashBranch>
  • 45. Git Commands - web git instaweb Web support 45
  • 46. Commands Comparison (Git/SVN) 46 Git SVN git clone <repo> svn checkout url git pull svn update git init git add . git commit svnadmin create repo svn import file://repo git diff rev path svn diff -rrev path git apply patch -p0 git status svn status git checkout path svn revert path git add file git rm file git mv file svn add file svn rm file svn mv file
  • 47. Commands Comparison (Git/SVN) 47 Git SVN git commit -a svn commit git branch branch git checkout branch svn copy <source> <destination> svn switch <destination> git branch svn list http://example.com/svn/branches/ git merge branch svn merge -r R1:R2 <source>
  • 49. Github repo Git remote repository 49 https://github.com
  • 50. Git Architecture 50 .git branches hooks info logs objects refs COMMIT_EDITMSG config description HEAD index ORIG_HEAD Contains a set of shell scripts which executes after user commands Store blob, Tree, commit & tag Store heads, remotes & tags Keep track of current branch in workspace Keep track of staged changes Store user defined configurations
  • 52. Git Architecture How commit works 52 $ git commit Test.java Test1.java -m 'made a change’ blob a906cb2a4a904a152e80877d4088654daad0c859 Test.java blob b456tb2a4a904a152e80877d4088654daad0c843 Test1.java Path to Object = .git / a9 / 06cb2a4a904a152e80877d4088654daad0c859
  • 53. Git Architecture How commit works 53 blob a906cb2a4a904a152e80877d4088654daad0c859 blob b456tb2a4a904a152e80877d4088654daad0c843 tree 99f1a6d12cb4b6f19c8655fca46c3ecf317074e0
  • 54. Git Architecture How commit works 54 blob a906cb2a4a904a152e80877d4088654daad0c859 blob b456tb2a4a904a152e80877d4088654daad0c843 tree 99f1a6d12cb4b6f19c8655fca46c3ecf317074e0 commit fdf4fc3344e67ab068f836878b6c4951e3b15f3d
  • 56. Git Architecture 56 $ git cat-file -p 99f1a6d12cb4b6f19c8655fca46c3ecf317074e0 100644 blob a906cb2a4a904a152e80877d4088654daad0c859 Test.java 100644 blob b456tb2a4a904a152e80877d4088654daad0c843 Test1.java $ git cat-file -p a906cb2a4a904a152e80877d4088654daad0c859 public class Test { String arg … } $ git cat-file -p fdf4fc3344e67ab068f836878b6c4951e3b15f3d tree 99f1a6d12cb4b6f19c8655fca46c3ecf317074e0 author Vimukthi Randika <vimukthi@gmail.com> 1243040974 -0700 committer Vimukthi Randika <vimukthi@gmail.com > 1243040974 -0700 first commit
  • 57. $ find .git/objects -type f .git/objects/01/55eb4229851634a0f03eb265b69f5a2d56f341 # tree 2 .git/objects/1a/410efbd13591db07496601ebc7a059dd55cfe9 # commit 3 .git/objects/1f/7a7a472abf3dd9643fd615f6da379c4acb3e3a # test.txt v2 .git/objects/3c/4e9cd789d88d8d89c1073707c3585e41b0e614 # tree 3 .git/objects/83/baae61804e65cc73a7201a7252750c76066a30 # test.txt v1 .git/objects/95/85191f37f7b0fb9444f35a9bf50de191beadc2 # tag .git/objects/ca/c0cab538b970a37ea1e769cbbde608743bc96d # commit 2 .git/objects/d6/70460b4b4aece5915caf5c68d12f560a9fe3e4 # 'test content' .git/objects/d8/329fc1cc938780ffdd9f94e0d364e0ea74f579 # tree 1 .git/objects/fa/49b077972391ad58037050f2a75f74e3671e92 # new.txt .git/objects/fd/f4fc3344e67ab068f836878b6c4951e3b15f3d # commit 1 $ git gc Counting objects: 17, done. Delta compression using 2 threads. Compressing objects: 100% (13/13), done. Writing objects: 100% (17/17), done. Total 17 (delta 1), reused 10 (delta 0 Git Commands - pack 57
  • 58. $ find .git/objects -type f .git/objects/71/08f7ecb345ee9d0084193f147cdad4d2998293 .git/objects/d6/70460b4b4aece5915caf5c68d12f560a9fe3e4 .git/objects/info/packs .git/objects/pack/pack-7a16e4488ae40c7d2bc56ea2bd43e25212a66c45.idx .git/objects/pack/pack-7a16e4488ae40c7d2bc56ea2bd43e25212a66c45.pack Git Commands - pack 58 O1 O2 O3 O4 commit flows ∆1 ∆2 ∆3 git gc O4∆3∆2∆1
  • 59. GIT SVN Integration • Fedora • Ubuntu • Windows Using cygwin and install GIT via its package manager 59 $ yum install git-svn $ apt-get install git-svn Installation
  • 60. GIT SVN Integration SVN Repo Local Master Branch A Branch B Branch C 60
  • 61. GIT SVN Integration git svn clone <repo> git svn clone <repo> <directory> Download remote SVN repo to local machine SVN Repo Local Master $ git svn clone http://selene.aepona.com/internal/services/bdd- testsuit/trunk bdd-testsuit 61
  • 62. GIT SVN Integration git svn rebase git svn dcommit Applying changes to SVN repo SVN Repo Local Master $ git svn clone http://selene.aepona.com/internal/services/bdd - testsuit/trunk bdd-testsuit # Making branches, adding, commiting, merging $ git svn rebase $ git svn dcommit 62
  • 65. Merging Mechanisms : Rebase 98ca9 34ac3 f30ab master d45th $ git rebase -i HEAD~3 65 98ca9 1qw34 master Renaming License folder Moving Licenses different folder Adding Licenses origin/HEAD origin/master Adding license to separate folder origin/HEAD origin/master