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
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
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
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
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
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
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