-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
3186 lines (2972 loc) · 133 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
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
312
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
349
350
351
352
353
354
355
356
357
358
359
360
361
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
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html>
<html lang="en">
<head>
<title>No BS Git</title>
<!-- Meta -->
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width,initial-scale=1" />
<meta name="description" content="Learn Git once and for all." />
<meta name="handheldfriendly" content="true" />
<meta name="theme-color" content="#e84e31" />
<!-- OG -->
<meta property="og:title" content="No BS Git" />
<meta property="og:description" content="Learn Git once and for all." />
<meta
property="og:image"
content="https://maciejb2k.github.io/no-bs-git/assets/opengraph.jpg"
/>
<meta property="og:url" content="https://maciejb2k.github.io/no-bs-git/" />
<meta property="og:type" content="website" />
<!-- Twitter -->
<meta name="twitter:title" content="No BS Git" />
<meta name="twitter:description" content="Learn Git once and for all." />
<meta
name="twitter:image"
content="https://maciejb2k.github.io/no-bs-git/assets/opengraph.jpg"
/>
<meta name="twitter:card" content="summary_large_image" />
<meta property="twitter:domain" content="maciejb2k.github.io" />
<!-- Favicon -->
<link rel="icon" type="image/x-icon" href="assets/favicon.ico" />
<!-- Google Fonts -->
<link rel="preconnect" href="https://fonts.googleapis.com" />
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin />
<link
href="https://fonts.googleapis.com/css2?family=Open+Sans:wght@400;600;700&family=Fira+Code&display=swap"
rel="stylesheet"
/>
<!-- Styles -->
<link rel="stylesheet" href="css/style.css" />
</head>
<body>
<script>
const currentTheme = localStorage.getItem("theme");
if (currentTheme == "dark") {
document.body.classList.add("dark-theme");
}
</script>
<div class="mobile-nav">
<button class="mobile-nav__close">
<svg
class="h-6 w-6"
xmlns="http://www.w3.org/2000/svg"
viewBox="0 0 24 24"
stroke="currentColor"
aria-hidden="true"
>
<path
stroke-linecap="round"
stroke-linejoin="round"
stroke-width="2"
d="M6 18L18 6M6 6l12 12"
/>
</svg>
</button>
<p class="nav__title">On this page:</p>
<ul class="nav-list">
<li>
<a href="#" class="nav-link">Home</a>
</li>
</ul>
</div>
<div class="menu-background"></div>
<header class="header">
<div class="header__left">
<a href="#">
<img
src="assets/logo_color.png"
alt="text with name of the website"
class="header__logo"
/>
</a>
</div>
<div class="header__right">
<a
href="https://github.com/maciejb2k/no-bs-git"
target="_blank"
class="github-button"
>
<svg
xmlns="http://www.w3.org/2000/svg"
width="20"
height="20"
fill="currentColor"
viewBox="0 0 1792 1792"
>
<path
d="M896 128q209 0 385.5 103t279.5 279.5 103 385.5q0 251-146.5 451.5t-378.5 277.5q-27 5-40-7t-13-30q0-3 .5-76.5t.5-134.5q0-97-52-142 57-6 102.5-18t94-39 81-66.5 53-105 20.5-150.5q0-119-79-206 37-91-8-204-28-9-81 11t-92 44l-38 24q-93-26-192-26t-192 26q-16-11-42.5-27t-83.5-38.5-85-13.5q-45 113-8 204-79 87-79 206 0 85 20.5 150t52.5 105 80.5 67 94 39 102.5 18q-39 36-49 103-21 10-45 15t-57 5-65.5-21.5-55.5-62.5q-19-32-48.5-52t-49.5-24l-20-3q-21 0-29 4.5t-5 11.5 9 14 13 12l7 5q22 10 43.5 38t31.5 51l10 23q13 38 44 61.5t67 30 69.5 7 55.5-3.5l23-4q0 38 .5 88.5t.5 54.5q0 18-13 30t-40 7q-232-77-378.5-277.5t-146.5-451.5q0-209 103-385.5t279.5-279.5 385.5-103zm-477 1103q3-7-7-12-10-3-13 2-3 7 7 12 9 6 13-2zm31 34q7-5-2-16-10-9-16-3-7 5 2 16 10 10 16 3zm30 45q9-7 0-19-8-13-17-6-9 5 0 18t17 7zm42 42q8-8-4-19-12-12-20-3-9 8 4 19 12 12 20 3zm57 25q3-11-13-16-15-4-19 7t13 15q15 6 19-6zm63 5q0-13-17-11-16 0-16 11 0 13 17 11 16 0 16-11zm58-10q-2-11-18-9-16 3-14 15t18 8 14-14z"
></path>
</svg>
<span>GitHub</span>
</a>
<button class="btn-toggle">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" width="20" height="20">
<path d="M10 7C10 10.866 13.134 14 17 14C18.9584 14 20.729 13.1957 21.9995 11.8995C22 11.933 22 11.9665 22 12C22 17.5228 17.5228 22 12 22C6.47715 22 2 17.5228 2 12C2 6.47715 6.47715 2 12 2C12.0335 2 12.067 2 12.1005 2.00049C10.8043 3.27098 10 5.04157 10 7ZM4 12C4 16.4183 7.58172 20 12 20C15.0583 20 17.7158 18.2839 19.062 15.7621C18.3945 15.9187 17.7035 16 17 16C12.0294 16 8 11.9706 8 7C8 6.29648 8.08133 5.60547 8.2379 4.938C5.71611 6.28423 4 8.9417 4 12Z"></path>
</svg>
</button>
<button class="nav-toggle-button">
<svg
xmlns="http://www.w3.org/2000/svg"
width="20"
height="20"
viewBox="0 0 1792 1792"
>
<path
d="M1664 1344v128q0 26-19 45t-45 19h-1536q-26 0-45-19t-19-45v-128q0-26 19-45t45-19h1536q26 0 45 19t19 45zm0-512v128q0 26-19 45t-45 19h-1536q-26 0-45-19t-19-45v-128q0-26 19-45t45-19h1536q26 0 45 19t19 45zm0-512v128q0 26-19 45t-45 19h-1536q-26 0-45-19t-19-45v-128q0-26 19-45t45-19h1536q26 0 45 19t19 45z"
></path>
</svg>
</button>
</div>
</header>
<div class="container">
<div class="wrapper">
<aside class="nav">
<p class="nav__title">On this page:</p>
<ul class="nav-list">
<li>
<a href="#" class="nav-link">Home</a>
</li>
</ul>
</aside>
<div class="content">
<main>
<div class="group">
<h2 class="heading heading--h2">Boring preface</h2>
<p class="text">
<b>Git is hard for beginners</b>. Do you have similar feelings?
I think most of you can agree with that. Git was very hard for
me, and after months of struggling with Git, I finally found out
why.
</p>
<p class="text">
<b
>In my opinion, the problem is about lack of a full
understanding of a
<span class="code">.git/</span> directory.</b
>
</p>
<p class="text">
<b>Git is not a blackbox, which somehow works</b>. By uneveiling
the mystery behind the
<span class="code">.git/</span> directory, you will be able to
freely use Git in your projects without worries.
</p>
</div>
<div class="group">
<h2 class="heading heading--h2">Before we start</h2>
<p class="text">
<a href="https://git-scm.com/downloads" target="_blank"
>Install Git</a
>
and make sure it works by typing:
<span class="code">git --version</span>
</p>
<p class="text">
<b>You have to learn how to use Git from the command line</b>.
If you want to understand it, you have to use it from the
command line.
</p>
<p class="text">
This guide assumes that
<b>you are familiar with the basics of Git</b>, but it's often
unclear to you how Git actually works, and how to deal with the
common Git problems.
</p>
</div>
<div class="group">
<h2 class="heading heading--h2">Creating a Git repository</h2>
<p class="text">
<span class="code">git init</span> creates a hidden
<span class="code">.git/</span>
directory in the current location.
</p>
<p class="text">
Git determines whether you are inside a Git repository by,
checking if there is a <span class="code">.git/</span> directory
at the root of the directory.
</p>
<p class="text text--label">
Take a look what is inside
<span class="code">.git/</span> directory:
</p>
<pre><code class="code-block language-bash">$ cd .git/ && ls -al
total 32K
-rw-r--r-- 1 maciejb maciejb 23 Sep 8 13:19 HEAD
drwxr-xr-x 2 maciejb maciejb 4.0K Sep 8 13:19 branches
-rw-r--r-- 1 maciejb maciejb 92 Sep 8 13:19 config
-rw-r--r-- 1 maciejb maciejb 73 Sep 8 13:19 description
drwxr-xr-x 2 maciejb maciejb 4.0K Sep 8 13:19 hooks
drwxr-xr-x 2 maciejb maciejb 4.0K Sep 8 13:19 info
drwxr-xr-x 4 maciejb maciejb 4.0K Sep 8 13:19 objects
drwxr-xr-x 4 maciejb maciejb 4.0K Sep 8 13:19 refs</code></pre>
<p class="text">
Familirize yourself with these files and directories and don't
do anything else for now. We will be focusing on the
<span class="code">objects/</span> and
<span class="code">refs/</span> directories in the next parts.
</p>
</div>
<div class="group">
<h2 class="heading heading--h2">SHA-1</h2>
<p class="text">Everything in Git is based on <b>SHA-1</b>.</p>
<p class="text">
It is a
<a href="https://en.wikipedia.org/wiki/SHA-1" target="_blank"
>cryptographic hash function</a
>. You don't have to know how it mathematically works. You have
to remember, that it takes any piece of text and turns it into
<b>unique 40-character string of letters and numbers</b>. It's
impossible to get back the original text from this unique
string.
</p>
<div class="form">
<div class="form__group">
<label for="sha1-input" class="form__label">Input:</label>
<input
class="form__input"
id="sha1-input"
type="text"
value="can horses swim?"
/>
</div>
<div class="form__group">
<label for="sha1-output" class="form__label"
>SHA-1 Output:</label
>
<input
class="form__input"
id="sha1-output"
type="text"
disabled
/>
</div>
</div>
<p class="text">
If you are wondering, what if SHA-1 will produce the same output
for two different inputs, you can read about
<a
href="https://en.wikipedia.org/wiki/Hash_collision"
target="_blank"
>collisions of hash functions</a
>.
</p>
<p class="text">
<b>Spoiler</b>: You will find that the probability of collision
is extremly low. Also, you will learn that SHA-1 is not secure
anymore, but it doesn't matter, because it is not used for
security in Git.
</p>
</div>
<div class="group">
<h2 class="heading heading--h2">The primary role of Git</h2>
<p class="text">
At the lowest conceptual level, Git is just a simple
<b>key:value</b> store.
</p>
<p class="text">
It calculates the SHA-1 hash of the given input, which may be a
text file, photo, pdf or anything else. Then it stores the input
under the name of the calculated SHA-1 hash inside the
<span class="code">.git/objects/</span> directory.
</p>
<p class="text">
This explanation is very simplified, but it is enough for now,
just to understand the core concept of Git.
</p>
</div>
<div class="group">
<h2 class="heading heading--h2">Calculate SHA-1 in Git</h2>
<p class="text">
We can use one of the internal Git commands to calculate the
SHA-1 hash of the given input with the
<span class="code">git hash-object</span> command:
</p>
<p class="text text--label">You can try it by yourself:</p>
<pre><code class="code-block language-bash">$ echo -n "can horses swim?" | git hash-object --stdin
4e91d48941493c77aa18db2cf7661c1fcb4b6ee8</code></pre>
<p class="text text--label">
Another example with the text file:
</p>
<pre><code class="code-block language-bash">$ echo -n "can horses swim?" > file.txt
$ git hash-object file.txt
4e91d48941493c77aa18db2cf7661c1fcb4b6ee8</code></pre>
<p class="text">
<b>Notice</b>: If you try to calculate SHA-1 from the same text
using the hash calculation form from above and the
<span class="code">git hash-object</span> command, you will
notice that <b>the results are different</b>. Why is that?
</p>
<p class="text">
To explain this behaviour, we need to introduce some new terms.
</p>
</div>
<div class="group">
<h2 class="heading heading--h2">Types of objects in Git</h2>
<p class="text">
Everything in Git is expressed with "internal git objects".
</p>
<p class="text">
Git has four types of objects:
<span class="code">blob</span>, <span class="code">tree</span>,
<span class="code">commit</span> and
<span class="code">tag</span>.
</p>
</div>
<div class="group">
<h2 class="heading heading--h2">Blob</h2>
<p class="text">Blob is a <b>generic piece of content</b>.</p>
<p class="text">
If you are using Git in your projects, your files with code are
stored as Blobs inside the Git repository.
</p>
<p class="text">
Blob is just a file, which is stored in the
<span class="code">.git/objects/</span> directory.
</p>
<p class="text text--label">Blob is structured as follows:</p>
<pre><code class="code-block language-bash">header + content</code></pre>
<p class="text text--label">
The <b>header</b> consists of the object type, which in our case
is a blob, a single whitespace, its content size in bytes and a
NULL byte:
</p>
<pre><code class="code-block language-bash"><object type> <size of content in bytes>\0
# Example header
blob 12\0</code></pre>
<p class="text">
The <b>content</b> is our input data (stdin, content of a file,
etc.), which we want to store in the Git repository.
</p>
</div>
<div class="group">
<h2 class="heading heading--h2">Creating a Blob</h2>
<p class="text text--label">
For instance, we want to create a Blob from the previous
<span class="code">file.txt</span>:
</p>
<pre><code class="code-block language-bash">can horses swim?</code></pre>
<p class="text text--label">
The blob will look like this (header + content):
</p>
<pre><code class="code-block language-bash">blob 17\0can horses swim?</code></pre>
<p class="text text--label">
The <span class="code">git hash-object -w</span> command with a
<span class="code">-w</span> flag creates and writes the blob to
the <span class="code">.git/objects</span> directory.
</p>
<pre><code class="code-block language-bash">$ mkdir test-git-blobs
$ cd test-git-blobs
$ git init
$ echo -n "can horses swim?" > file.txt
$ git hash-object -w file.txt
4e91d48941493c77aa18db2cf7661c1fcb4b6ee8</code></pre>
<p class="text">
<b>Exercise:</b> Go ahead, and find that newly created blob
inside the <span class="code">.git/objects</span> directory. Try
to view the content of the blob.
</p>
<p class="text">
You can play around with creating the blob using this form:
</p>
<div class="form">
<div class="form__group">
<label for="blob-input" class="form__label"
>File content:</label
>
<input
class="form__input"
id="blob-input"
type="text"
value="can horses swim?"
/>
</div>
<div class="form__group">
<label for="blob-sha1" class="form__label">SHA-1:</label>
<input
class="form__input"
id="blob-sha1"
type="text"
disabled
/>
</div>
<div class="form__group">
<label for="blob-content" class="form__label"
>Blob (header + content):</label
>
<textarea
class="form__input"
name=""
id="blob-content"
cols="30"
rows="5"
disabled
></textarea>
</div>
<div class="form__group">
<label for="blob-compressed" class="form__label"
>Compressed Blob (header + content) in HEX:</label
>
<textarea
class="form__input"
name=""
id="blob-compressed"
cols="30"
rows="5"
disabled
></textarea>
</div>
</div>
</div>
<div class="group">
<h2 class="heading heading--h2">Compressing the Blob</h2>
<p class="text">
As you may notice in the form above, there is an extra field
with <b>compression</b>.
</p>
<p class="text">
Git is smart enough to <b>compress</b> the content of the blob
before it is stored in the
<span class="code">.git/objects/</span> directory.
</p>
<p class="text">
Git uses the
<a href="https://en.wikipedia.org/wiki/Deflate" target="_blank"
>Deflate</a
>
data compression algorithm.
</p>
<p class="text">
When Git saves the blob, it calculates the SHA-1 of the
<b>raw blow</b>
(header + content), then <b>compresses the blob</b> to a raw
bytes and finally saves them under the name of hash:
</p>
</div>
<div class="group">
<h2 class="heading heading--h2">How blobs are stored</h2>
<p class="text text--label">
Git groups the blobs in the directories by the
<b>first two characters</b> of the blob. Our blob starts with
the <span class="code">4e</span> prefix, so it is stored in the
<span class="code">.git/objects/4e</span> directory:
</p>
<pre><code class="code-block language-bash">$ ls -l .git/objects/
total 12K
drwxr-xr-x 2 maciejb maciejb 4.0K Sep 9 09:01 4e
drwxr-xr-x 2 maciejb maciejb 4.0K Sep 8 13:19 info
drwxr-xr-x 2 maciejb maciejb 4.0K Sep 8 13:19 pack
$ ls -l .git/objects/4e
total 4.0K
-r--r--r-- 1 maciejb maciejb 32 Sep 9 09:01 91d48941493c77aa18db2cf7661c1fcb4b6ee8</code></pre>
<p class="text text--label">
If you try to view the content of the file with the
<span class="code">cat</span> command, you will get some random
characters, se we will output hex bytes, the same way as in the
form above with the <span class="code">hexdump</span> command:
</p>
<pre><code class="code-block language-bash">$ hexdump -v -e '1/1 "%02x"' 91d48941493c77aa18db2cf7661c1fcb4b6ee8
78014bcac94f52303463484ecc53c8c82f2a4e2d56282ecfccb5070061a2082c</code></pre>
<p class="text">
Of course nobody views the blobs in the Git repository like
this, I want to demonstrate that the compression works. You can
compare the results to the compression bytes from the form
above.
</p>
</div>
<div class="group">
<h2 class="heading heading--h2">Viewing the Blob</h2>
<p class="text text--label"></p>
<p class="text">
To decompress and view <b>any Git object</b>, in our case the
<b>Blob</b>, you should use the
<span class="code">git cat-file <SHA-1></span> command:
</p>
<p class="text">
<b>Notice:</b> The command requires the SHA-1 hash of the blob,
not the file name from the filesystem. You can provide only a
few first characters of SHA-1 hash, as long as it is unique.
</p>
<p class="text text--label">
The
<span class="code">git cat-file</span> with
<span class="code">-p</span> flag prints the content of the
object, and the <span class="code">-t</span> flag prints the
type of the object:
</p>
<pre><code class="code-block language-bash"># Full SHA-1 hash
$ git cat-file -p 4e91d48941493c77aa18db2cf7661c1fcb4b6ee8
can horses swim?
$ git cat-file -t 4e91d48941493c77aa18db2cf7661c1fcb4b6ee8
blob
# Short SHA-1 hash
$ git cat-file -p 4e91d489414
can horses swim?
$ git cat-file -t 4e91d4
blob</code></pre>
<p class="text">
Now you are familiar with the
<span class="code">blob</span> object.
</p>
<p class="text">
<b>Notice:</b> The <span class="code">blob</span> object
introduced new problem - we have lost the <b>filename</b>,
because the SHA-1 is only calculated from the content of the
file, the filenames is ignored and the SHA-1 hash becomes the
filename.
</p>
<p class="text">
You will understand the solution to this problem when we start
discussing <span class="code">tree</span> objects.
</p>
</div>
<div class="group">
<h2 class="heading heading--h2">Note on git objects</h2>
<p class="text">
Each Git object is structured, compressed and stored (<span
class="code"
>.git/objects</span
>) in the same way.
</p>
<p class="text text--label">
The only difference is in the
<b>object name</b> in the header, and in the <b>content</b>:
</p>
<pre><code class="code-block language-plain"># Object stucture
header + content
# Blob
blob <content size in bytes>\0<content>
# Tree
tree <content size in bytes>\0<content>
# Commit
commit <content size in bytes>\0<content>
# Tag
tag <content size in bytes>\0<content></code></pre>
</div>
<div class="group">
<h2 class="heading heading--h2">Commits</h2>
<p class="text">
<b>Commit</b> is a <b>snapshot</b> of your project at a given
time.
</p>
<p class="text">
<span class="code">commit</span> is a second object type in Git.
</p>
<p class="text">
Commits are stored in the
<span class="code">.git/objects</span> directory.
</p>
<p class="text">
To explain the <span class="code">commit</span> object, we need
to prepare some files and directories in our git repository.
</p>
<p class="text text-label">
Create a new directory and follow the instructions. Don't worry
if you don't know the commands, we will cover them later.
</p>
<pre><code class="code-block language-bash">$ mkdir git-commits
$ cd git-commits
$ git init
$ mkdir dir1 dir2
$ echo -n "new file 1" > dir1/file1.txt
$ echo -n "new file 2" > dir2/file2.txt
$ echo -n "new file 3" > file3.txt
$ git add .
$ git commit -m "Init commit"
$ echo -n "new file 4" > file4.txt
$ git add .
$ git commit -m "Second commit"
$ git log
$ q
</code></pre>
<p class="text text--label">What happened:</p>
<ul class="list">
<li class="list__item">
the command <span class="code">git add .</span> marked the new
files as ready to commit
</li>
<li class="list__item">
the command
<span class="code">git commit -m "Init commit"</span> saved
them to the git repository with message "Init commit".
</li>
<li class="list__item">
the command
<span class="code">git log</span> displayed list of commits
</li>
</ul>
<p class="text text--label">
Take a look at the <span class="code">git log</span> output:
</p>
<pre><code class="code-block language-bash">$ git log
commit 6bef83cf8560f8fc4994279afae86635cf1c394c (HEAD -> master)
Author: Maciej Biel <[email protected]>
Date: Sun Sep 10 10:01:13 2023 +0200
Second commit
commit 5d8da6e51bc54151677ba373cea0e13c9129eb6a (HEAD -> master)
Author: Maciej Biel <[email protected]>
Date: Sun Sep 10 09:04:50 2023 +0200
Init commit</code></pre>
<p class="text">
Do you remember the
<span class="code">git cat-file</span> command from the previous
sections?
</p>
</div>
<div class="group">
<h2 class="heading heading--h2">Inspecting the commit</h2>
<p class="text">
With the <span class="code">git cat-file</span> command we can
inspect any object in the Git repository.
</p>
<p class="text text--label">
Copy the SHA-1 of the commits from the
<span class="code">git log</span> command and inspect the type
of these objects:
</p>
<pre><code class="code-block language-plain">$ git cat-file -t 6bef83cf85
commit
$ git cat-file -t 5d8da6e51b
commit</code></pre>
<p class="text text--label">
Let's print the content of the commits:
</p>
<pre><code class="code-block language-plain">$ git cat-file -p 6bef83cf85
tree 01d7ad59ab92e3efe29f4347fe0b996d7188f580
parent 5d8da6e51bc54151677ba373cea0e13c9129eb6a
author Maciej Biel <[email protected]> 1694332873 +0200
committer Maciej Biel <[email protected]> 1694332873 +0200
Second commit
$ git cat-file -p 5d8da6e51b
tree 384a95a8d3539872c6eab82dedf3ad0cc2cf11c2
author Maciej Biel <[email protected]> 1694329490 +0200
committer Maciej Biel <[email protected]> 1694329490 +0200
Init commit</code></pre>
</div>
<div class="group">
<h2 class="heading heading--h2">Structure of the commit</h2>
<p class="text">Commits are always structured in the same way:</p>
<ul class="list">
<li class="list__item">
bunch of headers like: <span class="code">tree</span>,
<span class="code">parent</span>,
<span class="code">author</span>, etc.
</li>
<li class="list__item">newline <span class="code">\n</span></li>
<li class="list__item">commit message</li>
</ul>
</div>
<div class="group">
<h2 class="heading heading--h2">Parents of the commit</h2>
<pre><code class="code-block language-plain">parent 5d8da6e51bc54151677ba373cea0e13c9129eb6a</code></pre>
<p class="text">
The commit may have zero or multiple
<span class="code">parent</span> headers.
</p>
<p class="text">
When the commit is <b>the first commit</b> in the Git
repository, it doesn't have a
<span class="code">parent</span> header (<span class="code"
>5d8da6e51b</span
>
from previous section). It is the only commit in the repository
without a <span class="code">parent</span> header.
</p>
<p class="text">
Each next commit <b>must reference</b> it's previous commit
using a <span class="code">parent</span> header and SHA-1 of the
previous commit.
</p>
<p class="text">
When the commit has multiple parents, it means that it is a
<b>merge commit</b>, and it was created from multiple previous
commits. We will talk about merging strategies later.
</p>
<p class="text">
Commits are chained together using the
<span class="code">parent</span> header. That means, Git history
is not a <b>tree</b>, but a <b>DAG</b> (Directed Acyclic Graph).
</p>
</div>
<div class="group">
<h2 class="heading heading--h2">
Author and commiter of the commit
</h2>
<pre><code class="code-block language-plain">author Maciej Biel <[email protected]> 1694329490 +0200
committer Maciej Biel <[email protected]> 1694329490 +0200</code></pre>
<p class="text">
The <b>author</b> is the person who wrote the code changes,
while the <b>committer</b> is the one who added them to the
repository.
</p>
<p class="text">
For instance a <b>commiter</b> might be a GitHub when merging a
pull request.
</p>
</div>
<div class="group">
<h2 class="heading heading--h2">Tree</h2>
<p class="text">
The <span class="code">tree</span> is a thrid type of Git
objects.
</p>
<p class="text">
The <span class="code">tree</span> is very similar to a
<b>UNIX directory</b>.
</p>
<p class="text">
Combination of <span class="code">blobs</span> and
<span class="code">trees</span> is used to reflect the project
file structure within Git.
</p>
<p class="text">
All the content from the <span class="code">commit</span> (files
added to git during creation of commit) is stored using only
<span class="code">blobs</span> or
<span class="code">trees</span>.
</p>
<p class="text text--label">
Let's inspect the <span class="code">tree</span> header from the
<span class="code">5d8da6e51b</span> commit:
</p>
<pre><code class="code-block"># Inspecting the commit
$ git cat-file -p 5d8da6e51b
...
tree 384a95a8d3539872c6eab82dedf3ad0cc2cf11c2
...</code></pre>
<pre><code class="code-block"># Inspecting the tree type
$ git cat-file -t 384a95a8d3
tree
# Inspecting the tree
$ git cat-file -p 384a95a8d3539
040000 tree cc0fea78ece05cc37dae4d302b4b3a0301595eb9 dir1
040000 tree ef56f0a20f52ca47f13c8c5a72d6b7a3a22a3534 dir2
100644 blob b037b2f50c795358d208ffb853999c7d14257773 file3.txt</code></pre>
<p class="text">
During creation of the <span class="code">commit</span>, Git
creates a <span class="code">tree</span> object, which contains
all the files and directories, starting from the root directory
of the project, and writes them to the
<span class="code">.git/objects</span> directory.
</p>
<p class="text">
The <span class="code">tree</span> object solves the problem
with <b>missing filename</b> during creation of the blob.
</p>
<p class="text text--label">
We can lookup the <span class="code">tree</span> object
corresponding to <span class="code">dir1</span> directory:
</p>
<pre><code class="code-block">$ git cat-file -p cc0fea78ece05c
100644 blob 4988b5e6c604b490401d0a1be9f6f8025b60781c file1.txt</code></pre>
<p class="text">
Blobs are always <b>leafs</b> in the tree structure.
</p>
</div>
<div class="group">
<h2 class="heading heading--h2">Structure of the tree row</h2>
<pre><code class="code-block">040000 tree cc0fea78ece05cc37dae4d302b4b3a0301595eb9 dir1</code></pre>
<p class="text text--label">
Let's take a look at the first row. It consits of:
</p>
<ul class="list">
<li class="list__item">
<span class="code">040000</span> - file mode
</li>
<li class="list__item">
<span class="code">tree</span> - object type (<span
class="code"
>tree</span
>
or <span class="code">blob</span>)
</li>
<li class="list__item">
<span class="code"
>cc0fea78ece05cc37dae4d302b4b3a0301595eb9</span
>
- SHA-1 of the saved object in the
<span class="code">.git/objects</span>
</li>
<li class="list__item">
<span class="code">dir1</span> - name of the file / directory
</li>
</ul>
</div>
<div class="group">
<h2 class="heading heading--h2">Space efficiency in commits</h2>
<p class="text">
Take a look at the SHA-1 of the
<span class="code">file3.txt</span> in the tree from first
commit and from the second commit. <b>It didn't change</b>. The
same goes for the <span class="code">dir1</span>,
<span class="code">dir2</span> directories and blobs inside
them.
</p>
<pre><code class="code-block"># Second commit
$ git cat-file -p 01d7ad59ab92e3efe2
040000 tree cc0fea78ece05cc37dae4d302b4b3a0301595eb9 dir1
040000 tree ef56f0a20f52ca47f13c8c5a72d6b7a3a22a3534 dir2
100644 blob b037b2f50c795358d208ffb853999c7d14257773 file3.txt
100644 blob 86825070f391e4be005bc2c73f91057cc7e94b28 file4.txt
# First commit tree
$ git cat-file -p 384a95a8d353987
040000 tree cc0fea78ece05cc37dae4d302b4b3a0301595eb9 dir1
040000 tree ef56f0a20f52ca47f13c8c5a72d6b7a3a22a3534 dir2
100644 blob b037b2f50c795358d208ffb853999c7d14257773 file3.txt</code></pre>
<p class="text">
<b>Git saves space</b> by referencing in the next commits
objects, which SHA-1 didn't change.
</p>
<p class="text">
For instance, if the content of
<span class="code">file3.txt</span> will change, Git wil create
a new blob with a new SHA-1, and will reference it in the next
commit.
</p>
</div>
<div class="group">
<h2 class="heading heading--h2">Commits uneveiled</h2>
<p class="text">
That's all about commits!
<b>No more magic behind the scenes</b>. You can always at any
time inspect any Git object in the repository. The only thing
you need is an SHA-1 of the object and that's it!
</p>
<p class="text text--label">
Let's summarize what we have learned to that point:
</p>
<ul class="list">
<li class="list__item">
<span class="code">.git/objects</span> is a directory, which
acts as a <b>database</b> for Git objects.
</li>
<li class="list__item">
<span class="code">blobs</span>,
<span class="code">trees</span> and
<span class="code">commits</span> are <b>object types</b> in
Git, which are stored in the
<span class="code">.git/objects</span> directory.
</li>
<li class="list__item">
<span class="code">commit</span> is a <b>snapshot</b> of your
project, has a reference to the main
<span class="code">tree</span> object, which contains whole
<b>working directory structure</b>, and a reference to a
<b>parent commit</b>.
</li>
<li class="list__item">
<span class="code">tree</span> acts as a directory, contains
other <b>trees and blobs</b> with their names.
</li>
<li class="list__item">
<span class="code">blob</span> is a generic piece of content,
used to store <b>any file</b> from the working directory.
</li>
</ul>
<img src="assets/git-object-model.png" alt="" class="image" />
<div class="image-caption">
Source:
<a
href="http://shafiul.github.io/gitbook/1_the_git_object_model.html"
target="_blank"
>Git Community Book</a
>
</div>
</div>
<div class="group">
<h2 class="heading heading--h2">Loose objects and compression</h2>
<p class="text">
Let's take a look at the
<span class="code">.git/objects</span> directory and inspect the
files after creating the commits in the previous section:
</p>
<pre><code class="code-block language-bash">ls -l .git/objects
total 48
drwxr-xr-x 2 maciejb maciejb 4096 Sep 10 10:01 01
drwxr-xr-x 2 maciejb maciejb 4096 Sep 10 09:04 38
drwxr-xr-x 2 maciejb maciejb 4096 Sep 10 09:04 49
drwxr-xr-x 2 maciejb maciejb 4096 Sep 10 09:04 5d
drwxr-xr-x 2 maciejb maciejb 4096 Sep 10 10:01 6b
drwxr-xr-x 2 maciejb maciejb 4096 Sep 10 10:01 86
drwxr-xr-x 2 maciejb maciejb 4096 Sep 10 09:04 8c
drwxr-xr-x 2 maciejb maciejb 4096 Sep 10 09:04 b0
drwxr-xr-x 2 maciejb maciejb 4096 Sep 10 09:04 cc
drwxr-xr-x 2 maciejb maciejb 4096 Sep 10 09:04 ef
drwxr-xr-x 2 maciejb maciejb 4096 Sep 10 09:04 info
drwxr-xr-x 2 maciejb maciejb 4096 Sep 10 09:04 pack</code></pre>
<p class="text">
All single files in the first ten directories, except the
<span class="code">info</span> and
<span class="code">pack</span> directories, are called
<b>loose objects</b>.
</p>
</div>
<div class="group">
<h2 class="heading heading--h2">Packfiles</h2>
<p class="text">
Every object with different SHA-1 in Git repository is stored as
a <b>separate object</b>.
</p>
<p class="text">
<b
>Dealing with lots of small files on disk is very
inefficient.</b
>
Sending these files over the network too (have you ever pushed
<span class="code">node_modules/</span> to GitHub 😂)?
</p>
<p class="text">
Let's assume, we have a very long file which takes up 100KB of
space on the disk. We commit it and then make a change in only
one character in the file, so in the next commit, the
<b>SHA-1 will change</b>.
</p>
<p class="text">
Even though <b>99.9% of the file is the same</b> as before, Git
must create a copy of the 100KB file in the next commit. This
sound very space inefficient.
</p>
<p class="text">
Fortunately, Git has a solution for this problem. To save space,
Git combines multiple objects into a single file called a
<b>packfile</b>.
</p>
</div>
<div class="group">
<h2 class="heading heading--h2">Packing objects</h2>
<p class="text">
Git has a built-in
<a href="https://git-scm.com/docs/git-gc" target="_blank"
><b>garbarge collector</b></a
>, which runs from time to time (for instance on
<span class="code">git push</span>) and packs
<b>all the loose objects</b> into a <b>single packfiles</b>.
This happens automatically, and we don't have to worry about it.
</p>
<p class="text">
For learning purposes, we will pack the objects manually. To do
that, we will use the <span class="code">git gc</span> command: