You are on page 1of 80

@AorJoa

Chapter 1 : Introduction
1.1 (Version Control System : VCS)
Local Version Control System : LVCS
Centralized Version Control System : CVCS
Distributed Version Control System : DVCS
1.2 Git
1.3 Git
Chapter 2 : Git basic
2.1
2.2 Git
Chapter 3 : Installation
3.1 Windows
3.2 Mac OS X
3.3 Others operating system
Chapter 4 : Git scenario
1 : !@#%!@
2 : Remote repo
3 : ,,
4 : Conflict
5 : Alias
6 : Fast Forward 3way Merge
7 : Branch
8 : Commit History
9 : Difference
10 : Reflog
11 : Optimize
12 : Stash

1
1
2
3
4
6
16
18
29
36
38
39
44
50
53
55
55
56
58
60
62
63
63

13 : Tag Commit
14 : ignore
Workflow
Fork Pull Request
Chapter 5 : Git GUI

Chapter 6 :

65
65
66
69
72
76
77

Chapter 1 : Introduction
Git (Version control)


Version control ?
1.1 (Version Control System : VCS)
Version control ( Revision control, Source control)

( & Something like that) Version control


Backup ( )
Version control 3 Local, Centralized Distributed
Local Version Control System : LVCS

Backup



Revision Control System : RCS
Walter F. Tichy
RCS Developer
Tools Mac OS X
Patch

2
Centralized Version Control System : CVCS
Local Version Control

USB Drive

Server Server
Server Server (
)

Project manager

Server Server
Server Harddisk
(Server Client ) Server Log
( Server Server )
Version control Centralized CVS, SourceSafe,
Subversion, Team Foundation Server

3
Distributed Version Control System : DVCS
Centralized Version Control System Server
Distributed
Server
Server Server
Server
Server (
T___T)

Team A Team
B
Server

Centralized Server
Git (), BitKeeper( ), Mercurial, Bazaar Darcs

4
1.2 Git
Linux kernel
. . 19912002 Linus Benedict Torvalds Linux
kernel Patch Source code
( Tarball)
Linus Linux kernel
Linus
Patch
Linus Patch Source code
Patch
2002
DVCS BitKeeper
BitMover BitKeeper Commercial software
Larry McVoy CEO BitMover Open
source
2005 Andrew Tridgell ( "Tridge")
Samba BitKeeper
Open source SourcePuller BitKeeper
client ( Larry McVoy )
Tridge Reverseengineered
BitKeeper Larry Tridge
Metadata
? Tridge
Metadata BitKeeper SourcePuller
Metadata Telnet
BitKeeper server HELP BitKeeper client
Linus Tridge
Tridge Tridge Tridge
Larry 3

5
BitKeeper server Tridge Linus
Community Open source License
Community ( Tools BitKeeper )
Linus Project Linux Git
, ,
Git
Global information
tracker "Goddamn idiotic
truckload of sh*t" ( )
Linus Version control
Linux kernel
Version control BitKeeper Version control


( branch )
Distributed Version Control System
Linux kernel ( )
Git 3 2005 C,Shell script Perl, 6
Git Open source GNU GPL version 2
Selfhosting ( Git Source code Git ) 7 , Branch
18 29 ( Patch Linux kernel
Tree 6.7 ) 26 2005 Linus Junio Hamano Git

6
1.3 Git
Version control (
) Version control (
) Git

Version control CVS, Subversion, Perforce, Bazaar

Delta Storage ( Differences )

3 Commit ( Version )
Commit 1 A, B C
Commit 2 ( )
A B C C ( C )
Pointer C Commit 1
Commit 3 A Pointer Commit
Commit 2 B C Commit 2
Commit 3 C Pointer Commit ( Commit 2
) Commit 2 Pointer C
C Commit 1 C Pointer

7
Commit Commit
Commit Commit

(Note : Commit 3 Origin.c Rename Commit 4 )

Delta storage Snapshot


storage Snapshot storage Snapshot ( Delta
) Snapshot storage
Revision Snapshot Snapshot References
Commit Delta storage Reverse delta
Forward delta 4 5 ( Commit)
2

Reverse File
Forward File Delta


3 A , B C
Commit 1 Commit Snapshot
Snapshot A1,B1
C1
Commit 2 A B Snapshot A2 B2
C Reference Snapshot C1
Commit 3 A Reference Snapshot A2 Snapshot
A1 B C Snapshot B3 C2
2 Delta storage
Snapshot storage Git Storage Snapshot ( Git git pack
Delta )

9
Git Snapshot Hash SHA1 ( ) 160 Bits
SHA1 40 (09 af)
Checksum Checksum
Checksum
SHA1

160 Bits 60 Bits

10
Git Snapshot 40
Checksum Snapshot 3 c3d, f13, 6d4 84f
3 40 ( 3
) Git Commit Commit KeyValue
Key Snapshot Value
Git
Commit Snapshot
Snapshot

Delta Snapshot Conceptual


Git Delta

Distributed VCS Server
Log Commit
Server Subversion Version control
Centralized VCS
Log Commit Server
Git Coding
Server

11

Git Add Commit
Git
Snapshot

4
Untrack Git Git
Stack trace, Error Track
Committed Commit Git
Committed
Modified Git Staged Commit
Git
Staged Commit Untrack Modified
Git Staged
Commit ( Config
Commit )

12

Git Working directory (


) Staging area ( git add)
Git Repository Git Snapshot Commit ( git
commit) Snapshot Staging area Git
Committed Git Version control
Commit ( Staging Commit Working directory ) Commit
Snapshot Staging area

13
Git object model
Git Hash SHA1 40
Object
6ff87c4664981e4397625791c8ea3bbb5f2279a3
Object 3 Type Object , Size
Object Content Type Object 4
Blob,Tree,Commit Tag Pointer Object
Git
Blob Binary Large Object
Object Metadata

Tree Directory Blob Snapshot


SHA1 ( ) Tree
Snapshot

14
Commit Tree Commit

+ Tree Tree
+ Parent Commit
+ Author
+ Committer Commit
Object Model

15
Flow Commit Git (cc: schacon.github.io) Git commit
Tree Tree Tree Blob
Snapshot Commit

Tag Commit Digital signature


Tag Object
Software Commit
Tag Commit Release V1.0
Commit Tag

Git Branch
Checkout

Branch Master Feature Branch Feature


Software
Branch Issue Master Merge Master Master
Feature Merge Master

16

Chapter 2 : Git basic


2.1
Init
Init Initial Git Version control
Clone
Git
Clone Repository Clone
Git
Repository
Snapshot
Snapshot


Git snapshot

Repository
Repository Repo Git object model
Commit Repo
Repo Repository
Local repo Remote repo
Branch
Branch
Branch
Git Branch
Branch Branch Master
Branch Feature Branch Feature Master
Branch Branch Feature Master
Branch Branch HEAD

17
Add
Add Snapshot Add
Staging area Commit Snapshot
Git Repository Commit
Commit
Commit
Commit Commit
? Commit Snapshot Staging area Git
Repository
Commit
Branch
Commit Branch Git Branch
Switch
Remote
Remote Repository Git
Repo Official version Git
Server DVCS Repo
Code Repo Repo
Repo ( Merge
)
Push
Update Remote repository
Pull
Remote repository
Merge
Branch Master
Branch Dev_send_email
Merge Master

18
Master Master

2.2 Git
$ git config

Log Git

$ git config global user.name AorJoa

$ git config global user.email aorjoa@iaor.com


email
Git global $ git config list

$ git config global color.ui true


UI
$ git init
Git Git
Commit Repo
.git ( .
OS )
Repo
$ git init
Git

19
$ git clone
Remote Repo
Git init Git clone
Remote Repo
.git Clone Repo
https://github.com/chanwit/spockreporthtml.git
$ git clone https://github.com/chanwit/spockreporthtml.git Clone Remote Repo

