summaryrefslogtreecommitdiff
path: root/Documentation/tutorial.txt
blob: 66b6e300a217d008de44c5be7e3f7e0f01179933 (plain)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
A short git tutorial
====================
May 2005


Introduction
------------

This is trying to be a short tutorial on setting up and using a git
archive, mainly because being hands-on and using explicit examples is
often the best way of explaining what is going on.

In normal life, most people wouldn't use the "core" git programs
directly, but rather script around them to make them more palatable. 
Understanding the core git stuff may help some people get those scripts
done, though, and it may also be instructive in helping people
understand what it is that the higher-level helper scripts are actually
doing. 

The core git is often called "plumbing", with the prettier user
interfaces on top of it called "porcelain".  You may not want to use the
plumbing directly very often, but it can be good to know what the
plumbing does for when the porcelain isn't flushing... 


Creating a git archive
----------------------

Creating a new git archive couldn't be easier: all git archives start
out empty, and the only thing you need to do is find yourself a
subdirectory that you want to use as a working tree - either an empty
one for a totally new project, or an existing working tree that you want
to import into git. 

For our first example, we're going to start a totally new archive from
scratch, with no pre-existing files, and we'll call it "git-tutorial".
To start up, create a subdirectory for it, change into that
subdirectory, and initialize the git infrastructure with "git-init-db":

	mkdir git-tutorial
	cd git-tutorial
	git-init-db 

to which git will reply

	defaulting to local storage area

which is just git's way of saying that you haven't been doing anything
strange, and that it will have created a local .git directory setup for
your new project. You will now have a ".git" directory, and you can
inspect that with "ls". For your new empty project, ls should show you
three entries:

 - a symlink called HEAD, pointing to "refs/heads/master"

   Don't worry about the fact that the file that the HEAD link points to
   doesn't even exist yet - you haven't created the commit that will
   start your HEAD development branch yet.

 - a subdirectory called "objects", which will contain all the git SHA1
   objects of your project. You should never have any real reason to
   look at the objects directly, but you might want to know that these
   objects are what contains all the real _data_ in your repository.

 - a subdirectory called "refs", which contains references to objects.

   In particular, the "refs" subdirectory will contain two other
   subdirectories, named "heads" and "tags" respectively.  They do
   exactly what their names imply: they contain references to any number
   of different "heads" of development (aka "branches"), and to any
   "tags" that you have created to name specific versions of your
   repository. 

   One note: the special "master" head is the default branch, which is
   why the .git/HEAD file was created as a symlink to it even if it
   doesn't yet exist. Basically, the HEAD link is supposed to always
   point to the branch you are working on right now, and you always
   start out expecting to work on the "master" branch.

   However, this is only a convention, and you can name your branches
   anything you want, and don't have to ever even _have_ a "master"
   branch.  A number of the git tools will assume that .git/HEAD is
   valid, though.

   [ Implementation note: an "object" is identified by its 160-bit SHA1
   hash, aka "name", and a reference to an object is always the 40-byte
   hex representation of that SHA1 name. The files in the "refs"
   subdirectory are expected to contain these hex references (usually
   with a final '\n' at the end), and you should thus expect to see a
   number of 41-byte files containing these references in this refs
   subdirectories when you actually start populating your tree ]

You have now created your first git archive. Of course, since it's
empty, that's not very useful, so let's start populating it with data.


	Populating a git archive
	------------------------

We'll keep this simple and stupid, so we'll start off with populating a
few trivial files just to get a feel for it.

Start off with just creating any random files that you want to maintain
in your git archive. We'll start off with a few bad examples, just to
get a feel for how this works:

	echo "Hello World" >hello
	echo "Silly example" >example

you have now created two files in your working directory, but to
actually check in your hard work, you will have to go through two steps:

 - fill in the "cache" aka "index" file with the information about your
   working directory state

 - commit that index file as an object.

The first step is trivial: when you want to tell git about any changes
to your working directory, you use the "git-update-cache" program.  That
program normally just takes a list of filenames you want to update, but
to avoid trivial mistakes, it refuses to add new entries to the cache
(or remove existing ones) unless you explicitly tell it that you're
adding a new entry with the "--add" flag (or removing an entry with the
"--remove") flag. 

So to populate the index with the two files you just created, you can do

	git-update-cache --add hello example

and you have now told git to track those two files.

