understanding-git.tex 18.2 KB
Newer Older
1
2
%\documentclass{beamer}

3
\usepackage{moy-bouveret-slides}
4
5
\usepackage{tikz}
\usetikzlibrary{shapes}
6
\usepackage{moy-bouveret-tikz}
7
\usepackage{minted}
8
9
\usepackage{comment}

10
\title{Understanding Git}
11
12
13

%\subtitle{...}

14
\author{Sylvain Bouveret, Grégory Mounié, Matthieu Moy}
15

16
\institute[first.last@imag.fr]{[first].[last]@imag.fr\\
17
\url{http://recherche.noiraudes.net/resources/git/Slides/\jobname.pdf}}
18

19
\date{2021}
20
21
22
23
24

\begin{document}
%\begin{comment}
\begin{frame}
  \titlepage
25
26
27
28

  \begin{center}
    \includegraphics[width=2cm]{images/CC-BY-SA}
  \end{center}
29
30
31
32
33
34
\end{frame}

\begin{frame}
  \frametitle{Goals of the presentation}

  \begin{itemize}
35
36
37
38
  \item Presenting the data model behind Git
  \item Showing how Git stores data and history
  \item Understanding how to navigate between the commits
    of a repository
39
40
41
42
43
44
45
  \end{itemize}
\end{frame}

\begin{frame}
  \begin{columns}
    \column{.45\textwidth}
    \begin{center}
46
      \includegraphics[height=0.7\textheight]{fig/git}
47
48
    \end{center}
    \column{.5\textwidth}
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
    \pause
    \mode<beamer>{
      \begin{tikzpicture}
        \node[rectangle callout, callout relative pointer={(-0.5, 0)},callout pointer width=0.5cm,rounded corners,fill=fg, text width=0.8\textwidth, inner sep=10pt] {
          \color{bg}\footnotesize
          If that doesn't fix it, git.txt contains the phone number
          of a friend of mine who understands git. Just wait through a few
          minutes of ``It's really pretty simple, just think of branches
          as...'' and eventually you'll learn the commands that will fix
          everything.
        };
      \end{tikzpicture}
    }
    \mode<handout>{
      If that doesn't fix it, git.txt contains the phone number
      of a friend of mine who understands git. Just wait through a few
      minutes of ``It's really pretty simple, just think of branches
      as...'' and eventually you'll learn the commands that will fix
      everything.
    }
69
70
71
72
73
74
75
76
77
78
  \end{columns}
\end{frame}

\begin{frame}
\frametitle{Why do I need to learn about Git's
    internal?}
  \begin{itemize}
  \item Beauty of Git: \alert{very} simple data model\\
    (The tool is clever, the repository format is simple\&stupid)
  \item Understand the model, and the 150+ commands will become
79
    \alert{simple}!
80
81
82
  \end{itemize}
\end{frame}

83
\section{Objects, sha1}
84
85
86

\begin{frame}[label=objects] \frametitle{Content of a Git repository: Git objects}

87
88
  \tikz[baseline] \draw ++(0,.7ex) node[gitblob] {} node[right=0.3cm, fill=fg!20!bg] {blob}; Any sequence of bytes, represents file content\\
  \tikz[baseline] \draw ++(0,.7ex) node[gittree] {} node[right=0.3cm, fill=fg!20!bg] {tree}; Associates object to pathnames, represents a directory\\
89
  \uncover<2->{
90
  \tikz[baseline] \draw ++(0,.7ex) node[gitcommit] {} node[right=0.3cm, fill=fg!20!bg] {commit}; Metadata +
91
92
93
94
95
96
97
98
99
100
101
  pointer to tree + pointer to parents
  }

  \begin{center}
    \scalebox{.7}{
    \begin{tikzpicture}
      \draw           node[gitblob](blob1) {};
      \draw ++(3,  0) node[gitblob](blob2) {};
      \draw ++(1.5,2) node[gittree](tree1) {};
      \draw ++(4.5,2) node[gitblob](blob3) {};
      \draw ++(3,4)   node[gittree](tree2) {};
102
103
104
105
      \draw[commit arc] (tree1) to node[above,sloped]{\small file1.txt} (blob1);
      \draw[commit arc] (tree1) to node[above,sloped]{\small file2.txt} (blob2);
      \draw[commit arc] (tree2) to node[above,sloped]{\small dir1.txt} (tree1);
      \draw[commit arc] (tree2) to node[above,sloped]{\small file3.txt} (blob3);
106
107
108

      \uncover<2->{
      \draw(tree2) ++(0,1.5) node[gitcommit](commit1) {};
109
      \draw[commit arc] (commit1) to node[right]{tree} (tree2);
110
111
112
113
      }

      \uncover<3->{
        \draw(commit1) ++(4,0) node[gitcommit](commit2) {};
114
        \draw[commit arc] (commit2) to node[above]{parent} (commit1);
115
116
117
118
      }

      \uncover<4->{
        \draw (commit2) ++(0,-1.5)   node[gittree](treecommit2) {};
119
120
        \draw[commit arc] (commit2) to node[right]{tree} (treecommit2);
        \draw[commit arc] (treecommit2) to node[above,sloped,near start]{\small dir1.txt}  (tree1);
121
        \draw (treecommit2) ++(1,-2) node[gitblob](blob3commit2) {};
122
        \draw[commit arc] (treecommit2) to node[above,sloped]{\small file3.txt} (blob3commit2);
123
124
125
126
      } 

      \uncover<5->{
        \draw(commit2) ++(4,0) node[gitcommit](commit3) {};
127
        \draw[commit arc] (commit3) to node[below]{parent} (commit2);
128
129
130
131
      }
        
      \uncover<6->{
        \draw(commit2) ++(4,1) node[gitcommit](commit4) {};
132
        \draw[commit arc] (commit4) to node[above,sloped]{parent} (commit2);
133
134
135
136
      }

      \uncover<7->{
        \draw(commit3) ++(4,0) node[gitcommit](mergecommit) {};
137
138
        \draw[commit arc] (mergecommit) to node[below,sloped]{parent} (commit3);
        \draw[commit arc] (mergecommit) to node[above,sloped]{parent} (commit4);
139
140
141
142
143
144
145
146
147
148
149
150
      
        \draw (commit3) ++(0,-1.5) node {...};
        \draw (mergecommit) ++(0,-1.5) node {...};
      }
    \end{tikzpicture}
  }
  \end{center}
\end{frame}

\begin{frame}[fragile]
\frametitle{Git objects: On-disk format}
  \footnotesize
151
\begin{minted}[escapeinside=||]{console}
152
$ git log
153
|commit \alert{7a7fb77be431c284f1b6d036ab9aebf646060271}|
154
155
156
157
158
159
160
161
162
Author: Matthieu Moy <Matthieu.Moy@imag.fr>
Date:   Wed Jul 2 20:13:49 2014 +0200

    Initial commit
$ find .git/objects/
.git/objects/
.git/objects/fc
.git/objects/fc/264b697de62952c9ff763b54b5b11930c9cfec
.git/objects/7a
163
|.git/objects/\alert{7a/7fb77be431c284f1b6d036ab9aebf646060271}|
164
165
166
167
.git/objects/50
.git/objects/50/a345788a8df75e0f869103a8b49cecdf95a416
.git/objects/26
.git/objects/26/27a0555f9b58632be848fee8a4602a1d61a05f
168
\end{minted}
169
170
171
172
173
\end{frame}

\begin{frame}[fragile]
\frametitle{Git objects: On-disk format}
  \footnotesize
174
\begin{minted}{shell-session}
175
176
$ echo foo > README.txt; git add README.txt 
$ git commit -m "add README.txt"
177
178
179
180
\end{minted}
\vspace{-1.2em}
\begin{minted}[escapeinside=||]{shell-session}
|{[}master \alert{5454e3b}{]} add README.txt|
181
182
183
184
185
186
187
188
189
190
191
 1 file changed, 1 insertion(+)
 create mode 100644 README.txt
$ find .git/objects/
.git/objects/
.git/objects/fc
.git/objects/fc/264b697de62952c9ff763b54b5b11930c9cfec
.git/objects/7a
.git/objects/7a/7fb77be431c284f1b6d036ab9aebf646060271
.git/objects/25
.git/objects/25/7cc5642cb1a054f08cc83f2d943e56fd3ebe99
.git/objects/54
192
|.git/objects/\alert{54/54e3b}51e81d8d9b7e807f1fc21e618880c1ac9|
193
...
194
\end{minted}
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
%$ 
\end{frame}

\begin{frame}
\frametitle{Git objects: On-disk format}
  \begin{itemize}
  \item By default, 1 object = 1 file
  \item Name of the file = object unique identifier
    content
  \item Content-addressed database:
    \begin{itemize}
    \item Identifier computed as a hash of its content
    \item Content accessible from the identifier
    \end{itemize}
  \item Consequences:
    \begin{itemize}
    \item Objects are immutable
    \item Objects with the same content have the same identity\\
      (deduplication for free)
Grégory Mounié's avatar
Grégory Mounié committed
214
    \item Previously, no known collision in SHA1, no moving to SHA-256
215
216
217
218
219
220
221
    \item Acyclic (DAG = Directed Acyclic Graph)
    \end{itemize}
  \end{itemize}
\end{frame}

\begin{frame}[fragile]
\frametitle{On-disk format: Pack files}
222
  \footnotesize
223
  \begin{minted}[escapeinside=||]{console}
224
225
226
227
228
229
230
231
232
233
234
235
236
237
$ du -sh .git/objects/
68K     .git/objects/
$ git gc
...
$ du -sh .git/objects/
24K     .git/objects/
$ find .git/objects/
.git/objects/
.git/objects/pack
.git/objects/pack/pack-f9cbdc53005a4b500934625d...a3.idx
.git/objects/pack/pack-f9cbdc53005a4b500934625d...a3.pack
.git/objects/info
.git/objects/info/packs
$
238
\end{minted}
239
\vspace{-0.5cm}
240
241
242
243
244
245
246
247
248
249
250
251
252
  \begin{center}
    \large
    $\leadsto$ More efficient format, no conceptual change\\
    (objects are still there)
  \end{center}
  %$
\end{frame}

\begin{frame}[fragile]
\frametitle{Exploring the object database}
  \begin{itemize}
  \item \texttt{\bfseries{} git cat-file -p} : pretty-print the content of an object
  \end{itemize}
253
  \vfill
254
255
256
257
258
259
260
261
262
263
264
265
  \footnotesize
  \begin{semiverbatim}
$ git log -{}-oneline
{\color{red!50!black}\bfseries{}5454e3b} add README.txt
{\color{blue!50!black}\bfseries{}7a7fb77} Initial commit
$ git cat-file -p 5454e3b
tree {\color{green!50!black}\bfseries{}59802e9b115bc606b88df4e2a83958423661d8c4}
parent {\color{blue!50!black}\bfseries{}7a7fb77}be431c284f1b6d036ab9aebf646060271
author Matthieu Moy <Matthieu.Moy@imag.fr> 1404388746 +0200
committer Matthieu Moy <Matthieu.Moy@imag.fr> 1404388746 +0200

add README.txt
266
267
268
269
270
271
272
273
274
275
276
\end{semiverbatim}
\end{frame}

\begin{frame}[fragile]
\frametitle{Exploring the object database}
  \begin{itemize}
  \item \texttt{\bfseries{} git cat-file -p} : pretty-print the content of an object
  \end{itemize}
  \vfill
  \footnotesize
  \begin{semiverbatim}
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
$ git cat-file -p {\color{green!50!black}\bfseries{}59802e9b115bc606b88df4e2a83958423661d8c4}
100644 blob {\color{violet!50!black}\bfseries{}257cc5642cb1a054f08cc83f2d943e56fd3ebe99}  README.txt
040000 tree 2627a0555f9b58632be848fee8a4602a1d61a05f  sandbox
$ git cat-file -p {\color{violet!50!black}\bfseries{}257cc5642cb1a054f08cc83f2d943e56fd3ebe99}
foo
$ printf 'blob 4\textbackslash{}0foo\textbackslash{}n' | sha1sum
{\color{violet!50!black}\bfseries{}257cc5642cb1a054f08cc83f2d943e56fd3ebe99}  -
  \end{semiverbatim}
%$
\end{frame}

\begin{frame}[fragile]
\frametitle{Merge commits in the object
    database}
  \footnotesize
  \begin{semiverbatim}
$ git checkout -b branch HEAD^
Switched to a new branch 'branch'
$ echo foo > file.txt; git add file.txt  
$ git commit -m "add file.txt"
[branch f44e9ab] add file.txt
 1 file changed, 1 insertion(+)
 create mode 100644 file.txt
$ git merge master
Merge made by the 'recursive' strategy.
 README.txt | 1 +
 1 file changed, 1 insertion(+)
 create mode 100644 README.txt
  \end{semiverbatim}
%$
\end{frame}

\begin{frame}[fragile]
\frametitle{Merge commits in the object
    database}
312
  \scriptsize
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
  \begin{semiverbatim}
$ git checkout -b branch HEAD^
$ echo foo > file.txt; git add file.txt  
$ git commit -m "add file.txt"
$ git merge master
$ git log -{}-oneline -{}-graph
*   {\color{green!50!black}\bfseries{}1a7f9ae} (HEAD, branch) Merge branch 'master' into branch
|\textbackslash
| * {\color{blue!50!black}\bfseries{}5454e3b} (master) add README.txt
* | {\color{red!50!black}\bfseries{}f44e9ab} add file.txt
|/  
* 7a7fb77 Initial commit
$ git cat-file -p {\color{green!50!black}\bfseries{}1a7f9ae}
tree 896dbd61ffc617b89eb2380cdcaffcd7c7b3e183
parent {\color{red!50!black}\bfseries{}f44e9ab}ff8918f08e91c2a8fefe328dd9006e242
parent {\color{blue!50!black}\bfseries{}5454e3b}51e81d8d9b7e807f1fc21e618880c1ac9
author Matthieu Moy <Matthieu.Moy@imag.fr> 1404390461 +0200
committer Matthieu Moy <Matthieu.Moy@imag.fr> 1404390461 +0200

Merge branch 'master' into branch
  \end{semiverbatim}
%$
\end{frame}

\begin{frame}
\frametitle{Snapshot-oriented storage}
  \begin{itemize}
  \item A commit represents \alert{exactly} the state of the project
  \item A tree represents \alert{only} the state of the project (where
    we are, not how we got there)
  \item Renames are not tracked, but re-detected on demand
  \item Diffs are computed on demand (e.g. \texttt{git diff HEAD HEAD\^{}})
  \item Physical storage still efficient
  \end{itemize}
\end{frame}

349
\section{References}
350
351
352
353
354
355
356
357
358
359

\begin{frame}[fragile]
\frametitle{Branches, tags: references}
  \begin{itemize}
  \item In Java:
\begin{lstlisting}[language=Java]
String s; // Reference named s
s = new String("foo"); // Object pointed to by s
String s2 = s; // Two refs for the same object
\end{lstlisting}
360
361
\item In Git: likewise!
\footnotesize
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
    \begin{semiverbatim}
$ git log --oneline    
{\color{blue!50!black}\bfseries{}5454e3b} add README.txt
7a7fb77 Initial commit
$ cat .git/HEAD       
ref: refs/heads/master
$ cat .git/refs/heads/master 
{\color{blue!50!black}\bfseries{}5454e3b}51e81d8d9b7e807f1fc21e618880c1ac9   
$ git symbolic-ref HEAD
refs/heads/master
$ git rev-parse refs/heads/master
{\color{blue!50!black}\bfseries{}5454e3b}51e81d8d9b7e807f1fc21e618880c1ac9
    \end{semiverbatim}
%$
  \end{itemize}
\end{frame}

\begin{frame}
\frametitle{References (refs) and objects}

  \begin{center}
    \scalebox{.7}{
    \begin{tikzpicture}
      \draw           node[gitblob](blob1) {};
      \draw ++(3,  0) node[gitblob](blob2) {};
      \draw ++(1.5,2) node[gittree](tree1) {};
      \draw ++(4.5,2) node[gitblob](blob3) {};
      \draw ++(3,4)   node[gittree](tree2) {};
390
391
392
393
      \draw[commit arc] (tree1) to node[above,sloped]{\small file1.txt} (blob1);
      \draw[commit arc] (tree1) to node[above,sloped]{\small file2.txt} (blob2);
      \draw[commit arc] (tree2) to node[above,sloped]{\small dir1.txt} (tree1);
      \draw[commit arc] (tree2) to node[above,sloped]{\small file3.txt} (blob3);
394
395
396

      \uncover<1->{
      \draw(tree2) ++(0,1.5) node[gitcommit](commit1) {};
397
      \draw[commit arc] (commit1) to node[right]{tree} (tree2);
398
399
400
401
402
403
      }

      \uncover<2->{
        \draw (commit1) ++(0,2) node[gitref] (ref1) {master};
      }
      \uncover<2>{
404
        \draw[commit arc] (ref1.east) -- (commit1);
405
406
407
408
      }

      \uncover<3->{
        \draw(commit1) ++(4,0) node[gitcommit](commit2) {};
409
        \draw[commit arc] (commit2) to node[above]{parent} (commit1);
410
411
412
      }

      \uncover<3>{
413
        \draw[commit arc,->] (ref1.east) .. controls (ref1.east -| commit2) .. (commit2);
414
415
416
417
      }

      \uncover<3->{
        \draw (commit2) ++(0,-1.5)   node[gittree](treecommit2) {};
418
419
        \draw[commit arc] (commit2) to node[right]{tree} (treecommit2);
        \draw[commit arc] (treecommit2) to node[above,sloped,near start]{\small dir1.txt}  (tree1);
420
        \draw (treecommit2) ++(1,-2) node[gitblob](blob3commit2) {};
421
        \draw[commit arc] (treecommit2) to node[above,sloped]{\small file3.txt} (blob3commit2);
422
423
424
425
      } 

      \uncover<4->{
        \draw(commit2) ++(4,0) node[gitcommit](commit3) {};
426
        \draw[commit arc] (commit3) to node[below]{parent} (commit2);
427
428
429
      }
        
      \uncover<4>{
430
        \draw[commit arc,->] (ref1.east) .. controls (ref1.east -| commit3) .. (commit3);
431
432
433
434
      }

      \uncover<5->{
        \draw(commit2) ++(4,1) node[gitcommit](commit4) {};
435
        \draw[commit arc] (commit4) to node[above,sloped]{parent} (commit2);
436
437
438
439
      }

      \uncover<5->{
        \draw(commit3) ++(4,0) node[gitcommit](mergecommit) {};
440
441
        \draw[commit arc] (mergecommit) to node[below,sloped]{parent} (commit3);
        \draw[commit arc] (mergecommit) to node[above,sloped]{parent} (commit4);
442
443
444
445
446
447
      
        \draw (commit3) ++(0,-1.5) node {...};
        \draw (mergecommit) ++(0,-1.5) node {...};
      }

      \uncover<5->{
448
        \draw[commit arc,->] (ref1.east) .. controls (ref1.east -| mergecommit) .. (mergecommit);
449
450
451
452
      }

      \uncover<6>{
        \draw (ref1) ++(-1,0) node[gitref] (head) {HEAD};
453
        \draw[commit arc] (head) -- (ref1);
454
455
456
457
458
459
460
461
462
      }
    \end{tikzpicture}
  }
  \end{center}
\end{frame}

\begin{frame}
\frametitle{Sounds Familiar?}
  \begin{center}
463
    \includegraphics[width=\textwidth]{images/gitk-branch}
464
465
466
467
468
469
470
471
472
473
474
475
476
477

    {\Huge $\approx$}
    
    \scalebox{.7}{
    \begin{tikzpicture}
      \uncover<1->{
      \draw node[gitcommit](commit1) {};
      }

      {
      }

      {
        \draw(commit1) ++(4,0) node[gitcommit](commit2) {};
478
        \draw[commit arc] (commit2) to node[above]{parent} (commit1);
479
480
481
482
      }

      {
        \draw(commit2) ++(4,0) node[gitcommit](commit3) {};
483
        \draw[commit arc] (commit3) to node[below]{parent} (commit2);
484
485
486
487
      }
        
      {
        \draw(commit2) ++(4,1) node[gitcommit](commit4) {};
488
        \draw[commit arc] (commit4) to node[above,sloped]{parent} (commit2);
489
490
491
492
      }

      {
        \draw(commit3) ++(4,0) node[gitcommit](mergecommit) {};
493
494
        \draw[commit arc] (mergecommit) to node[below,sloped]{parent} (commit3);
        \draw[commit arc] (mergecommit) to node[above,sloped]{parent} (commit4);
495
496
497
      }
      
      \draw (commit4) ++(0,1.5) node[gitref](branch) {branch};
498
      \draw[commit arc] (branch.east) -- (commit4);
499
500
501

      \draw (mergecommit) ++(0,3) node[gitref] (ref1) {master};
      \draw (ref1) ++(-1,0) node[gitref] (head) {HEAD};
502
503
      \draw[commit arc] (head) -- (ref1);
      \draw[commit arc,->] (ref1.east) .. controls (ref1.east -| mergecommit) .. (mergecommit);
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
    \end{tikzpicture}
  }
  \end{center}
\end{frame}

\begin{frame}
  \frametitle{Branches, HEAD, tags}
  \begin{itemize}
  \item A branch is a ref to a commit
  \item A lightweight tag is a ref (usually to a commit) (like a
    branch, but doesn't move)
  \item Annotated tags are objects containing a ref + a (signed) message
  \item HEAD is ``where we currently are''
    \begin{itemize}
    \item If HEAD points to a branch, the next commit will move the branch
    \item If HEAD points directly to a commit (detached HEAD), the
      next commit creates a commit not in any branch (warning!)
    \end{itemize}
  \end{itemize}
\end{frame}
524
525
526
527
528
529
530
531
532
533
534
535
536
537

\section{Branches and tags in practice}

\begin{frame}
  \frametitle{Branches and Tags in Practice}
  \begin{itemize}
  \item Create a local branch and check it out:\\
    \texttt{git checkout -b \textit{branch-name}}
  \item List local branches:\\
    \texttt{git branch}
  \item List all branches (including remote-tracking):\\
    \texttt{git branch -a}
  \item Create a tag:\\
    \texttt{git tag \textit{tag-name}}
538
539
  \item Switch to a branch, a tag, or a commit:\\
    \texttt{git checkout \textit{branch-name/tag-name/commit}}
540
541
542
543
  \end{itemize}
\end{frame}


544
545
%\end{comment}

546
% \section{More Documentation}
547

548
549
550
551
552
553
554
% \begin{frame}
% \frametitle{More Documentation}
%   \begin{itemize}
%   \item \url{http://ensiwiki.ensimag.fr/index.php/Maintenir_un_historique_propre_avec_Git}
%   \item \url{http://ensiwiki.ensimag.fr/index.php/Ecrire_de_bons_messages_de_commit_avec_Git}
%   \end{itemize}
% \end{frame}
555

556
% \section{Exercises}
557

558
559
% \begin{frame}[fragile]
%   \frametitle{Exercises}
560

561
562
563
564
565
566
567
%   \begin{itemize}
%   \item Visit \url{https://github.com/moy/dumb-project.git}
%   \item Fork it from the web interface (or just \texttt{git clone})
%   \item Clone it on your machine
%   \item Repair the dirty history!
%   \end{itemize}
% \end{frame}
568

569
570
571
572
573
% \begin{frame}[fragile]
%   \frametitle{If you liked this training ...}
%   \begin{center}
%     \Large ``Principes et utilisations de l'outil Git'', 1 day
%     training. Next iteration: April 25th 2016. Tell your friends ;-).
574
    
575
576
577
%     {\tiny \url{http://formation-continue.grenoble-inp.fr/genie-logiciel-programmation/principes-et-utilisations-de-l-outil-git-681441.kjsp#page-presentation}}
%   \end{center}
% \end{frame}
578
579
580
581
582
583
584
585

\end{document}

%%% Local Variables: 
%%% mode: latex
%%% TeX-master: t
%%% ispell-local-dictionary: "american"
%%% End: