IAC 2024 - IA Fast Track to Search Focused AI Solutions
Git Presentation - Purple Scout AB Malmö
1. Git
Sébastien Cevey <seb@cine7.net>
http://bytes.inso.cc/
Purple Scout presentation (April 17, 2009)
Swiss Federal Institute XMMS2 Music Player Playlouder MSP
of Technology, Lausanne
4. Version Control Systems
• Record history
• Facilitate team work
• Expose source code
• Branch/merge development paths
4
5. Centralized VCS
permissions
Master branches
Repository
commit
checkout update
Copies Working Working Working
Copy Copy Copy
Alice Bob Charlie
5
6. Distributed VCS
Repository
Repository
Remote
(public/private) push
Repository
clone
pull
Working Working Working
Copy Copy Copy
Local Repository Repository Repository
Alice Bob Charlie
6
7. Centralized vs Distributed
• •
synchronous asynchronous
- unique reference - copy = repository
- fixed hierarchy - loose hierarchy
• •
centralized access offline:
control - history
- commit
- branches
• •
simpler more powerful
7
9. Git History
• Linus Torvalds needed a replacement for
BitKeeper to maintain the Linux kernel
• Requirements:
• Distributed • Non-linear, custom workflows
• Scalable & Fast • Talk over HTTP, SSH, FTP, rsync,
email, etc
• Integrity guarantee
• First prototype on April 7, 2005
• Now used by many FOSS projects:
Linux kernel, Freedesktop (X.org), Ruby on Rails, XMMS2,
Android, Gnome, GCC, etc.
9
10. Why “Git” ?
git (plural gits)
2. (British, slang, pejorative) A silly,
incompetent, stupid, annoying or childish
person.
“Jacko is a git.”
Linus Torvalds:
“I'm an egotistical bastard, and I name all my projects after
myself. First Linux, now git.”
Alternatively: “Global Information Tracker”
10
11. Git vs. other DVCS
(Mercurial, Darcs, Bazaar, Monotone, etc)
+ Fast, scalable (execution and setup)
+ Powerful small tools (Unix spirit)
+ Lively development
+ Linus (smart design)
11
12. Git vs. other DVCS
(Mercurial, Darcs, Bazaar, Monotone, etc)
+ Fast, scalable (execution and setup)
+ Powerful small tools (Unix spirit)
+ Lively development
+ Linus (smart design)
- Learning curve (grasp concepts)
- Imperfect on MS Windows
- Linus (no library)
12
13. Why Git is better than X?
• Cheap Local Branching
• Everything is Local
• Git is Fast
• Git is Small
• The Staging Area
• Distributed
• Any Workflow
• Easy to Learn
Source: http://whygitisbetterthanx.com/
13
38. rewind history
$ git reset --hard HEAD^ # parent of HEAD
HEAD is now at 54eda77 More stuff
master
$ git status
# On branch master
nothing to commit (working directory clean)
Makefile
README
54eda77
src
|-- Makefile
|-- helloworld.c
`-- helloworld.h
04f087b
38
39. rewind history
$ git reset HEAD^ # parent of HEAD
src/helloworld.c: locally modified
master
$ git diff
diff --git a/src/helloworld.c b/src/helloworld.c
index e69de29..54a606d 100644
--- a/src/helloworld.c
+++ b/src/helloworld.c
@@ -0,0 +1 @@
+/* End of fil */
Makefile
README
54eda77
src
|-- Makefile
|-- helloworld.c <- edited
`-- helloworld.h
04f087b
39
40. new history
$ $EDITOR src/helloworld.c
$ git commit -a -m quot;Proper commitquot;
master
Created commit 0657c63: Proper commit
1 files changed, 1 insertions(+), 0 deletions(-)
0657c63
Makefile
README
54eda77
src
|-- Makefile
|-- helloworld.c
`-- helloworld.h
04f087b
40
49. undo
$ git reset --hard stable # undo last merge
HEAD is now at 0657c63 Proper commit
feature-X
master
$ git checkout feature-X
Switched to branch quot;feature-Xquot;
20fbfdd
stable 0657c63
02bbf10
Makefile
README
54eda77
src
|-- Makefile
|-- helloworld.c
`-- helloworld.h
04f087b
49
50. rebase
$ git rebase master
First, rewinding head to replay your work on top of it...
feature-X
Applying: Work on X
master
Applying: Edit README for X
$ git show-branch
* [feature-X] Edit README for X
! [master] Proper commit
aeaf0a
--
* [feature-X] Edit README for X
* [feature-X^] Work on X
*+ [master] Proper commit
2e309b
stable 0657c63
Makefile
README
54eda77
src
|-- Makefile
|-- helloworld.c
`-- helloworld.h
04f087b
50
57. clone
t
aul
r
ste
f
seg
ma
fix-
(over local filesystem, ssh://, git://,
rsync://, ftp://etc)
D
C E
B
lt
fau
r
ste
public repo
A
seg
ma
fix-
$ git clone $URL
Initialized empty Git repository in local/
copy/.git/
D
C E
B
local repo
A
57
58. branch namespace
t
aul
r
ste
f
seg
ma
fix-
D
C E
origin/
B
lt
fau
r
r
ste
ste
public repo
A
seg
ma
ma
fix-
$ git branch --all
*master
origin/HEAD
D
origin/fix-segfault
C E
origin/master
B
local repo
A
58
59. public repository updated
t
aul
r
ste
f
seg
ma
fix-
D F
C E
origin/
B
lt
fau
r
r
ste
ste
public repo
A
seg
ma
ma
fix-
D
C E
B
local repo
A
59
60. fetch update
t
aul
r
ste
f
seg
ma
fix-
D F
C E
origin/
B
lt
fau
r
r
ste
ste
public repo
A
seg
ma
ma
fix-
$ git fetch origin
remote: Counting objects: 5, done.
remote: Compressing objects: 100% (2/2), done.
D F
remote: Total 3 (delta 0), reused 0 (delta 0)
C E
Unpacking objects: 100% (3/3), done.
From /public/repo
B
E..F fix-segfault -> origin/fix-segfault
local repo
A
60
61. checkout tracking branch
t
aul
r
ste
f
seg
ma
fix-
“topic branch” “tracking branches” D F
C E
origin/
B
lt
fau
r
r
rk
ste
ste
public repo
A
seg
wo
ma
ma
fix-
$ git checkout -b work
origin/fix-segfault
D F
Branch work set up to track remote branch
refs/remotes/origin/fix-segfault.
C E
Switched to a new branch quot;workquot;
B
local repo
A
61
62. commit topic branch
t
aul
r
ste
f
seg
ma
fix-
D F
origin/
C E
lt
fau
r
r
rk
ste
ste
public repo
B
seg
wo
ma
ma
fix-
$ $EDITOR
$ git commit -a -m quot;Another fixquot;
G
[...]
D F
C E
local repo
B
62
63. push new commit
t
aul
r
ste
f
seg
ma
fix-
G
D F
origin/
C E
lt
fau
r
r
rk
ste
ste
public repo
B
seg
wo
ma
ma
fix-
$ git push origin work:fix-segfault
Total 0 (delta 0), reused 0 (delta 0)
G To /public/repo
F..G work -> feature-X
D F
C E
local repo
B
63
64. public repository updated
t
aul
r
ste
f
seg
ma
fix-
H
G
origin/
D F
lt
fau
r
r
rk
ste
ste
public repo
C E
seg
wo
ma
ma
fix-
G
D F
C E
local repo
B
64
65. fetch update
t
aul
r
ste
f
seg
ma
fix-
H
G
origin/
D F
lt
fau
r
r
rk
ste
ste
public repo
C E
seg
wo
ma
ma
fix-
$ git fetch origin
[...]
H
From /public/repo
G..H fix-segfault -> origin/fix-segfault
G
D F
local repo
C
65
66. pull to merge update
t
aul
r
ste
f
seg
ma
fix-
H
G
origin/
D F
lt
fau
r
r
rk
ste
ste
public repo
C E
seg
wo
ma
ma
fix-
$ git pull origin
Updating G..H
H
Fast forward
README | 1 +
G
1 files changed, 1 insertions(+), 0
deletions(-)
D F
local repo
C
66
67. Basic multi-repo commands
• git clone: init a copy of a repository
• git fetch: sync remote repo R with
local branches R/*
• git pull: fetch + merge
• git push <repo> [<local-branch>:<remote-branch>]
67
68. ma
ste
r
wo
rk
pre
-m
e rge
ma
ste
dev/
r
ma
ste
r tes
t
bug
gy-
cra
dev
p
68
ma
ste
r
exp
eri
me
nta
l
fix-
seg
f aul
anna
t
Real-life layout
stable/
ma
ste
r
ma
ste
r
fix-
seg
fau
lt
fix-
seg
f
local repo
aul
t
stable
69. ma
ste
r
wo
rk
pre
-m
e rge
ma
ste
dev/
r
ma
ste
r tes
t
bug
gy-
cra
dev
p
git remote add anna $URL
anna/
69
ma
ste
r
exp
eri
me
nta
l
fix-
seg
f aul
anna
t
Real-life layout
stable/
ma
ste
r
ma
ste
r
fix-
seg
fau
lt
fix-
seg
f
local repo
aul
t
stable
70. ma
ste
r
wo
rk
pre
-m
e rge
ma
ste
dev/
r
ma
ste
r tes
t
bug
gy-
cra
dev
p
anna/
70
ma
git fetch anna
ste
r
exp
eri ma
me ste
nt r
al
fix- exp
seg eri
fau
lt me
nta
l
fix-
seg
f aul
anna
t
Real-life layout
stable/
ma
ste
r
ma
ste
r
fix-
seg
fau
lt
fix-
seg
f
local repo
aul
t
stable
71. ma
ste
r
not in .git/”
“just a repo,
wo
git --bare init
rk
pre
-m
mine
e rge
ma
ste
dev/
r
ma
ste
r tes
t
bug
gy-
cra
dev
p
anna/
71
ma
ste
r
exp
eri ma
me ste
nt r
al
fix- exp
seg eri
fau
lt me
nta
l
fix-
seg
f aul
anna
t
Real-life layout
stable/
ma
ste
r
ma
ste
r
fix-
seg
fau
lt
fix-
seg
f
local repo
aul
t
stable
72. ma
ste
r
wo
rk
pre
-m
mine
e rge
ma
ste
dev/
r
ma
ste
r tes
t
git remote add mine $URL
bug
gy-
cra
dev
p
anna/
72
ma
ste
r
exp
eri ma
me ste
nt r
al
fix- exp
seg eri
fau
lt me
nta
l
fix-
seg
f aul
anna
t
Real-life layout
stable/
ma
ste
r
ma
ste
r
fix-
seg
fau
lt
fix-
seg
f
local repo
aul
t
stable
73. ma
ste
r
ma
ste
r
wo
rk
pre
-m
erg
e
pre
-m
mine
e rge
ma
mine/
ste
ma r
ste
r
tes
t
pre
-m
e rge bug
gy-
cra
dev
p
73
dev/
ma
ma ste
r
ste
r
exp
eri
me
git push mine work:master pre-merge
nta
l
fix-
anna/
seg
f
ma aul
anna
t
Real-life layout
ste
r
exp
eri
me
nt al
ma
fix- ste
“remotes”
r
seg
fau
lt
fix-
seg
f aul
t
stable
stable/
local repo
ma
ste
r
74. git remote
• Help track remote (i.e. other) repositories
• Pull from / push to them
• Use tracking branches like any branch (merge, rebase,
diff, log, show, etc)
74
75. e.g. XMMS2 trees
Official trees stable devel
Developer public
public
public trees theefer
tru
Local trees Joe Sixpack tru anders theefer
user maintainers developer
75
76. e.g. XMMS2 trees
stable devel
olof’s server
public
public
olof public theefer
tru
olof Joe Sixpack tru anders theefer
contributer user maintainers developer
76
78. Between the working
directory and the repository
Repository
Working
master
Directory
IDEAS
Makefile
README
src
|-- Makefile
|-- helloworld.c
`-- helloworld.h
04f087b
78
79. The Index (or “Staging Area”)
Index
IDEAS
Makefile
Repository
README
Working src
|-- Makefile
master
Directory |-- helloworld.c
`-- helloworld.h
IDEAS
Makefile
README
src
|-- Makefile
|-- helloworld.c
`-- helloworld.h
04f087b
79
80. Committing using the index
Index
IDEAS
Makefile
Repository
README
Working src
|-- Makefile
master
Directory |-- helloworld.c
`-- helloworld.h
IDEAS
Makefile
README
src
|-- Makefile
|-- helloworld.c
`-- helloworld.h
git add/rm git commit
04f087b
80
81. Bypassing the Index
Index
IDEAS
Makefile
Repository
README
Working src
|-- Makefile
master
Directory |-- helloworld.c
`-- helloworld.h
IDEAS
Makefile
README
src
|-- Makefile
|-- helloworld.c
`-- helloworld.h
04f087b
git commit -a
81
82. new file
Index
IDEAS
Makefile
Repository
README
Working src
|-- Makefile
master
Directory |-- helloworld.c
`-- helloworld.h
IDEAS
Makefile
README
src
|-- Makefile
|-- helloworld.c
`-- helloworld.h
test.c <- new file
TODO <- new file
04f087b
82
83. $ git status
new file
# On branch master
# Changed but not updated:
# (use quot;git add <file>...quot; to update
what will be committed)
#
# modified: test.c
# modified: TODO
Index #
no changes added to commit (use
quot;git addquot; and/or quot;git commit -aquot;)
IDEAS
Makefile
Repository
README
Working src
|-- Makefile
master
Directory |-- helloworld.c
`-- helloworld.h
IDEAS
Makefile
README
src
|-- Makefile
|-- helloworld.c
`-- helloworld.h
test.c <- new file
TODO <- new file
04f087b
83
84. new file: add to index
Index
IDEAS
Makefile
Repository
README
Working src
|-- Makefile
master
Directory |-- helloworld.c
`-- helloworld.h
TODO <- new file
IDEAS
Makefile
README
src
|-- Makefile
|-- helloworld.c
`-- helloworld.h
test.c <- new file
TODO
git add TODO
04f087b
84
85. new file: add to index
$ git status
# On branch master
# Changes to be committed:
Index # (use quot;git reset HEAD <file>...quot; to unstage)
#
# modified: TODO
IDEAS
#
Makefile
Repository
README # Changed but not updated: [...]
Working src
|-- Makefile
master
Directory |-- helloworld.c
`-- helloworld.h
TODO <- new file
IDEAS
Makefile
README
src
|-- Makefile
|-- helloworld.c
`-- helloworld.h
test.c <- new file
TODO
git add TODO
04f087b
85
86. new file: commit index
Index
IDEAS
Makefile
Repository
README
Working src
|-- Makefile
master
Directory |-- helloworld.c
`-- helloworld.h
TODO
IDEAS
Makefile
README
src
|-- Makefile
|-- helloworld.c
`-- helloworld.h deadbeef
test.c <- new file
TODO
git commit
04f087b
86
87. edited file
Index
IDEAS
Makefile
Repository
README
Working src
|-- Makefile
master
Directory |-- helloworld.c
`-- helloworld.h
TODO
IDEAS
Makefile
README <- edited
src
|-- Makefile
|-- helloworld.c
`-- helloworld.h deadbeef
test.c <- new file
TODO
04f087b
87
88. edited file: update index
Index
IDEAS
Makefile
Repository
README <- edited
Working src
|-- Makefile
master
Directory |-- helloworld.c
`-- helloworld.h
TODO
IDEAS
Makefile
README
src
|-- Makefile
|-- helloworld.c
`-- helloworld.h deadbeef
test.c <- new file
TODO
git add README
04f087b
88
89. edited file: commit index
Index
IDEAS
Makefile
Repository
README
Working src
|-- Makefile
master
Directory |-- helloworld.c
`-- helloworld.h
TODO
IDEAS
Makefile
f4ec56
README
src
|-- Makefile
|-- helloworld.c
`-- helloworld.h deadbeef
test.c <- new file
TODO
git commit
04f087b
89
90. edited file
Index
IDEAS
Makefile
Repository
README
Working src
|-- Makefile
master
Directory |-- helloworld.c
`-- helloworld.h
TODO
IDEAS <- 3 hunks
Makefile
f4ec56
README
src
|-- Makefile
|-- helloworld.c
`-- helloworld.h deadbeef
test.c <- new file
TODO
04f087b
90
91. add selected hunks to index
Index
IDEAS <- 1 hunk
Makefile
Repository
README
Working src
|-- Makefile
master
Directory |-- helloworld.c
`-- helloworld.h
TODO
IDEAS <- 2 hunks
Makefile
f4ec56
README
src
|-- Makefile
|-- helloworld.c
`-- helloworld.h deadbeef
(also see git add --interactive)
test.c <- new file
TODO
git add -p IDEAS
04f087b
91
92. add selected hunks to index
Index
$ git add -p src/xmms/testclient.c
[...]
@@ -44,7 +44,7 @@ int main (int argc, char **argv) 1 hunk
IDEAS <-
Makefile
Repository
fd = xmms_ipc_transport_fd_get (transport);
README
Working src
|-- Makefile
- msg = xmms_ipc_msg_string_new (42, quot;korv!quot;);
master
Directory |-- helloworld.c
+ msg = xmms_ipc_msg_string_new (42, quot;apan!quot;);
`-- helloworld.h
TODO
i = 5;
IDEAS <- 2 hunks
Makefilethis hunk [y/n/a/d/j/J/?]?
Stage
f4ec56
README
src
|-- Makefile
|-- helloworld.c
`-- helloworld.h deadbeef
(also see git add --interactive)
test.c <- new file
TODO
git add -p IDEAS
04f087b
92
93. diff’ing options
Index
IDEAS <- 1 hunk
Makefile
Repository
README
Working src
|-- Makefile
master
Directory |-- helloworld.c
git diff --cached
`-- helloworld.h
TODO
IDEAS <- 2 hunks
Makefile
f4ec56
README
src git diff
|-- Makefile
|-- helloworld.c
`-- helloworld.h deadbeef
test.c <- new file
TODO
git diff HEAD
04f087b
93
94. Conflicts: marked in index
Index
IDEAS <- conflicted
Makefile
Repository
README
Working src
|-- Makefile
master
Directory |-- helloworld.c
`-- helloworld.h
TODO
IDEAS <- merged
Makefile
f4ec56
README
src
|-- Makefile
|-- helloworld.c
`-- helloworld.h deadbeef
TODO
04f087b
94
95. Reset changes: 3 options
Index
IDEAS
Makefile
Repository
README
Working src
|-- Makefile
master
Directory |-- helloworld.c
`-- helloworld.h
TODO
IDEAS
Makefile
f4ec56
README
src
|-- Makefile
|-- helloworld.c
`-- helloworld.h deadbeef
TODO
04f087b
95
96. reset --hard
reset the repository
HEAD, the index
Index
and the working deadbeef
directory. IDEAS
Makefile
Repository
README
Working src
|-- Makefile
master
Directory |-- helloworld.c
`-- helloworld.h
deadbeef TODO
IDEAS
Makefile
f4ec56
README
src
|-- Makefile
|-- helloworld.c
`-- helloworld.h git reset --hard deadbeef deadbeef
TODO
Warning: any uncommitted
04f087b
change is lost!
96
97. reset --soft
only reset the
repository HEAD.
Index
f4ec56
IDEAS
Makefile
Repository
README
Working src
|-- Makefile
master
Directory |-- helloworld.c
`-- helloworld.h
f4ec56 TODO
IDEAS
Makefile
f4ec56
README
src
|-- Makefile
|-- helloworld.c
`-- helloworld.h deadbeef
git reset --soft deadbeef
TODO
Previous commit becomes
04f087b
“changes to be committed”.
97
98. reset --mixed (default)
reset the repository
HEAD and the
Index
index, but not the deadbeef
working directory. IDEAS
Makefile
Repository
README
Working src
|-- Makefile
master
Directory |-- helloworld.c
`-- helloworld.h
f4ec56 TODO
IDEAS
Makefile
f4ec56
README
src
|-- Makefile
|-- helloworld.c
`-- helloworld.h deadbeef
git reset --mixed deadbeef
TODO
Keep changes but not
04f087b
marked for commit.
98
99. Using the Index (or not)
• “Invisible” area to prepare commits
• Interact with:
- working directory (add, rm)
- repository (reset, commit)
• Analyze state (status, diff)
• Can usually be bypassed (using -a)
• Warning:
- Can commit files in inconsistent state
(e.g. not tested)
99
101. Rewrite history!
• Changing the history = point to a new history
• Correct mistakes
• Cleanup messy iterative development
• Warning:
- Changes commit ids
- (Possibly) creates conflicts if when people
merge the rewritten branch
- Best used locally, not on an important tree
101
102. Rewrite history! (1)
• git commit --amend
r
ste
ma
• “Replace the latest commit by
the current state of the index” K
D
• Allows to keep or edit the C
commit message
B
A
102
103. Rewrite history! (1)
• git commit --amend
r
ste
ma
• “Replace the latest commit by
the current state of the index” D K
• Allows to keep or edit the C
commit message
B
A
103
104. Rewrite history! (2)
• git rebase --interactive
r
ste
ma
• Rewrite a sequence of commits:
- omit
F
- squash with parent E
- edit D
C
B
A
104
105. Rewrite history! (2)
• git rebase --interactive
r
ste
ma
• Rewrite a sequence of commits:
- omit
F T (E+F)
- squash with parent E
- edit D S (D)
C
B B
A
105
106. Rewrite history! (3)
• git filter-branch
• Rewrite history using functions/scripts
• Edit author infos, commit message,
directories, commits, tags, etc.
• RTFM...
106
107. git bisect
• Useful to find the commit that
introduces a problem
r
ste
ma
• git bisect start <bad> [<good>] HEAD
H
D
C
B
A
107
108. git bisect
• Useful to find the commit that
introduces a problem
r
ste
ma
• git bisect start <bad> [<good>] H
• git bisect bag|good D
C
B
HEAD
A
108
109. git bisect
• Useful to find the commit that
introduces a problem
r
ste
ma
• git bisect start <bad> [<good>] H
• git bisect bag|good D
HEAD
C
B
A
109
110. git bisect
• Useful to find the commit that
introduces a problem
r
ste
ma
• git bisect start <bad> [<good>] H
• git bisect bag|good HEAD
D
C
B
A
110
112. git stash
• git stash [save [message]]
r
ste
• git stash apply
ma
• git stash pop # apply and drop
modifs
• Can also preserve the state of
the index
112
113. git stash
• git stash [save [message]]
{0}
r
ste
sh@
• git stash apply
ma
sta
• git stash pop # apply and drop
modifs
• Can also preserve the state of
the index
113
114. git stash
• git stash [save [message]]
{0}
r
ste
sh@
• git stash apply
ma
sta
• git stash pop # apply and drop
modifs
• Can also preserve the state of
the index
114
115. git stash
• git stash [save [message]]
{0}
r
ste
sh@
• git stash apply
ma
sta
• git stash pop # apply and drop
modifs
modifs
• Can also preserve the state of
the index
115