forked from git/git
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgitcore-tutorial.7
More file actions
2192 lines (2182 loc) · 71 KB
/
Copy pathgitcore-tutorial.7
File metadata and controls
2192 lines (2182 loc) · 71 KB
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
'\" t
.\" Title: gitcore-tutorial
.\" Author: [FIXME: author] [see http://docbook.sf.net/el/author]
.\" Generator: DocBook XSL Stylesheets v1.75.2 <http://docbook.sf.net/>
.\" Date: 11/06/2010
.\" Manual: Git Manual
.\" Source: Git 1.7.3.2.161.g3089c
.\" Language: English
.\"
.TH "GITCORE\-TUTORIAL" "7" "11/06/2010" "Git 1\&.7\&.3\&.2\&.161\&.g308" "Git Manual"
.\" -----------------------------------------------------------------
.\" * set default formatting
.\" -----------------------------------------------------------------
.\" disable hyphenation
.nh
.\" disable justification (adjust text to left margin only)
.ad l
.\" -----------------------------------------------------------------
.\" * MAIN CONTENT STARTS HERE *
.\" -----------------------------------------------------------------
.SH "NAME"
gitcore-tutorial \- A git core tutorial for developers
.SH "SYNOPSIS"
.sp
git *
.SH "DESCRIPTION"
.sp
This tutorial explains how to use the "core" git commands to set up and work with a git repository\&.
.sp
If you just need to use git as a revision control system you may prefer to start with "A Tutorial Introduction to GIT" (\fBgittutorial\fR(7)) or \m[blue]\fBthe GIT User Manual\fR\m[]\&\s-2\u[1]\d\s+2\&.
.sp
However, an understanding of these low\-level tools can be helpful if you want to understand git\(cqs internals\&.
.sp
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\(cqt flushing\&.
.sp
Back when this document was originally written, many porcelain commands were shell scripts\&. For simplicity, it still uses them as examples to illustrate how plumbing is fit together to form the porcelain commands\&. The source tree includes some of these scripts in contrib/examples/ for reference\&. Although these are not implemented as shell scripts anymore, the description of what the plumbing layer commands do is still valid\&.
.if n \{\
.sp
.\}
.RS 4
.it 1 an-trap
.nr an-no-space-flag 1
.nr an-break-flag 1
.br
.ps +1
\fBNote\fR
.ps -1
.br
.sp
Deeper technical details are often marked as Notes, which you can skip on your first reading\&.
.sp .5v
.RE
.SH "CREATING A GIT REPOSITORY"
.sp
Creating a new git repository couldn\(cqt be easier: all git repositories 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\&.
.sp
For our first example, we\(cqre going to start a totally new repository from scratch, with no pre\-existing files, and we\(cqll call it \fIgit\-tutorial\fR\&. To start up, create a subdirectory for it, change into that subdirectory, and initialize the git infrastructure with \fIgit init\fR:
.sp
.if n \{\
.RS 4
.\}
.nf
$ mkdir git\-tutorial
$ cd git\-tutorial
$ git init
.fi
.if n \{\
.RE
.\}
.sp
.sp
to which git will reply
.sp
.if n \{\
.RS 4
.\}
.nf
Initialized empty Git repository in \&.git/
.fi
.if n \{\
.RE
.\}
.sp
.sp
which is just git\(cqs way of saying that you haven\(cqt 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 \fIls\fR\&. For your new empty project, it should show you three entries, among other things:
.sp
.RS 4
.ie n \{\
\h'-04'\(bu\h'+03'\c
.\}
.el \{\
.sp -1
.IP \(bu 2.3
.\}
a file called
HEAD, that has
ref: refs/heads/master
in it\&. This is similar to a symbolic link and points at
refs/heads/master
relative to the
HEAD
file\&.
.sp
Don\(cqt worry about the fact that the file that the
HEAD
link points to doesn\(cqt even exist yet \(em you haven\(cqt created the commit that will start your
HEAD
development branch yet\&.
.RE
.sp
.RS 4
.ie n \{\
\h'-04'\(bu\h'+03'\c
.\}
.el \{\
.sp -1
.IP \(bu 2.3
.\}
a subdirectory called
objects, which will contain all the 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
\fIdata\fR
in your repository\&.
.RE
.sp
.RS 4
.ie n \{\
\h'-04'\(bu\h'+03'\c
.\}
.el \{\
.sp -1
.IP \(bu 2.3
.\}
a subdirectory called
refs, which contains references to objects\&.
.RE
.sp
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 \fIheads\fR of development (aka \fIbranches\fR), and to any \fItags\fR that you have created to name specific versions in your repository\&.
.sp
One note: the special master head is the default branch, which is why the \&.git/HEAD file was created points to it even if it doesn\(cqt 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\&.
.sp
However, this is only a convention, and you can name your branches anything you want, and don\(cqt have to ever even \fIhave\fR a master branch\&. A number of the git tools will assume that \&.git/HEAD is valid, though\&.
.if n \{\
.sp
.\}
.RS 4
.it 1 an-trap
.nr an-no-space-flag 1
.nr an-break-flag 1
.br
.ps +1
\fBNote\fR
.ps -1
.br
.sp
An \fIobject\fR is identified by its 160\-bit SHA1 hash, aka \fIobject name\fR, 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 \en at the end), and you should thus expect to see a number of 41\-byte files containing these references in these refs subdirectories when you actually start populating your tree\&.
.sp .5v
.RE
.if n \{\
.sp
.\}
.RS 4
.it 1 an-trap
.nr an-no-space-flag 1
.nr an-break-flag 1
.br
.ps +1
\fBNote\fR
.ps -1
.br
.sp
An advanced user may want to take a look at \fBgitrepository-layout\fR(5) after finishing this tutorial\&.
.sp .5v
.RE
.sp
You have now created your first git repository\&. Of course, since it\(cqs empty, that\(cqs not very useful, so let\(cqs start populating it with data\&.
.SH "POPULATING A GIT REPOSITORY"
.sp
We\(cqll keep this simple and stupid, so we\(cqll start off with populating a few trivial files just to get a feel for it\&.
.sp
Start off with just creating any random files that you want to maintain in your git repository\&. We\(cqll start off with a few bad examples, just to get a feel for how this works:
.sp
.if n \{\
.RS 4
.\}
.nf
$ echo "Hello World" >hello
$ echo "Silly example" >example
.fi
.if n \{\
.RE
.\}
.sp
.sp
you have now created two files in your working tree (aka \fIworking directory\fR), but to actually check in your hard work, you will have to go through two steps:
.sp
.RS 4
.ie n \{\
\h'-04'\(bu\h'+03'\c
.\}
.el \{\
.sp -1
.IP \(bu 2.3
.\}
fill in the
\fIindex\fR
file (aka
\fIcache\fR) with the information about your working tree state\&.
.RE
.sp
.RS 4
.ie n \{\
\h'-04'\(bu\h'+03'\c
.\}
.el \{\
.sp -1
.IP \(bu 2.3
.\}
commit that index file as an object\&.
.RE
.sp
The first step is trivial: when you want to tell git about any changes to your working tree, you use the \fIgit update\-index\fR 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 index (or remove existing ones) unless you explicitly tell it that you\(cqre adding a new entry with the \-\-add flag (or removing an entry with the \-\-remove) flag\&.
.sp
So to populate the index with the two files you just created, you can do
.sp
.if n \{\
.RS 4
.\}
.nf
$ git update\-index \-\-add hello example
.fi
.if n \{\
.RE
.\}
.sp
.sp
and you have now told git to track those two files\&.
.sp
In fact, as you did that, if you now look into your object directory, you\(cqll notice that git will have added two new objects to the object database\&. If you did exactly the steps above, you should now be able to do
.sp
.if n \{\
.RS 4
.\}
.nf
$ ls \&.git/objects/??/*
.fi
.if n \{\
.RE
.\}
.sp
.sp
and see two files:
.sp
.if n \{\
.RS 4
.\}
.nf
\&.git/objects/55/7db03de997c86a4a028e1ebd3a1ceb225be238
\&.git/objects/f2/4c74a2e500f5ee1332c86b94199f52b1d1d962
.fi
.if n \{\
.RE
.\}
.sp
.sp
which correspond with the objects with names of 557db\&... and f24c7\&... respectively\&.
.sp
If you want to, you can use \fIgit cat\-file\fR to look at those objects, but you\(cqll have to use the object name, not the filename of the object:
.sp
.if n \{\
.RS 4
.\}
.nf
$ git cat\-file \-t 557db03de997c86a4a028e1ebd3a1ceb225be238
.fi
.if n \{\
.RE
.\}
.sp
.sp
where the \-t tells \fIgit cat\-file\fR to tell you what the "type" of the object is\&. git will tell you that you have a "blob" object (i\&.e\&., just a regular file), and you can see the contents with
.sp
.if n \{\
.RS 4
.\}
.nf
$ git cat\-file blob 557db03
.fi
.if n \{\
.RE
.\}
.sp
.sp
which will print out "Hello World"\&. The object 557db03 is nothing more than the contents of your file hello\&.
.if n \{\
.sp
.\}
.RS 4
.it 1 an-trap
.nr an-no-space-flag 1
.nr an-break-flag 1
.br
.ps +1
\fBNote\fR
.ps -1
.br
.sp
Don\(cqt confuse that object with the file hello itself\&. The object is literally just those specific \fBcontents\fR 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\&.
.sp .5v
.RE
.if n \{\
.sp
.\}
.RS 4
.it 1 an-trap
.nr an-no-space-flag 1
.nr an-break-flag 1
.br
.ps +1
\fBNote\fR
.ps -1
.br
.sp
The second example demonstrates that you can abbreviate the object name to only the first several hexadecimal digits in most places\&.
.sp .5v
.RE
.sp
Anyway, as we mentioned previously, you normally never actually take a look at the objects themselves, and typing long 40\-character hex names is not something you\(cqd normally want to do\&. The above digression was just to show that \fIgit update\-index\fR did something magical, and actually saved away the contents of your files into the git object database\&.
.sp
Updating the index 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\(cqve only \fBtold\fR git about them\&.
.sp
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\&.
.sp
In particular, let\(cqs not even check in the two files into git yet, we\(cqll start off by adding another line to hello first:
.sp
.if n \{\
.RS 4
.\}
.nf
$ echo "It\(aqs a new day for git" >>hello
.fi
.if n \{\
.RE
.\}
.sp
.sp
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 \fIgit diff\-files\fR command:
.sp
.if n \{\
.RS 4
.\}
.nf
$ git diff\-files
.fi
.if n \{\
.RE
.\}
.sp
.sp
Oops\&. That wasn\(cqt very readable\&. It just spit out its own internal version of a \fIdiff\fR, 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\&.
.sp
To make it readable, we can tell \fIgit diff\-files\fR to output the differences as a patch, using the \-p flag:
.sp
.if n \{\
.RS 4
.\}
.nf
$ git diff\-files \-p
diff \-\-git a/hello b/hello
index 557db03\&.\&.263414f 100644
\-\-\- a/hello
+++ b/hello
@@ \-1 +1,2 @@
Hello World
+It\(aqs a new day for git
.fi
.if n \{\
.RE
.\}
.sp
.sp
i\&.e\&. the diff of the change we caused by adding another line to hello\&.
.sp
In other words, \fIgit diff\-files\fR always shows us the difference between what is recorded in the index, and what is currently in the working tree\&. That\(cqs very useful\&.
.sp
A common shorthand for git diff\-files \-p is to just write git diff, which will do the same thing\&.
.sp
.if n \{\
.RS 4
.\}
.nf
$ git diff
diff \-\-git a/hello b/hello
index 557db03\&.\&.263414f 100644
\-\-\- a/hello
+++ b/hello
@@ \-1 +1,2 @@
Hello World
+It\(aqs a new day for git
.fi
.if n \{\
.RE
.\}
.sp
.SH "COMMITTING GIT STATE"
.sp
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 \fItree\fR object, and committing that \fItree\fR object as a \fIcommit\fR object together with an explanation of what the tree was all about, along with information of how we came to that state\&.
.sp
Creating a tree object is trivial, and is done with \fIgit write\-tree\fR\&. 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\(cqre now tying together all the different filenames with their contents (and their permissions), and we\(cqre creating the equivalent of a git "directory" object:
.sp
.if n \{\
.RS 4
.\}
.nf
$ git write\-tree
.fi
.if n \{\
.RE
.\}
.sp
.sp
and this will just output the name of the resulting tree, in this case (if you have done exactly as I\(cqve described) it should be
.sp
.if n \{\
.RS 4
.\}
.nf
8988da15d077d4829fc51d8544c097def6644dbb
.fi
.if n \{\
.RE
.\}
.sp
.sp
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\(cqll see mainly a binary mess, so that\(cqs less interesting)\&.
.sp
However \(em normally you\(cqd never use \fIgit write\-tree\fR on its own, because normally you always commit a tree into a commit object using the \fIgit commit\-tree\fR command\&. In fact, it\(cqs easier to not actually use \fIgit write\-tree\fR on its own at all, but to just pass its result in as an argument to \fIgit commit\-tree\fR\&.
.sp
\fIgit commit\-tree\fR normally takes several arguments \(em it wants to know what the \fIparent\fR of a commit was, but since this is the first commit ever in this new repository, and it has no parents, we only need to pass in the object name of the tree\&. However, \fIgit commit\-tree\fR also wants to get a commit message on its standard input, and it will write out the resulting object name for the commit to its standard output\&.
.sp
And this is where we create the \&.git/refs/heads/master file which is pointed at by HEAD\&. This file is supposed to contain the reference to the top\-of\-tree of the master branch, and since that\(cqs exactly what \fIgit commit\-tree\fR spits out, we can do this all with a sequence of simple shell commands:
.sp
.if n \{\
.RS 4
.\}
.nf
$ tree=$(git write\-tree)
$ commit=$(echo \(aqInitial commit\(aq | git commit\-tree $tree)
$ git update\-ref HEAD $commit
.fi
.if n \{\
.RE
.\}
.sp
.sp
In this case this creates a totally new commit that is not related to anything else\&. Normally you do this only \fBonce\fR for a project ever, and all later commits will be parented on top of an earlier commit\&.
.sp
Again, normally you\(cqd never actually do this by hand\&. There is a helpful script called git commit that will do all of this for you\&. So you could have just written git commit instead, and it would have done the above magic scripting for you\&.
.SH "MAKING A CHANGE"
.sp
Remember how we did the \fIgit update\-index\fR on file hello and then we changed hello afterward, and could compare the new state of hello with the state we saved in the index file?
.sp
Further, remember how I said that \fIgit write\-tree\fR writes the contents of the \fBindex\fR file to the tree, and thus what we just committed was in fact the \fBoriginal\fR contents of the file hello, not the new ones\&. We did that on purpose, to show the difference between the index state, and the state in the working tree, and how they don\(cqt have to match, even when we commit things\&.
.sp
As before, if we do git diff\-files \-p in our git\-tutorial project, we\(cqll still see the same difference we saw last time: the index file hasn\(cqt changed by the act of committing anything\&. However, now that we have committed something, we can also learn to use a new command: \fIgit diff\-index\fR\&.
.sp
Unlike \fIgit diff\-files\fR, which showed the difference between the index file and the working tree, \fIgit diff\-index\fR shows the differences between a committed \fBtree\fR and either the index file or the working tree\&. In other words, \fIgit diff\-index\fR wants a tree to be diffed against, and before we did the commit, we couldn\(cqt do that, because we didn\(cqt have anything to diff against\&.
.sp
But now we can do
.sp
.if n \{\
.RS 4
.\}
.nf
$ git diff\-index \-p HEAD
.fi
.if n \{\
.RE
.\}
.sp
.sp
(where \-p has the same meaning as it did in \fIgit diff\-files\fR), and it will show us the same difference, but for a totally different reason\&. Now we\(cqre comparing the working tree not against the index file, but against the tree we just wrote\&. It just so happens that those two are obviously the same, so we get the same result\&.
.sp
Again, because this is a common operation, you can also just shorthand it with
.sp
.if n \{\
.RS 4
.\}
.nf
$ git diff HEAD
.fi
.if n \{\
.RE
.\}
.sp
.sp
which ends up doing the above for you\&.
.sp
In other words, \fIgit diff\-index\fR normally compares a tree against the working tree, but when given the \-\-cached flag, it is told to instead compare against just the index cache contents, and ignore the current working tree state entirely\&. Since we just wrote the index file to HEAD, doing git diff\-index \-\-cached \-p HEAD should thus return an empty set of differences, and that\(cqs exactly what it does\&.
.if n \{\
.sp
.\}
.RS 4
.it 1 an-trap
.nr an-no-space-flag 1
.nr an-break-flag 1
.br
.ps +1
\fBNote\fR
.ps -1
.br
.sp
\fIgit diff\-index\fR really always uses the index for its comparisons, and saying that it compares a tree against the working tree is thus not strictly accurate\&. In particular, the list of files to compare (the "meta\-data") \fBalways\fR comes from the index file, regardless of whether the \-\-cached flag is used or not\&. The \-\-cached flag really only determines whether the file \fBcontents\fR to be compared come from the working tree or not\&.
.sp
This is not hard to understand, as soon as you realize that git simply never knows (or cares) about files that it is not told about explicitly\&. git will never go \fBlooking\fR for files to compare, it expects you to tell it what the files are, and that\(cqs what the index is there for\&.
.sp .5v
.RE
.sp
However, our next step is to commit the \fBchange\fR we did, and again, to understand what\(cqs going on, keep in mind the difference between "working tree contents", "index file" and "committed tree"\&. We have changes in the working tree that we want to commit, and we always have to work through the index file, so the first thing we need to do is to update the index cache:
.sp
.if n \{\
.RS 4
.\}
.nf
$ git update\-index hello
.fi
.if n \{\
.RE
.\}
.sp
.sp
(note how we didn\(cqt need the \-\-add flag this time, since git knew about the file already)\&.
.sp
Note what happens to the different \fIgit diff\-*\fR versions here\&. After we\(cqve updated hello in the index, git diff\-files \-p now shows no differences, but git diff\-index \-p HEAD still \fBdoes\fR show that the current state is different from the state we committed\&. In fact, now \fIgit diff\-index\fR shows the same difference whether we use the \-\-cached flag or not, since now the index is coherent with the working tree\&.
.sp
Now, since we\(cqve updated hello in the index, we can commit the new version\&. We could do it by writing the tree by hand again, and committing the tree (this time we\(cqd have to use the \-p HEAD flag to tell commit that the HEAD was the \fBparent\fR of the new commit, and that this wasn\(cqt an initial commit any more), but you\(cqve done that once already, so let\(cqs just use the helpful script this time:
.sp
.if n \{\
.RS 4
.\}
.nf
$ git commit
.fi
.if n \{\
.RE
.\}
.sp
.sp
which starts an editor for you to write the commit message and tells you a bit about what you have done\&.
.sp
Write whatever message you want, and all the lines that start with \fI#\fR will be pruned out, and the rest will be used as the commit message for the change\&. If you decide you don\(cqt want to commit anything after all at this point (you can continue to edit things and update the index), you can just leave an empty message\&. Otherwise git commit will commit the change for you\&.
.sp
You\(cqve now made your first real git commit\&. And if you\(cqre interested in looking at what git commit really does, feel free to investigate: it\(cqs a few very simple shell scripts to generate the helpful (?) commit message headers, and a few one\-liners that actually do the commit itself (\fIgit commit\fR)\&.
.SH "INSPECTING CHANGES"
.sp
While creating changes is useful, it\(cqs even more useful if you can tell later what changed\&. The most useful command for this is another of the \fIdiff\fR family, namely \fIgit diff\-tree\fR\&.
.sp
\fIgit diff\-tree\fR can be given two arbitrary trees, and it will tell you the differences between them\&. Perhaps even more commonly, though, you can give it just a single commit object, and it will figure out the parent of that commit itself, and show the difference directly\&. Thus, to get the same diff that we\(cqve already seen several times, we can now do
.sp
.if n \{\
.RS 4
.\}
.nf
$ git diff\-tree \-p HEAD
.fi
.if n \{\
.RE
.\}
.sp
.sp
(again, \-p means to show the difference as a human\-readable patch), and it will show what the last commit (in HEAD) actually changed\&.
.if n \{\
.sp
.\}
.RS 4
.it 1 an-trap
.nr an-no-space-flag 1
.nr an-break-flag 1
.br
.ps +1
\fBNote\fR
.ps -1
.br
.sp
Here is an ASCII art by Jon Loeliger that illustrates how various \fIdiff\-*\fR commands compare things\&.
.sp
.if n \{\
.RS 4
.\}
.nf
diff\-tree
+\-\-\-\-+
| |
| |
V V
+\-\-\-\-\-\-\-\-\-\-\-+
| Object DB |
| Backing |
| Store |
+\-\-\-\-\-\-\-\-\-\-\-+
^ ^
| |
| | diff\-index \-\-cached
| |
diff\-index | V
| +\-\-\-\-\-\-\-\-\-\-\-+
| | Index |
| | "cache" |
| +\-\-\-\-\-\-\-\-\-\-\-+
| ^
| |
| | diff\-files
| |
V V
+\-\-\-\-\-\-\-\-\-\-\-+
| Working |
| Directory |
+\-\-\-\-\-\-\-\-\-\-\-+
.fi
.if n \{\
.RE
.\}
.sp .5v
.RE
.sp
More interestingly, you can also give \fIgit diff\-tree\fR the \-\-pretty flag, which tells it to also show the commit message and author and date of the commit, and you can tell it to show a whole series of diffs\&. Alternatively, you can tell it to be "silent", and not show the diffs at all, but just show the actual commit message\&.
.sp
In fact, together with the \fIgit rev\-list\fR program (which generates a list of revisions), \fIgit diff\-tree\fR ends up being a veritable fount of changes\&. A trivial (but very useful) script called \fIgit whatchanged\fR is included with git which does exactly this, and shows a log of recent activities\&.
.sp
To see the whole history of our pitiful little git\-tutorial project, you can do
.sp
.if n \{\
.RS 4
.\}
.nf
$ git log
.fi
.if n \{\
.RE
.\}
.sp
.sp
which shows just the log messages, or if we want to see the log together with the associated patches use the more complex (and much more powerful)
.sp
.if n \{\
.RS 4
.\}
.nf
$ git whatchanged \-p
.fi
.if n \{\
.RE
.\}
.sp
.sp
and you will see exactly what has changed in the repository over its short history\&.
.if n \{\
.sp
.\}
.RS 4
.it 1 an-trap
.nr an-no-space-flag 1
.nr an-break-flag 1
.br
.ps +1
\fBNote\fR
.ps -1
.br
.sp
When using the above two commands, the initial commit will be shown\&. If this is a problem because it is huge, you can hide it by setting the log\&.showroot configuration variable to false\&. Having this, you can still show it for each command just adding the \-\-root option, which is a flag for \fIgit diff\-tree\fR accepted by both commands\&.
.sp .5v
.RE
.sp
With that, you should now be having some inkling of what git does, and can explore on your own\&.
.if n \{\
.sp
.\}
.RS 4
.it 1 an-trap
.nr an-no-space-flag 1
.nr an-break-flag 1
.br
.ps +1
\fBNote\fR
.ps -1
.br
.sp
Most likely, you are not directly using the core git Plumbing commands, but using Porcelain such as \fIgit add\fR, \(oqgit\-rm\(cq and \(oqgit\-commit\(cq\&.
.sp .5v
.RE
.SH "TAGGING A VERSION"
.sp
In git, there are two kinds of tags, a "light" one, and an "annotated tag"\&.
.sp
A "light" tag is technically nothing more than a branch, except we put it in the \&.git/refs/tags/ subdirectory instead of calling it a head\&. So the simplest form of tag involves nothing more than
.sp
.if n \{\
.RS 4
.\}
.nf
$ git tag my\-first\-tag
.fi
.if n \{\
.RE
.\}
.sp
.sp
which just writes the current HEAD into the \&.git/refs/tags/my\-first\-tag file, after which point you can then use this symbolic name for that particular state\&. You can, for example, do
.sp
.if n \{\
.RS 4
.\}
.nf
$ git diff my\-first\-tag
.fi
.if n \{\
.RE
.\}
.sp
.sp
to diff your current state against that tag which at this point will obviously be an empty diff, but if you continue to develop and commit stuff, you can use your tag as an "anchor\-point" to see what has changed since you tagged it\&.
.sp
An "annotated tag" is actually a real git object, and contains not only a pointer to the state you want to tag, but also a small tag name and message, along with optionally a PGP signature that says that yes, you really did that tag\&. You create these annotated tags with either the \-a or \-s flag to \fIgit tag\fR:
.sp
.if n \{\
.RS 4
.\}
.nf
$ git tag \-s <tagname>
.fi
.if n \{\
.RE
.\}
.sp
.sp
which will sign the current HEAD (but you can also give it another argument that specifies the thing to tag, e\&.g\&., you could have tagged the current mybranch point by using git tag <tagname> mybranch)\&.
.sp
You normally only do signed tags for major releases or things like that, while the light\-weight tags are useful for any marking you want to do \(em any time you decide that you want to remember a certain point, just create a private tag for it, and you have a nice symbolic name for the state at that point\&.
.SH "COPYING REPOSITORIES"
.sp
git repositories are normally totally self\-sufficient and relocatable\&. Unlike CVS, for example, there is no separate notion of "repository" and "working tree"\&. A git repository normally \fBis\fR the working tree, with the local git information hidden in the \&.git subdirectory\&. There is nothing else\&. What you see is what you got\&.
.if n \{\
.sp
.\}
.RS 4
.it 1 an-trap
.nr an-no-space-flag 1
.nr an-break-flag 1
.br
.ps +1
\fBNote\fR
.ps -1
.br
.sp
You can tell git to split the git internal information from the directory that it tracks, but we\(cqll ignore that for now: it\(cqs not how normal projects work, and it\(cqs really only meant for special uses\&. So the mental model of "the git information is always tied directly to the working tree that it describes" may not be technically 100% accurate, but it\(cqs a good model for all normal use\&.
.sp .5v
.RE
.sp
This has two implications:
.sp
.RS 4
.ie n \{\
\h'-04'\(bu\h'+03'\c
.\}
.el \{\
.sp -1
.IP \(bu 2.3
.\}
if you grow bored with the tutorial repository you created (or you\(cqve made a mistake and want to start all over), you can just do simple
.sp
.if n \{\
.RS 4
.\}
.nf
$ rm \-rf git\-tutorial
.fi
.if n \{\
.RE
.\}
.sp
and it will be gone\&. There\(cqs no external repository, and there\(cqs no history outside the project you created\&.
.RE
.sp
.RS 4
.ie n \{\
\h'-04'\(bu\h'+03'\c
.\}
.el \{\
.sp -1
.IP \(bu 2.3
.\}
if you want to move or duplicate a git repository, you can do so\&. There is
\fIgit clone\fR
command, but if all you want to do is just to create a copy of your repository (with all the full history that went along with it), you can do so with a regular
cp \-a git\-tutorial new\-git\-tutorial\&.
.sp
Note that when you\(cqve moved or copied a git repository, your git index file (which caches various information, notably some of the "stat" information for the files involved) will likely need to be refreshed\&. So after you do a
cp \-a
to create a new copy, you\(cqll want to do
.sp
.if n \{\
.RS 4
.\}
.nf
$ git update\-index \-\-refresh
.fi
.if n \{\
.RE
.\}
.sp
in the new repository to make sure that the index file is up\-to\-date\&.
.RE
.sp
Note that the second point is true even across machines\&. You can duplicate a remote git repository with \fBany\fR regular copy mechanism, be it \fIscp\fR, \fIrsync\fR or \fIwget\fR\&.
.sp
When copying a remote repository, you\(cqll want to at a minimum update the index cache when you do this, and especially with other peoples\(aq repositories you often want to make sure that the index cache is in some known state (you don\(cqt know \fBwhat\fR they\(cqve done and not yet checked in), so usually you\(cqll precede the \fIgit update\-index\fR with a
.sp
.if n \{\
.RS 4
.\}
.nf
$ git read\-tree \-\-reset HEAD
$ git update\-index \-\-refresh
.fi
.if n \{\
.RE
.\}
.sp
.sp
which will force a total index re\-build from the tree pointed to by HEAD\&. It resets the index contents to HEAD, and then the \fIgit update\-index\fR makes sure to match up all index entries with the checked\-out files\&. If the original repository had uncommitted changes in its working tree, git update\-index \-\-refresh notices them and tells you they need to be updated\&.
.sp
The above can also be written as simply
.sp
.if n \{\
.RS 4
.\}
.nf
$ git reset
.fi
.if n \{\
.RE
.\}
.sp
.sp
and in fact a lot of the common git command combinations can be scripted with the git xyz interfaces\&. You can learn things by just looking at what the various git scripts do\&. For example, git reset used to be the above two lines implemented in \fIgit reset\fR, but some things like \fIgit status\fR and \fIgit commit\fR are slightly more complex scripts around the basic git commands\&.
.sp
Many (most?) public remote repositories will not contain any of the checked out files or even an index file, and will \fBonly\fR contain the actual core git files\&. Such a repository usually doesn\(cqt even have the \&.git subdirectory, but has all the git files directly in the repository\&.
.sp
To create your own local live copy of such a "raw" git repository, you\(cqd first create your own subdirectory for the project, and then copy the raw repository contents into the \&.git directory\&. For example, to create your own copy of the git repository, you\(cqd do the following
.sp
.if n \{\
.RS 4
.\}
.nf
$ mkdir my\-git
$ cd my\-git
$ rsync \-rL rsync://rsync\&.kernel\&.org/pub/scm/git/git\&.git/ \&.git
.fi
.if n \{\
.RE
.\}
.sp
.sp
followed by
.sp
.if n \{\
.RS 4
.\}
.nf
$ git read\-tree HEAD
.fi
.if n \{\
.RE
.\}
.sp
.sp
to populate the index\&. However, now you have populated the index, and you have all the git internal files, but you will notice that you don\(cqt actually have any of the working tree files to work on\&. To get those, you\(cqd check them out with
.sp
.if n \{\
.RS 4
.\}
.nf
$ git checkout\-index \-u \-a
.fi
.if n \{\
.RE
.\}
.sp
.sp
where the \-u flag means that you want the checkout to keep the index up\-to\-date (so that you don\(cqt have to refresh it afterward), and the \-a flag means "check out all files" (if you have a stale copy or an older version of a checked out tree you may also need to add the \-f flag first, to tell \fIgit checkout\-index\fR to \fBforce\fR overwriting of any old files)\&.
.sp
Again, this can all be simplified with
.sp
.if n \{\
.RS 4
.\}
.nf
$ git clone rsync://rsync\&.kernel\&.org/pub/scm/git/git\&.git/ my\-git
$ cd my\-git
$ git checkout
.fi
.if n \{\
.RE
.\}
.sp
.sp
which will end up doing all of the above for you\&.
.sp
You have now successfully copied somebody else\(cqs (mine) remote repository, and checked it out\&.
.SH "CREATING A NEW BRANCH"
.sp
Branches in git are really nothing more than pointers into the git object database from within the \&.git/refs/ subdirectory, and as we already discussed, the HEAD branch is nothing but a symlink to one of these object pointers\&.
.sp
You can at any time create a new branch by just picking an arbitrary point in the project history, and just writing the SHA1 name of that object into a file under \&.git/refs/heads/\&. You can use any filename you want (and indeed, subdirectories), but the convention is that the "normal" branch is called master\&. That\(cqs just a convention, though, and nothing enforces it\&.
.sp
To show that as an example, let\(cqs go back to the git\-tutorial repository we used earlier, and create a branch in it\&. You do that by simply just saying that you want to check out a new branch:
.sp
.if n \{\
.RS 4
.\}
.nf
$ git checkout \-b mybranch
.fi
.if n \{\
.RE
.\}
.sp
.sp
will create a new branch based at the current HEAD position, and switch to it\&.
.if n \{\
.sp
.\}
.RS 4
.it 1 an-trap
.nr an-no-space-flag 1
.nr an-break-flag 1
.br
.ps +1
\fBNote\fR
.ps -1
.br
.sp
If you make the decision to start your new branch at some other point in the history than the current HEAD, you can do so by just telling \fIgit checkout\fR what the base of the checkout would be\&. In other words, if you have an earlier tag or branch, you\(cqd just do
.sp
.if n \{\
.RS 4
.\}
.nf
$ git checkout \-b mybranch earlier\-commit
.fi
.if n \{\
.RE
.\}
.sp
.sp
and it would create the new branch mybranch at the earlier commit, and check out the state at that time\&.
.sp .5v
.RE
.sp
You can always just jump back to your original master branch by doing
.sp
.if n \{\
.RS 4
.\}
.nf
$ git checkout master
.fi
.if n \{\
.RE
.\}
.sp
.sp
(or any other branch\-name, for that matter) and if you forget which branch you happen to be on, a simple
.sp
.if n \{\
.RS 4
.\}
.nf
$ cat \&.git/HEAD
.fi
.if n \{\
.RE
.\}
.sp
.sp
will tell you where it\(cqs pointing\&. To get the list of branches you have, you can say
.sp
.if n \{\
.RS 4
.\}
.nf
$ git branch
.fi
.if n \{\
.RE
.\}
.sp
.sp
which used to be nothing more than a simple script around ls \&.git/refs/heads\&. There will be an asterisk in front of the branch you are currently on\&.
.sp
Sometimes you may wish to create a new branch \fIwithout\fR actually checking it out and switching to it\&. If so, just use the command