In fact, as you did that, if you now look into your object directory,
you'll notice that git will have added two new objects to the object
store.  If you did exactly the steps above, you should now be able to do

	ls .git/objects/??/*

and see two files:

	.git/objects/55/7db03de997c86a4a028e1ebd3a1ceb225be238 
	.git/objects/f2/4c74a2e500f5ee1332c86b94199f52b1d1d962

which correspond with the object with SHA1 names of 557db... and f24c7..
respectively.

If you want to, you can use "git-cat-file" to look at those objects, but
you'll have to use the object name, not the filename of the object:

	git-cat-file -t 557db03de997c86a4a028e1ebd3a1ceb225be238

where the "-t" tells git-cat-file to tell you what the "type" of the
object is. Git will tell you that you have a "blob" object (ie just a
regular file), and you can see the contents with

	git-cat-file "blob" 557db03de997c86a4a028e1ebd3a1ceb225be238

which will print out "Hello World".  The object 557db...  is nothing
more than the contents of your file "hello". 

[ Digression: don't confuse that object with the file "hello" itself.  The
  object is literally just those specific _contents_ of the file, and
  however much you later change the contents in file "hello", the object we
  just looked at will never change.  Objects are immutable.  ]

Anyway, as we mentioned previously, you normally never actually take a
look at the objects themselves, and typing long 40-character hex SHA1
names is not something you'd normally want to do.  The above digression
was just to show that "git-update-cache" did something magical, and
actually saved away the contents of your files into the git content
store. 

Updating the cache did something else too: it created a ".git/index"
file.  This is the index that describes your current working tree, and
something you should be very aware of.  Again, you normally never worry
about the index file itself, but you should be aware of the fact that
you have not actually really "checked in" your files into git so far,
you've only _told_ git about them.

However, since git knows about them, you can now start using some of the
most basic git commands to manipulate the files or look at their status. 

In particular, let's not even check in the two files into git yet, we'll
start off by adding another line to "hello" first:

	echo "It's a new day for git" >>hello

and you can now, since you told git about the previous state of "hello", ask
git what has changed in the tree compared to your old index, using the
"git-diff-files" command:

	git-diff-files 

oops.  That wasn't very readable.  It just spit out its own internal
version of a "diff", but that internal version really just tells you
that it has noticed that "hello" has been modified, and that the old object
contents it had have been replaced with something else.

To make it readable, we can tell git-diff-files to output the
differences as a patch, using the "-p" flag:

	git-diff-files -p

which will spit out

	diff --git a/hello b/hello
	--- a/hello
	+++ b/hello
	@@ -1 +1,2 @@
	 Hello World
	+It's a new day for git

ie the diff of the change we caused by adding another line to "hello".

In other words, git-diff-files always shows us the difference between
what is recorded in the index, and what is currently in the working
tree. That's very useful.

A common shorthand for "git-diff-files -p" is to just write

	git diff

which will do the same thing. 


	Committing git state
	--------------------

Now, we want to go to the next stage in git, which is to take the files
that git knows about in the index, and commit them as a real tree. We do
that in two phases: creating a "tree" object, and committing that "tree"
object as a "commit" object together with an explanation of what the
tree was all about, along with information of how we came to that state.

Creating a tree object is trivial, and is done with "git-write-tree". 
There are no options or other input: git-write-tree will take the
current index state, and write an object that describes that whole
index.  In other words, we're now tying together all the different
filenames with their contents (and their permissions), and we're
creating the equivalent of a git "directory" object:

	git-write-tree

and this will just output the name of the resulting tree, in this case
(if you have done exactly as I've described) it should be

	8988da15d077d4829fc51d8544c097def6644dbb

which is another incomprehensible object name. Again, if you want to,
you can use "git-cat-file -t 8988d.." to see that this time the object
is not a "blob" object, but a "tree" object (you can also use
git-cat-file to actually output the raw object contents, but you'll see
mainly a binary mess, so that's less interesting).

However - normally you'd never use "git-write-tree" on its own, because
normally you always commit a tree into a commit object using the
"git-commit-tree" command. In fact, it's easier to not actually use
git-write-tree on its own at all, but to just pass its result in as an
argument to "git-commit-tree".

"git-commit-tree" normally takes several arguments - it wants to know
what the _parent_ of a commit was, but since this is the first commit
ever in this new archive, and it has no parents, we only need to pass in
the tree ID. However, git-commit-tree also wants to get a commit message
on its standard input, and it will write out the resulting ID for the
commit to its standard output.

And this is where we start using the .git/HEAD file. The HEAD file is
supposed to contain the reference to the top-of-tree, and since that's