You are on page 1of 214

Git Tutorial

http://ihower.tw/git
2013/1

Agenda

Git
Git
Git
Git
Git Tips
GitHub
2

?
a.k.a. ihower
http://ihower.tw
CTO PassionBean
The organizer of RubyConf Taiwan
http://rubyconf.tw
3


2002 ( copy )
2005 SubVersion
2007 SVK
2008 Git ( SVN push/pull)
2009 Git ( master feature branches)
2011 Git ( git flow topic branch rebase)

1.

Why?

Quote

http://jedi.org/blog/archives/004784.html


http://blog.ez2learn.com/2011/10/20/taiwan-software-lacking-of-vcs/

Version Control System (VCS)


Repository ()

Branch()
Tag()

Repository ?

log build files


Repository

10

Local VCS

copy paste , rcs

Version 3

Version 2

Version 1

paste

copy

11

Local VCS

copy paste , rcs

Version 3

Version 2

Version 1

paste

copy

11

Local VCS

copy paste , rcs

Version 3

Version 2

Version 1

paste

Local

copy

11

Local VCS

copy paste , rcs

Version 3

Version 2

Version 1

paste

Local

copy

11

Centralized VCS (Lock)

Version 3

Version 2

Version 1

lock and checkout

commit
B

12

Centralized VCS (Lock)

Version 3

Version 2

Version 1

lock and checkout

commit
B

12

Centralized VCS (Lock)

Version 3

Version 2

Version 1

lock and checkout

Server

commit
B

12

Centralized VCS (Lock)

Version 3

Version 2

Version 1

lock and checkout

commit

}
}
Server

12

Centralized VCS (Lock)

Version 3

Version 2

Version 1

lock and checkout

commit

}
}
Server

Local

12

Centralized VCS (Lock)

Version 3

Version 2

Version 1

lock and checkout

commit

}
}
Server

Local

unlock...
12

Centralized VCS (Merge)


CVS, Subversion, Perforce

Version 3

Version 2

Version 1

checkout
A

commit

13

Centralized VCS (Merge)


CVS, Subversion, Perforce

Version 3

Version 2

Version 1

checkout
A

}
commit

13

Centralized VCS (Merge)


CVS, Subversion, Perforce

Version 3

Version 2

Version 1

checkout
A

Server

commit

13

Centralized VCS (Merge)


CVS, Subversion, Perforce

Version 3

Version 2

Version 1

checkout
A

}
}
Server

commit

13

Centralized VCS (Merge)


CVS, Subversion, Perforce

Version 3

Version 2

Version 1

checkout
A

}
}
Server

commit

Local

13

Centralized VCS (Merge)


CVS, Subversion, Perforce

Version 3

Version 2

Version 1

checkout
A

}
}
Server

commit

1.
B
Local

2. Single point of failure


13

Distributed VCS
Git, Mercurial(Hg), Bazaar

Version Y

Version X

pull

push

Version X

Version Y

checkout

Computer A

commit

Computer B

14
9

Distributed VCS
Git, Mercurial(Hg), Bazaar

Version Y

Version X

pull

push

Version X

Version Y

checkout

Computer A

commit

Computer B

14
9

Distributed VCS
Git, Mercurial(Hg), Bazaar

Version Y

Server

Version X

pull

push

Version X

Version Y

checkout

Computer A

commit

Computer B

14
9

Distributed VCS
Git, Mercurial(Hg), Bazaar

Version Y

}
}
Server

Version X

pull

push

Version X

Version Y

checkout

Computer A

commit

Computer B

14
9

Distributed VCS
Git, Mercurial(Hg), Bazaar

Version Y

}
}
Server

Version X

pull

push

Version X

Version Y

checkout

Computer A

Local

commit

Computer B

14
9

Distributed VCS
Git, Mercurial(Hg), Bazaar

Version Y

}
}
Server

Version X

pull

push

Version X

Version Y

checkout

Computer A

Local

commit

Computer B

workflow
!
14
9

Local development
VCS
commit history log

CSV
commit history log
server
15

2. Git

16

Git
(DVCS)
Linux Torvalds (Linux creator)
Linux Kernel
2005/4 2005/6 Linux
Kernel2005/12 1.0

17