$ git status

$ git status

$ git add
Staging area Commit Git Repo
$ git add .

$ git add sub_folder/*.txt


sub_folder .txt
$ git add index.txt
index.txt
$ git status

20

Add Staging area 2 index.txt test.txt


sub_folder Add
$ git rm cached sub_folder/test.txt Cached
version $ git checkout
index.txt

$ git mv
$ git mv Space
$ git mv sub_folder/test.txt sub_folder/bio.txt

21

$ git commit
Staging area Git Repository $ git
commit m
Commit
$ git commit m "Initial Git basic" Commit

22
$ git rm

$ git rm $ git remove

r
$ git rm r sub_folder
Git
2 Commit
Git Repo
Commit Commit

$ git branch
Branch
Branch Branch

$ git branch
Branch
$ git branch dev
Branch dev
$ git branch d dev Branch dev
$ git checkout
Branch Commit Branch
$ git checkout dev
Branch dev Branch dev
$ git checkout b dev
Branch dev Checkout
$git branch dev $git checkout dev

23
$ git merge
Branch Branch
about.html Branch background_red master
Checkout master Merge background_red
master master about.html
$ git checkout b background_red background_red

$ git add . Commit Branch background_red $ git commit m "change


about.html backgound to red"

$ git checkout master master


merge $ git merge background_red

24
$ git remote
Remote repository
Repository Remote repository
origin Server Github.com Repository
Path https://github.com/Aorjoa/basic_git.git
$ git remote add origin https://github.com/Aorjoa/basic_git.git

$git remote rm origin


$ git push
Repo Remote repo
Commit Repo
Git Working directoty ( .git
) Repo Server Bare repository ( bare
$ git init bare $ git clone bare https://github.com/chanwit/spockreporthtml.git)
push git push Remote
repo Branch ( Branch Local Remote Branch
master )
$ git push origin master

25
$ git fetch
Remote Repo
( Remote Repo Update Local Repo
Remote Repo
Working directory )
Merge Merge
Working directory
$ git fetch origin master Remote Repo origin ( Branch master)

$ git pull
git pull git fetch git merge
git fetch git merge Merge
git pull
$ git pull origin master Remote Repo origin Local repo
Remote repo Merge Branch )

26
$ git rebase
Branch Merge Rebase

Rebase commit Push Remote repo Rebase
Branch master Feature
Bug Branch master Bug Branch
Feature master Rebase
Branch Feature Bug

$ git rebase master Log Graph Rebase Branch Feature


Commit

Commit B , C F 3 Commit B, C F Commit


timeline Rebase Commit Push
Remote repo Commit Push Object Commit
Push Remote rep Object Commit
.

27
$ git log
Log
Commit Hash SHA1
Commit
$ git log
Repository
Commit Commit
Branch

$ git reset
2 soft reset
soft Commit hard reset hard
Commit ( Snapshot Reset )
git reset hard
$ git reset hard ed51e23

28
$ git revert

$
git reset
Commit

$ git revert noedit ed51e23..HEAD
Commit ed51e23
HEAD
( Conflict

) Commit 2 ca764a3 4e8849b noedit
Comment Commit Revert Comment
Commit Revert Commit
(Add line two Add line one)


Flow Git

29

Chapter 3 : Installation
Version control, Git Git
Git
========================================================================
3.1 Windows
========================================================================
1) http://gitscm.com/downloads

Download Windows

2) Git

30

Installer Next

31
Next

Directory Git Next

32
Component Next

Shortcut Next

33
: Git Git Bash
: Windows Command Prompt Git
: Windows Command Prompt Git UNIX
( Command CMD )
Next

34
Windows UNIX
Platform Next

35
View ReleaseNotes.rtf Finish

Git bash Start menu > All


Programs > Git > Git bash
git version

Version Git

36
========================================================================
3.2 Mac OS X
========================================================================
1) http://gitscm.com/downloads Download Mac OS X

2) Git

37

Continue

38

Terminal git version

========================================================================
3.3 Others operating system
========================================================================
Donwload http://gitscm.com/downloads

39

Chapter 4 : Git scenario

Git Git
Mac OS X Source
code Software
Windows Git bash
UNIX Git bash ( Command Prompt)
1 : !@#%!@

$ git config global user.name AorJoa
( )
$ git config global user.email aorjoa@iaor.com
Email
$ git config global color.ui true
$ git config global user.ui true
2

Git flow


$ git init Repository Git

40


readme.txt $ nano readme.txt nano Text editor Unix
Notepad
Ctrl + X ( X )

Save Y

Enter nano

Command ls

Text editor Notepad Save


$ git status Working directory
> Add/Delete/Rename
Add Commit
Commit (
Track Commit Commir Add) $ git add

41

2 Commit ($ git add )


Commit Commit ($ git checkout )

$ git rm r recursive
$ git rm r Git
Working directory

$ git mv

42
readme.txt Untracked files
git add $ git add readme.txt $ git status

Change to be committed
Staging area ( Snapshot) $ git commit m "Initial Git repository"
Snapshot Staging area Git

$ git status woking directory

readme.txt git add readme.txt git commit m


comment Edit#1 add line ( Sublime nano)

43
$ git log Commit

Flow Git Repo


Clone Github $ git init $ git clone Repo

$ git clone https://github.com/chanwit/spockreporthtml.git Clone Remote Repo


Remote repo

44
2 : Remote repo
.zip .tar Tarball
Email
Email Version control Git
Remote repositoy
Git remote
repository Github.com, Gitlab.org Bitbucket.org
Remote repo Github.com
Tutorial try.github.io
Portfolio (
Octocat Github >__<)
StepbyStep
(1) https://www.github.com

Sign up for GitHub

(2) ( Micro Plan


) Provate repo

45
(3) Finish sign up Email Github
link Confirm
Repo

(4) Connect Pull Push Github Github


Email Github SSH
key (Secure Shell key) Publickey cryptography
Public key ( .pub) Private Key ( )
Public key Private
Key Private Key Public key

Public key Github Private key SSH key


$ sshkeygen t rsa C "aorjoa@iaor.com"

46
Enter file in which to save the key (/Users/dekcom/.ssh/id_rsa): Enter
/Users/dekcom/.ssh/id_rsa already exists.
Overwrite (y/n)? y ()
Enter passphrase (empty for no passphrase): Passphrase (
) Enter
Enter same passphrase again: Passphrase
Private key (identification) Public key
Your identification has been saved in /Users/dekcom/.ssh/id_rsa.
Your public key has been saved in /Users/dekcom/.ssh/id_rsa.pub.

Public key
$ cat ~/.ssh/id_rsa.pub


Github

47
github.com Public key github Setting
> SSH Keys > Title ( ) > Key > Add key

(5) Repository
> New repository

> Repo

48
> Repo

> Repository Public Private

> Git README , .gitignore ( Git


repo) License ()

Create repository

SSH URL Repo


(6) Command line Remote repository Git
$ git remote add origin git@github.com:aorjoabook/basic_git.git
(7) Push Repo
$ git push origin master
Passphrase Dialog

49
Passphrase OK ( OS Command line )

Git github.com Repo


Repositories Repo Push
basic_git

readme.txt

readme.txt

Remote repo Github


Github Bitbucket
GitLab ( SSH keys )

50
3 : ,,
3 Checkout, Revert Reset
$ git status Commit
Working directory
Clean Conflict

> Checkout
Checkout
Branch
Commit
Checkout Branch master

(1) $ git log

(2) 8ba8bb9 $ git checkout 8ba8bb9

(3) $ git log

51
(4) $ git branch Detached branch branch checkout
branch

> Revert
Revert Commit
Commit
Revert Commit B Commit D
Commit C Commit
Revert
Revert Commit A Revert Commit A HEAD Conflict (
Conflict Conflict)
(1) Log $ git log

(2) $ git revert 8ba8bb9..HEAD ( ..HEAD Conflict )


Editor
Commit
:q! Enter Editor
$ git revert noedit 8ba8bb9..HEAD

52
(3) $ git log Revert commit 2e7885

(4) readme.txt

> Reset
Reset
Spec Software
2 Commit

2 Commit
Commit
soft hard

(1) log $git log oneline ( log )

(2) $ git reset hard


(3)

53

(4) Log $ git log oneline Log

4 : Conflict
Version control Conflict
Conflit Conflict
git
Branch master (Remote repo) Push Github Branch master
(Local repo) Git

(1) Github Push
Commit ( 3) Branch
Branch master

$ git commit am git add ( add ) git commit


(2) $ git pull origin master ( git pull git fetch + git merge )

Conflict

54
(3) Editor

2 Commit Conflict HEAD Commit 9a570f


HEAD

Commit

(4) Log

Git Tools Merge conflict Conflict


Conflict Editor

55
5 : Alias
Alias
$ git log graph decorate pretty=oneline abbrevcommit all alias lol $ git
config global add alias.lol "log graph decorate pretty=oneline abbrevcommit all"

$ git log graph Commit


(cc : http://uberblo.gs/2010/12/gitloltheothergitlog)
6 : Fast Forward 3way Merge
> Fast forward
Merge Git


> 3way Merge Merge
Commit
3
3way
Merge
3way Merge

3way
Merge $ git merge noff
branch

56
7 : Branch
Branch
Branch Brach
Branch Branch Branch
> Local branch
Branch Local $ git checkout b 'bugfix' $ git branch

> Remote branch


Local branch Branch Remote repo
Commit $ git commit am 'bugfix branch'

$ git push origin bugfix:issue Branch bugfix Branch


issue Remote repo origin $ git push origin master $ git push origin
master:master Branch Branch Remote repo

57
Github Remote repo ( origin) Branch issue
Branch Branch issue ( Branch bugfix Local repo )
master Branch master

> Local branch


Local branch Branch

Checkout master

$ git branch D bugfix

> Remote branch


$ git push origin delete issue Branch issue Remote repo

Github Branch master

$ git push origin :issue ( Branch


Remote repo )

58
8 : Commit History
Commit
readme.txt Commit !
Commit Commit (
Commit Comment Done
Commit Done#2 )
> Commit
Commit ( Commit ) $ git commit amend
m 'Edit comment' Comment Commit

> History Commit Rebase


Git $ git rebase i Rebase
Commit Commit
Commit ( base )
(1) Log $ git lol

59
(2) $ git rebase i 8ba8bb9
Commit base 8ba8bb9 ( Commit 8456e87
base)

Commit af13edd Cursor dd ( d )

:wq
Git Rebase Conflict

$ git status Conflict

60
readme.txt both modified
readme.txt Conflict

Commit

rebase $ git rebase continue

$ git lol $ git log

9 : Difference
Version control Option
Git $ git diff

Commit $ git diff

61
Branch $ git diff master dev

dev master ( ) master dev



Commit $ git diff 2e78857 9a570f6

62
$ git whatchanged since="2 day ago" oneline $ git whatchanged since="2
weeks ago" oneline

10 : Reflog
Git 23
!! $ git reset
Commit Holy Sh**t !
Git Git
Commit, Checkout Reflog $ git reflog

$ git reset hard 2e78857

63
$ git cherrypick af13edd Commit af13edd ( Commit Reflog)

11 : Optimize
Git Local repo Repo
Reflog
$ git gc

Delta compression
12 : Stash
Pull, Merge, Rebase Working
directory Commit

$ git stash Pull, Merge, Rebase Working directory
$ git stash pop

64

$ git status

$ git stash

Git status Working directory ( )

Pull , Merge Rebase $ git stash pop

65
13 : Tag Commit
Object Git Tag object
git tag a v1.0 d256a7f m 'Tag release V.1.0' ( Tag
tags Push $ git push origin tags

14 : ignore
Git
.war Deploy Git .gitignore
.gitignore $ git add .gitignore

Git readyou.me

66

Workflow
Branch
Flow Clone > Branch dev
> Branch dev > Merge Checkout master > Pull
Branch master > Checkout branch dev > Rebase branch dev >
Checkout master > Merge branch dev > Push Remote repo

Flow ()

Branch master (master Tag 0.1 Tag Commit )

Dev ( ) hotfix ( )
Merge Master master Tag 0.2 Dev
Merge hotfix Dev

67

Merge ( ) Dev ( ) Release ( )

Checkout dev Release

Merge Branch Dev Release ( ) Merge Master

Merge Dev Dev Merge Release Release Merge Master

68

(cc : http://nvie.com/posts/asuccessfulgitbranchingmodel)

69

Fork Pull Request


Fork
Clone Remote repo
Remote repo Push Repo
Push Repo Github
Fork
(1) Login Github Repository Fork

(2) Fork
Fork
(3) Repo Fork

Pull Request
Fork repo Repo Guthub Repo
Pull Request Fork Library
graph
Library graph Pull Request Repo
Pull Request
(1) Repo Fork

70
Repo Fork

(2) Repo Pull Requests


(3) New pull request

Create pull request

Comment Create pull request

71
Pull Request

Repo
(1) inbox message
Update readme.txt

(2) Merge pull request

(3) Confirm merge

(4) Request

72

Chapter 5 : Git GUI

Command line Git GUI Graphical User Interface


Version control Plain
Text Git GUI
Software SourceTree
OS

Git GUI SourceTree Commit Git


Commit Push
(1) http://www.sourcetreeapp.com SourceTree

(2) (
)
(3) Settings

(4) Add Account Account Git SourceTree


(5) Username GitHub

73
(6) Remote repo Add

(7) + New Repository > Add existing local repository

(8) Browse Local repo


(9) scenario_git

(10)

(11) Setting Advanced

(12) Use global user settings global

74
(13)

(14) Folder Repo Uncommitted changes

(15) Unstaged files Staged files

(16) Commit

(17) Comment Commit

(18) Commit Master Origin

75
(19) Push Origin (Github) Branch master

(20) master origin/master

(21) Gitghub

Git GUI Command


line Commit

76

Chapter 6 :

(1) Pro Git, [Scott Chacon, Ben Straub], ISBN13: 9781484200773


(2) Pro Git Grean Edition, https://github.com/opendream/progit
(3) commit commit ,
http://pphetra.blogspot.com/2011/02/gitcommitcommit.html
(4) git(1) Manual Page, https://www.kernel.org/pub/software/scm/git/docs/
(5) Getting Git Right, https://www.atlassian.com/git/
(6) Software Engineering ( 5), https://sites.google.com/site/chanwit/courses/se562
(7) Feature Branch Pull Request ,
http://www.narisa.com/forums/index.php?app=blog&blogid=9&showentry=3070
(8) git lol the other git log, http://uberblo.gs/2010/12/gitloltheothergitlog
(9) Git workflow, http://scottchacon.com
(10) Git introduction, http://en.oreilly.com/rails2008/public/asset/attachment/2816
(11) Git Magic by Ben Lynn Students of Stanford,
http://wwwcsstudents.stanford.edu/~blynn/gitmagic/book.pdf
(12) Git Alias , http://armno.in.th/2012/10/25/creatinggitalias
(13) version control Delta Storage,
http://versioncontrol.net/versioncontrolhowto/versioncontroldeltastorage
Thanks Wikipedia, Google, StackOverflow, Github and another source. :)

77

Blog Blog
( Tag ) Git




Git Clone (>.<)
Community
Git :
https://www.facebook.com/groups/440497309296387
Creative Common 4.0

Email : bhuridech@gmail.com
Website : www.iaor.com
WebBlog : aorjoa.blogspot.com
GitHub : https://github.com/Aorjoa

Source code

You might also like