Git?
Git
Linux Kernel
Perl
Eclipse
Gnome
KDE

Qt
Ruby on Rails
Android
PostgreSQL
Debian
X.org

18

Why Git is Better than X


http://thkoch2001.github.com/whygitisbetter/

Git
Git
Staging

GitHub!

Delta

delta
storage

C1

C2

file A

C3

C5

file B

file C

C4

20

DAG (DAG: Directed acyclic graph)


metadata snapshots

snapshot
storage

C1

C2

C3

C4

C5

A1

A1

A2

A2

B1

B2

C1

C2

C2

C3

21

DAG (DAG: Directed acyclic graph)


metadata snapshots

C1

snapshot
storage

C2

C3

C4

C5

Im a mini filesystem!
A1
A2
Not
just VCS.

A1

A2

B1

B2

C1

C2

C2

C3

21

DAG (DAG: Directed acyclic graph)


metadata snapshots

C1

snapshot
storage

C2

C3

C4

C5

Im a mini filesystem!
A1
A2
Not
just VCS.

A1

A2

B1

B2

C1

C2

C2

C3

21

22

Git graph example


story53v2

C6
story103

story53

C8

C4

C7

master

C5

C3

C2

C1

C0

23

24

Git
http://gugod.org/2009/12/git-graphing/

25

3. Git ?

26

Git SHA1(demo)
echo sweet > sweet.txt
git add .
find .git/objects -type f
Git .git/objects/aa/
823728ea7d592acc69b36875a482cdf3fd5c8d
"blob" SP "6" NUL "sweet" LF SHA1
printf "blob 6\000sweet\n" | shasum
echo 'sweet' | git hash-object -w --stdin
git cat-file -p aa823728ea7d592acc69b36875a482cdf3fd5c8d
27
http://www-cs-students.stanford.edu/~blynn/gitmagic/ch08.html

Git is Content Tracker


Git is content-addressable filesystem
SHA1
reuse revert
(decouple)

Git Objects BlobTree


CommitTag

28

Git Object Database


Working Directory
./

Rakefile

README

lib/

simplegit.rb

29

Blob

Working Directory

Git Directory

./

Rakefile

blob : a874b7

README

blob : a906cb

lib/

simplegit.rb

blob : a0a60a

30

Tree

(SHA1) Trees
Working Directory
./

Git Directory
tree : 1a738d

Rakefile

blob : a874b7

README

blob : a906cb

lib/

tree : fe8971

simplegit.rb

blob : a0a60a

31

Commit

commit Root tree Parent commits SHA1


Working Directory
./

Git Directory
tree : 1a738d

Rakefile

blob : a874b7

README

blob : a906cb

lib/

tree : fe8971

simplegit.rb

blob : a0a60a

commit : a11bef

32

Object DAG Example


commit

tree

tree

blob

tree

blob

blob

33

Trace Git commit (demo)


Commit SHA1
git cat-file -p a08181bf3
( commit tree )
git cat-file -p ea44d629
( tree blob SHA1)
git cat-file -p d9647d8a
( blob )

34

Object DAG Model


commit

tree

blob

35

refs
Reference Commit
tag commit
( tag Object )

branch branch commit


HEAD current branch

36

Reference is cheap!
branch tag refs
commit
CSV

37

Integrity
SHA1 checksum
Integrity:
SHA1 tree

38

Object DAG Model


HEAD

remote

branch

tag

commit

tree

blob

39

Trace Git branch (demo)


cat .git/HEAD current
branch branch
cat .git/refs/heads/master master branch
commit
cat .git/refs/tags/foobar foobar tag
commit

40

Object DAG Full Example


HEAD

tag

branch

commit
commit 1

commit
commit2

commit 3

tree

tree

tree

tree

blob

tree

tree

blob

tree

blob

blob

blob

41

Git
SVN
svn merge
branch branch
svn log
svn commit checkout
commit

42

4-1. Git

43

Git is a collection of
command-Line utilities

44

Install Git
Git
http://help.github.com/set-up-git-redirect

Git GUI(optional)
GitX (Mac)
TortoiseGit/Git Extensions (Windows)
gitq, gitk (Linux)

45

Setup
~/.gitconfig
git config --global user.name "Your Name"
git config --global user.email "your@email.com"
git config --global color.ui true
git config --global core.editor "vi"

46

Setup (Windows only)

git config --global core.autocrlf true


git config --global core.safecrlf true

47

Repository (demo)
mkdir sandbox
cd sandbox
git init

48

commit (demo)
touch README
git add README
git status
git commit -m First Commit

49

(demo)
README
git status
git diff
git add .
(!)

git status
git diff --cached
git commit -m Update README
50

: Working tree/Staging Area/Repository


working
directory

staging
area

git directory
(repository)

git add

git commit

git checkout

51

Working tree
working
directory

staging
area

git directory
(repository)

Untracked files
git add

Changes not
staged for
commit

git commit

Changes to be
committed
Commtted

52

Why? Staging Area


Working tree
commit
Staging area commit

Staging Area Index

53

commit (demo)
touch a.rb
touch b.rb
git add a.rb
git commit Update a
git add b.rb
git commit Update b

54

staging (demo)
a.rb
git add a.rb
a.rb
git commit -m commit a
commit

55

commit
(demo)
git add --patch
y staging
n staging
s hunk

GUI gitx

56

commit ?
(demo)
working
directory

Changes not
staged for
commit

Commtted
()

staging
area

Changes to be
committed
git reset

git checkout

git directory
(repository)

(demo)
git rm a.rb
git mv b.rb c.rb
git add .
git commit Remove a, Rename b to c
copy ? Git (SHA1)
cp

58

revert ( commit )
commit
commit revert
commit

git revert e37c75787


git revert HEAD^

59

(demo)
git tag foo
git tag foo <SHA1>
git tag bar -m some message
git tag
git tag -d foo

60


git log
git log --oneline
git log --oneline --decorate --graph
git log
GUI ...

61

Diff
git diff <SHA1> Working Tree
git diff <SHA1> <SHA1>
git diff --stat <SHA1>
git diff --cached git diff --staged
Staging Area

62

untracked (demo)
git clean -n
git clean -f
git clean -x gitignore

63


.gitignore ( commit )
~/.gitignore ( Global )
commit
.gitkeep
.gitignore
https://github.com/github/gitignore

64


commit ?
tmp/*
log/*
()

build/* compile
.DS_Store
Thumbs.rb
65

Commit
//
bug fixed
unit test commit
commit
commit

git diff --check


commit

66

4-2. Git

67

Branch ?
Topic feature
Bug fixes
(refactor)

68


master

C0

C1

C2

69

(master) git branch iss53


master

C0

C1

C2

iss53

70

(iss53) git commit


master

C0

C1

C2

C3

iss53

71

(master) git branch hotfix


(hotfix) git commit

C0

C1

master

hotfix

C2

C4

C3

iss53

72

(master) git merge hotfix


master

hotfix

C0

C1

C2

C4

C3

iss53

73

(master) git merge hotfix


master

hotfix

C0

C1

C2

C4

C3

iss53

fast-forward

73

(iss53) git commit


master

C0

C1

C2

C4

C3

C5

iss53

74

(master) git merge iss53


master

C0

C1

C2

C4

C3

C6

C5

iss53

75

(master) git merge iss53


master

C0

C1

C2

C4

C3

C6

C5

iss53

C6 merge commit
( parent commits)
75

branch (demo)
git branch new_feature
git branch git branch <SHA1>
git checkout new_feature
( git checkout -b new_feature)

touch new_feature.rb
git add new_feature.rb
git commit -m New feature
76

branch (demo)
git branch new_feature
git branch git branch <SHA1>
git checkout new_feature
( git checkout -b new_feature)

touch new_feature.rb
git add new_feature.rb
git commit -m New feature
checkout
working tree

76

branch (demo)
git checkout master
git merge new_feature

77

Merge CONFLICT ?
git status conflict
<<<<< =====
>>>>>>
git mergetool
git reset HEAD --hard

78

Merge CONFLICT ?
git status conflict
<<<<< =====
>>>>>>
git mergetool
git reset HEAD --hard
git reset HEAD --hard
working tree

78

Branch
git branch -m old_name new_name
git branch -M old_name new_name ()
git branch new_feature -d
git branch new_feature -D ()

79


Straight merge
branch commits
merge commit
Parents
fast-forward merge
commit --no-ff

80

(cont.)
Squashed commit
git merge new_feature --squash
merge-commit
logSVN merge

cherry-pick
git cherry-pick 714cba
commit
-x log SHA1 (
backpointing )
81

branch
working tree branch
staging area modified
commit push
reset
stash
git stash
git stash apply
git stash clear
82

Branches (savepoint)
git branch is cheap!
branch Graph

83

: Scout way
test_merge
git checkout -b test_merge
git merge XXX

git checkout master


git merge test_merge fast-forward

84

: Savepoint way
git branch savepoint
git merge XXX
git branch -d savepoint

git reset savepoint --hard

85

Branch
git checkout <branch_name> Branch
git checkout <SHA1>
detached HEAD
current branch
git checkout -b new_branch

86


Three-way merge
two-way merge (svn
two-way merge)
three-way merge

conflict

87
http://en.wikipedia.org/wiki/Merge_(revision_control)

Git merge strategy


recursive commits
three-way merge (
)
git merge foobar -s recursive -X ours
git merge foobar -s recursive -X theirs

resolve threeway merge


octopus branchs
ours branch commits
subtree
88

Git Branchs
git merge A B C

http://stackoverflow.com/questions/
366860/when-would-you-use-thedifferent-git-merge-strategies

89

4-3. Git

90


Repository
SSH
git clone git@github.com:ihower/sandbox.git

HTTP/HTTPS
git clone https://ihower@github.com/ihower/sandbox.git

Git protocol ()
git clone git://github.com/ihower/sandbox.git

File ( Dropbox git init --bare --shared !! Crazy!!)


git clone file://path/to/repo.git
91

GitHub Repository

SSH Key
ssh-keygen -t rsa -C "your_email@youremail.com"

Github
git remote add origin git@github.com:your_account/
sandbox.git
git push -u origin master
git push
![rejected] git pull
92

Clone
SSH
git clone git@github.com:ihower/sandbox.git

Git
git clone git://github.com/ihower/sandbox.git

HTTPS
git clone https://github.com/ihower/sandbox.git

93

Pull
git pull origin master git pull
git fetch branch
branch merge
merge commit

94

Push Commit
git push git push origin master
master branch
master branch fast-forward

[rejected]
pull
push tags
git push --tags

95

branch
git branch -r
git checkout -t origin/foobar
git branch -r --merged ()

96

Branch
git push origin :foobar
git pull git fetch branch
git fetch -p

97

4-4. Git push

98

push branch
...
SVNGit Commit
commit logs!!
commit
commits
commit commits
commits

99

reset ()
revert reset commits

git reset e37c75787


git reset HEAD^ ( working tree)
git reset HEAD^ --soft ( staging area)
git reset HEAD^ --hard ()

100

savepoint
way branch
git reset <SHA1> --hard
branch name reference

101

Commit
committypocommit
git reset HEAD^

git commit -am message

git commit -a --amend

102

rebase ( commit )
git rebase -i e37c7578
pick 048b59e first commit
pick 995dbb3 change something
pick aa3e16e changed
#
#
#
#
#
#
#
#
#
#
#
#

Rebase 0072886..1b6475f onto 0072886


Commands:
p, pick = use commit
r, reword = use commit, but edit the commit message
e, edit = use commit, but stop for amending
s, squash = use commit, but meld into previous commit
f, fixup = like "squash", but discard this commit's log message
x, exec = run command (the rest of the line) using shell
If you remove a line here THAT COMMIT WILL BE LOST.
However, if you remove everything, the rebase will be aborted.
103

rebase is dangerous!!
It rewrite history!

104

rebase is dangerous!!
It rewrite history!
But I like it!
104

: rebase
rebase ( local branch)
1. branch
2. branch commits
apply/patch
commit log
local branch

105

: new_feature
master

master

new_feature

106

merge
(new_feature) git merge master
master

new_feature

107

rebase G !
(new_feature) git rebase master
master

new_feature

D`

E`

108

rebase G !
(new_feature) git rebase master
master

new_feature

D`

E`

merge
commit
108

rebase notes
D` E` D E commits
commitSHA1
rebase apply
conflict merge
conflict ?
continue, skip abort
rebase context

109

push commits

push !!

110


rebase + merge
( feature branch develop)

feature branch merge


feature branch typocommit
feature branch commits

feature branch git rebase develop -i


() git rebase -i
develop branch git merge feature --no-ff
111


rebase + merge

( feature branch develop)

feature branch merge


feature branch typocommit
feature branch commits

feature branch git rebase develop -i


() git rebase -i
develop branch git merge feature --no-ff
111

merge
commit

112

Demo (before merge)

113

http://ihower.tw/blog/archives/6704/

Demo (normal merge)

114

Demo (rebase + merge)

115

(1)
--no-ff merge commit
fast-forward
rebase feature branch push

feature branch

116

(2)
rebase conflict

rebase
branch rebase

117

rebase --onto
rebase
http://blog.yorkxin.org/2011/07/29/git-rebase

118

rebase --onto example

client branch C8, C9 master

C1

C2

C5

C6

C3

C4

master

C10

C8

server

C9

client

119

(client branch)

git rebase --onto C6 C4


C8'

C1

C2

C5

C6

C3

C4

C9'

client

master

C10

C8

server

C9

120

(client branch)

git rebase --onto C6 C4


C8'

C1

C2

C5

C6

C3

C4

C9'

client

master

C10

C8

server

C9

cherry-pick

120

5. Git

121

1. Remote Repositories

122

Centralized Workflow

Push Repository
Branches ()

shared
repository

developer

developer

developer

123

Integration-Manager Workflow

Open Source Push


Repository request pull
GitHub Fork Pull Request
official
repository

developer
repository

developer
repository

developer
repository

developer

developer

developer

pull
committer

124

Dictator and Lieutenants Workflow

Open Source Linux Kernel


patch pull request
official
repository

director

lieutenant
repository

developer

developer

lieutenant
repository

developer

developer

125

2. Branches ?

126


by Continuous Delivery ch.14

Develop on Mainline
Branch for Release
Branch by Feature
Branch by Team

127

Develop on Mainline
Mainline
branch release
branch spike

Continuous Integration
integrated
code
merge hell integration hell
releasable

128

Branch
?
Feature Toggle
http://martinfowler.com/bliki/FeatureToggle.html
Branch By Abstraction
http://continuousdelivery.com/2011/05/makelarge-scale-changes-incrementally-with-branchby-abstraction/

129

Branch for Release

release branch

release branch commit bugfixes (


bugfixescommit)
release branch branch
branch
code branch
130

Ruby on Rails
Rails 3.2
master 4.0
stable branches cherry-pick backporting
2-2-stable
2-3-stable bug fixes
3-1-stable bug fixes

131

Branch by Feature
Feature User Story Branch
Branch
releasable
CI
Feature Branch

Feature Branch
(Iteration)
Feature Branch ()
Tech Lead
132

Feature Branch
Open Source Project
Small core team patch
Release date patch

Antipattern ( branch )
codebase
code
commit
Delivery team merge
133

Branch by Team

Team
releasable
Team Branch

134

Git flow: branches

http://nvie.com/posts/a-successful-git-branching-model/
http://ihower.tw/blog/archives/5140


master: production-ready
develop:

136

137

(1)
Feature branches
bugs
develop
merge develop
develop
( merge rebase)

138

(1)
Feature branches
bugs
develop
merge develop
develop
( merge rebase)

branch!!
138

139

140

--no-ff
140

(2)
Release branches
release bugs
develop
merge master develop

141

(3)
Hotfix branches
release master

master
merge master develop

142

143

: Feature branches
: develop
release branches
: hotfix branches
: master

144
http://nvie.com/posts/a-successful-git-branching-model/

git-flow
git flow init
git flow feature finish feature_name
git flow feature publish feature_name
git flow feature track feature_name
git flow feature finish feature_name

145

Branch-Owner Policy
project leaders commit/merge
develop branch
release team master branch
topic branches pull
request code review
merge develop
GitHub pull request
146

Code Review

147

Branch Diff

148

6. Git Tips

149


git archive
git bisect
git blame
git grep
git show
git gc
git format-patch, send-email, am

150

git pull --rebase


merge
http://ihower.tw/blog/archives/3843

151

git pull --rebase


merge
http://ihower.tw/blog/archives/3843

branch
master
!

151

git pull merge?


rebase?
conflict merge
feature branch ?

conflict
--rebase

152

git pull merge?


rebase?
conflict merge
feature branch ?

conflict
--rebase
git flow
branches
merge !

152

Bash Prompt
http://ihower.tw/blog/archives/5436/

branch
commit

153

bash-completion
http://bash-completion.alioth.debian.org/
Install via Homebrew (Mac only)
brew install bash-completion
cp /usr/local/etc/bash_completion.d/gitcompletion.bash ~/.git-bash-completion.sh
~/.bash_profile
[ -f ~/.git-bash-completion.sh ] && . ~/.git-bashcompletion.sh
154

Useful alias
~/.gitconfig

[alias]
co = checkout
ci = commit
st = status
br = branch -v
rt = reset --hard
unstage = reset HEAD
uncommit = reset --soft HEAD^
l = log --pretty=oneline --abbrev-commit --graph -decorate
amend = commit --amend
who = shortlog -n -s --no-merges
g = grep -n --color -E
cp = cherry-pick -x
nb = checkout -b
155

Useful alias
~/.gitconfig

[alias]
# 'git add -u'
# 'git add .'
# 'git addall' staging
addall = !sh -c 'git add . && git add -u'

156

parent branch

merge
Github page
git checkout --orphan new_branch
git rm -rf . #

git add .
git commit -m new branch
157

Orphan branch example

158

commit branch ?
http://www.programblings.com/2008/06/07/the-illustrated-guide-to-recovering-lost-commits-with-git/

git reset, rebase branch


git gc ( commits
90)

git reflog git fsck --lost-found


git cherry-pick <SHA1>
git merge <SHA1>

159

git-filter-branch

http://gugod.org/2012/07/split-a-git-repository/

https://help.github.com/articles/removesensitive-data

160

Git Submodule Subtree


Git SVN
? SVN Externals?
Submodule
http://josephjiang.com/entry.php?id=342
http://josephjiang.com/entry.php?id=357

Subtree
http://git-scm.com/book/ch6-7.html

Ruby/Rails Develops XD
Ruby Bundler dependencies
161

git svn
git SVN repository
git svn clone http://url/svn/trunk/ project_name
(normal git operations)
git svn dcommit

162

Git Server
git --bare via SSH
Gitosis
Gitolite
Gitlab Gitolite Web
http://gitlabhq.com/

163

7. Github

164

GitHub?
twitter
facebook
rackspace
digg
Yahoo!
shopify
EMI
six apart

jQuery
YUI 3
mootools
Ruby on Rails
node.js
symfony
mongodb
Erlang
167

Watch!

Fork

169

Fork network

170

Patch ?
Fork Repository
Clone
Fix bugs Enhance features
Push
Pull request

171

Fork & Pull Requests

75% fork outside merge !!


watch 25% outside contributions !!

172

Follow!

Wiki

174

Code Review

Diff

Download Source
or Package

176

Github pages
http://pages.github.com/

your.github.com repositoryGithub
http://your.github.com
gh-pages your_projectGithub
http://your.github.com/your_project

177

Github pages
http://pages.github.com/

your.github.com repositoryGithub
http://your.github.com
gh-pages your_projectGithub
http://your.github.com/your_project

Git

( Branch merge)
177

Thank you.

google ihower

178


Scott Chacon OmniGraffle diagram
https://github.com/schacon/git-presentations
client

C1

C2

C5

C6

C8'

C3

C4

C10

C9'

master

server

179


http://pragprog.com/screencasts/v-jwsceasy/
source-control-made-easy
https://peepcode.com/products/git
https://peepcode.com/products/advanced-git
Git Internals, Peepcode
Pragmatic Version Control Using Git, Pragmatic
Pragmatic Guide to Git, Pragmatic
Continuous Delivery Ch.14
Version Control with Git, OReilly
180


http://ihower.tw/blog/category/git
http://nfarina.com/post/9868516270/git-is-simpler
http://tomayko.com/writings/the-thing-about-git
http://think-like-a-git.net
http://yanpritzker.com/git-book/
http://progit.org/book/
http://git-scm.com
http://help.github.com/
http://gitimmersion.com
http://www.gitready.com/
http://gitref.org
http://try.github.com

181

You might also like