-
Notifications
You must be signed in to change notification settings - Fork 42
/
user-manual.html
2566 lines (2566 loc) · 259 KB
/
user-manual.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 PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>Git User’s Manual (for version 1.5.3 or newer)</title><link rel="stylesheet" href="docbook-xsl.css" type="text/css"><meta name="generator" content="DocBook XSL Stylesheets V1.75.2"></head><body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"><div lang="en" class="book" title="Git User’s Manual (for version 1.5.3 or newer)"><div class="titlepage"><div><div><h1 class="title"><a name="id406383"></a>Git User’s Manual (for version 1.5.3 or newer)</h1></div></div><hr></div><div class="toc"><p><b>Table of Contents</b></p><dl><dt><span class="preface"><a href="#id406393"></a></span></dt><dt><span class="chapter"><a href="#repositories-and-branches">1. Repositories and Branches</a></span></dt><dd><dl><dt><span class="section"><a href="#how-to-get-a-git-repository">How to get a git repository</a></span></dt><dt><span class="section"><a href="#how-to-check-out">How to check out a different version of a project</a></span></dt><dt><span class="section"><a href="#understanding-commits">Understanding History: Commits</a></span></dt><dd><dl><dt><span class="section"><a href="#understanding-reachability">Understanding history: commits, parents, and reachability</a></span></dt><dt><span class="section"><a href="#history-diagrams">Understanding history: History diagrams</a></span></dt><dt><span class="section"><a href="#what-is-a-branch">Understanding history: What is a branch?</a></span></dt></dl></dd><dt><span class="section"><a href="#manipulating-branches">Manipulating branches</a></span></dt><dt><span class="section"><a href="#detached-head">Examining an old version without creating a new branch</a></span></dt><dt><span class="section"><a href="#examining-remote-branches">Examining branches from a remote repository</a></span></dt><dt><span class="section"><a href="#how-git-stores-references">Naming branches, tags, and other references</a></span></dt><dt><span class="section"><a href="#Updating-a-repository-With-git-fetch">Updating a repository with git fetch</a></span></dt><dt><span class="section"><a href="#fetching-branches">Fetching branches from other repositories</a></span></dt></dl></dd><dt><span class="chapter"><a href="#exploring-git-history">2. Exploring git history</a></span></dt><dd><dl><dt><span class="section"><a href="#using-bisect">How to use bisect to find a regression</a></span></dt><dt><span class="section"><a href="#naming-commits">Naming commits</a></span></dt><dt><span class="section"><a href="#creating-tags">Creating tags</a></span></dt><dt><span class="section"><a href="#browsing-revisions">Browsing revisions</a></span></dt><dt><span class="section"><a href="#generating-diffs">Generating diffs</a></span></dt><dt><span class="section"><a href="#viewing-old-file-versions">Viewing old file versions</a></span></dt><dt><span class="section"><a href="#history-examples">Examples</a></span></dt><dd><dl><dt><span class="section"><a href="#counting-commits-on-a-branch">Counting the number of commits on a branch</a></span></dt><dt><span class="section"><a href="#checking-for-equal-branches">Check whether two branches point at the same history</a></span></dt><dt><span class="section"><a href="#finding-tagged-descendants">Find first tagged version including a given fix</a></span></dt><dt><span class="section"><a href="#showing-commits-unique-to-a-branch">Showing commits unique to a given branch</a></span></dt><dt><span class="section"><a href="#making-a-release">Creating a changelog and tarball for a software release</a></span></dt><dt><span class="section"><a href="#Finding-commits-With-given-Content">Finding commits referencing a file with given content</a></span></dt></dl></dd></dl></dd><dt><span class="chapter"><a href="#Developing-With-git">3. Developing with git</a></span></dt><dd><dl><dt><span class="section"><a href="#telling-git-your-name">Telling git your name</a></span></dt><dt><span class="section"><a href="#creating-a-new-repository">Creating a new repository</a></span></dt><dt><span class="section"><a href="#how-to-make-a-commit">How to make a commit</a></span></dt><dt><span class="section"><a href="#creating-good-commit-messages">Creating good commit messages</a></span></dt><dt><span class="section"><a href="#ignoring-files">Ignoring files</a></span></dt><dt><span class="section"><a href="#how-to-merge">How to merge</a></span></dt><dt><span class="section"><a href="#resolving-a-merge">Resolving a merge</a></span></dt><dd><dl><dt><span class="section"><a href="#conflict-resolution">Getting conflict-resolution help during a merge</a></span></dt></dl></dd><dt><span class="section"><a href="#undoing-a-merge">Undoing a merge</a></span></dt><dt><span class="section"><a href="#fast-forwards">Fast-forward merges</a></span></dt><dt><span class="section"><a href="#fixing-mistakes">Fixing mistakes</a></span></dt><dd><dl><dt><span class="section"><a href="#reverting-a-commit">Fixing a mistake with a new commit</a></span></dt><dt><span class="section"><a href="#fixing-a-mistake-by-rewriting-history">Fixing a mistake by rewriting history</a></span></dt><dt><span class="section"><a href="#checkout-of-path">Checking out an old version of a file</a></span></dt><dt><span class="section"><a href="#interrupted-work">Temporarily setting aside work in progress</a></span></dt></dl></dd><dt><span class="section"><a href="#ensuring-good-performance">Ensuring good performance</a></span></dt><dt><span class="section"><a href="#ensuring-reliability">Ensuring reliability</a></span></dt><dd><dl><dt><span class="section"><a href="#checking-for-corruption">Checking the repository for corruption</a></span></dt><dt><span class="section"><a href="#recovering-lost-changes">Recovering lost changes</a></span></dt></dl></dd></dl></dd><dt><span class="chapter"><a href="#sharing-development">4. Sharing development with others</a></span></dt><dd><dl><dt><span class="section"><a href="#getting-updates-With-git-pull">Getting updates with git pull</a></span></dt><dt><span class="section"><a href="#submitting-patches">Submitting patches to a project</a></span></dt><dt><span class="section"><a href="#importing-patches">Importing patches to a project</a></span></dt><dt><span class="section"><a href="#public-repositories">Public git repositories</a></span></dt><dd><dl><dt><span class="section"><a href="#setting-up-a-public-repository">Setting up a public repository</a></span></dt><dt><span class="section"><a href="#exporting-via-git">Exporting a git repository via the git protocol</a></span></dt><dt><span class="section"><a href="#exporting-via-http">Exporting a git repository via http</a></span></dt><dt><span class="section"><a href="#pushing-changes-to-a-public-repository">Pushing changes to a public repository</a></span></dt><dt><span class="section"><a href="#forcing-push">What to do when a push fails</a></span></dt><dt><span class="section"><a href="#setting-up-a-shared-repository">Setting up a shared repository</a></span></dt><dt><span class="section"><a href="#setting-up-gitweb">Allowing web browsing of a repository</a></span></dt></dl></dd><dt><span class="section"><a href="#sharing-development-examples">Examples</a></span></dt><dd><dl><dt><span class="section"><a href="#maintaining-topic-branches">Maintaining topic branches for a Linux subsystem maintainer</a></span></dt></dl></dd></dl></dd><dt><span class="chapter"><a href="#cleaning-up-history">5. Rewriting history and maintaining patch series</a></span></dt><dd><dl><dt><span class="section"><a href="#patch-series">Creating the perfect patch series</a></span></dt><dt><span class="section"><a href="#using-git-rebase">Keeping a patch series up to date using git rebase</a></span></dt><dt><span class="section"><a href="#rewriting-one-commit">Rewriting a single commit</a></span></dt><dt><span class="section"><a href="#reordering-patch-series">Reordering or selecting from a patch series</a></span></dt><dt><span class="section"><a href="#patch-series-tools">Other tools</a></span></dt><dt><span class="section"><a href="#problems-With-rewriting-history">Problems with rewriting history</a></span></dt><dt><span class="section"><a href="#bisect-merges">Why bisecting merge commits can be harder than bisecting linear history</a></span></dt></dl></dd><dt><span class="chapter"><a href="#advanced-branch-management">6. Advanced branch management</a></span></dt><dd><dl><dt><span class="section"><a href="#fetching-individual-branches">Fetching individual branches</a></span></dt><dt><span class="section"><a href="#fetch-fast-forwards">git fetch and fast-forwards</a></span></dt><dt><span class="section"><a href="#forcing-fetch">Forcing git fetch to do non-fast-forward updates</a></span></dt><dt><span class="section"><a href="#remote-branch-configuration">Configuring remote-tracking branches</a></span></dt></dl></dd><dt><span class="chapter"><a href="#git-concepts">7. Git concepts</a></span></dt><dd><dl><dt><span class="section"><a href="#the-object-database">The Object Database</a></span></dt><dd><dl><dt><span class="section"><a href="#commit-object">Commit Object</a></span></dt><dt><span class="section"><a href="#tree-object">Tree Object</a></span></dt><dt><span class="section"><a href="#blob-object">Blob Object</a></span></dt><dt><span class="section"><a href="#trust">Trust</a></span></dt><dt><span class="section"><a href="#tag-object">Tag Object</a></span></dt><dt><span class="section"><a href="#pack-files">How git stores objects efficiently: pack files</a></span></dt><dt><span class="section"><a href="#dangling-objects">Dangling objects</a></span></dt><dt><span class="section"><a href="#recovering-from-repository-corruption">Recovering from repository corruption</a></span></dt></dl></dd><dt><span class="section"><a href="#the-index">The index</a></span></dt></dl></dd><dt><span class="chapter"><a href="#submodules">8. Submodules</a></span></dt><dd><dl><dt><span class="section"><a href="#_pitfalls_with_submodules">Pitfalls with submodules</a></span></dt></dl></dd><dt><span class="chapter"><a href="#low-level-operations">9. Low-level git operations</a></span></dt><dd><dl><dt><span class="section"><a href="#object-manipulation">Object access and manipulation</a></span></dt><dt><span class="section"><a href="#the-workflow">The Workflow</a></span></dt><dd><dl><dt><span class="section"><a href="#working-directory-to-index">working directory → index</a></span></dt><dt><span class="section"><a href="#index-to-object-database">index → object database</a></span></dt><dt><span class="section"><a href="#object-database-to-index">object database → index</a></span></dt><dt><span class="section"><a href="#index-to-working-directory">index → working directory</a></span></dt><dt><span class="section"><a href="#tying-it-all-together">Tying it all together</a></span></dt></dl></dd><dt><span class="section"><a href="#examining-the-data">Examining the data</a></span></dt><dt><span class="section"><a href="#merging-multiple-trees">Merging multiple trees</a></span></dt><dt><span class="section"><a href="#merging-multiple-trees-2">Merging multiple trees, continued</a></span></dt></dl></dd><dt><span class="chapter"><a href="#hacking-git">10. Hacking git</a></span></dt><dd><dl><dt><span class="section"><a href="#object-details">Object storage format</a></span></dt><dt><span class="section"><a href="#birdview-on-the-source-code">A birds-eye view of Git’s source code</a></span></dt></dl></dd><dt><span class="chapter"><a href="#glossary">11. Git Glossary</a></span></dt><dt><span class="appendix"><a href="#git-quick-start">A. Git Quick Reference</a></span></dt><dd><dl><dt><span class="section"><a href="#quick-creating-a-new-repository">Creating a new repository</a></span></dt><dt><span class="section"><a href="#managing-branches">Managing branches</a></span></dt><dt><span class="section"><a href="#exploring-history">Exploring history</a></span></dt><dt><span class="section"><a href="#making-changes">Making changes</a></span></dt><dt><span class="section"><a href="#merging">Merging</a></span></dt><dt><span class="section"><a href="#sharing-your-changes">Sharing your changes</a></span></dt><dt><span class="section"><a href="#repository-maintenance">Repository maintenance</a></span></dt></dl></dd><dt><span class="appendix"><a href="#todo">B. Notes and todo list for this manual</a></span></dt></dl></div><div class="preface"><div class="titlepage"><div><div><h2 class="title"><a name="id406393"></a></h2></div></div></div><p>Git is a fast distributed revision control system.</p><p>This manual is designed to be readable by someone with basic UNIX
command-line skills, but no previous knowledge of git.</p><p><a class="xref" href="#repositories-and-branches" title="Chapter 1. Repositories and Branches">Chapter 1, <i>Repositories and Branches</i></a> and <a class="xref" href="#exploring-git-history" title="Chapter 2. Exploring git history">Chapter 2, <i>Exploring git history</i></a> explain how
to fetch and study a project using git—read these chapters to learn how
to build and test a particular version of a software project, search for
regressions, and so on.</p><p>People needing to do actual development will also want to read
<a class="xref" href="#Developing-With-git" title="Chapter 3. Developing with git">Chapter 3, <i>Developing with git</i></a> and <a class="xref" href="#sharing-development" title="Chapter 4. Sharing development with others">Chapter 4, <i>Sharing development with others</i></a>.</p><p>Further chapters cover more specialized topics.</p><p>Comprehensive reference documentation is available through the man
pages, or <a class="ulink" href="git-help.html" target="_top">git-help(1)</a> command. For example, for the command
"git clone <repo>", you can either use:</p><div class="literallayout"><p>$ man git-clone</p></div><p>or:</p><div class="literallayout"><p>$ git help clone</p></div><p>With the latter, you can use the manual viewer of your choice; see
<a class="ulink" href="git-help.html" target="_top">git-help(1)</a> for more information.</p><p>See also <a class="xref" href="#git-quick-start" title="Appendix A. Git Quick Reference">Appendix A, <i>Git Quick Reference</i></a> for a brief overview of git commands,
without any explanation.</p><p>Finally, see <a class="xref" href="#todo" title="Appendix B. Notes and todo list for this manual">Appendix B, <i>Notes and todo list for this manual</i></a> for ways that you can help make this manual more
complete.</p></div><div class="chapter" title="Chapter 1. Repositories and Branches"><div class="titlepage"><div><div><h2 class="title"><a name="repositories-and-branches"></a>Chapter 1. Repositories and Branches</h2></div></div></div><div class="toc"><p><b>Table of Contents</b></p><dl><dt><span class="section"><a href="#how-to-get-a-git-repository">How to get a git repository</a></span></dt><dt><span class="section"><a href="#how-to-check-out">How to check out a different version of a project</a></span></dt><dt><span class="section"><a href="#understanding-commits">Understanding History: Commits</a></span></dt><dd><dl><dt><span class="section"><a href="#understanding-reachability">Understanding history: commits, parents, and reachability</a></span></dt><dt><span class="section"><a href="#history-diagrams">Understanding history: History diagrams</a></span></dt><dt><span class="section"><a href="#what-is-a-branch">Understanding history: What is a branch?</a></span></dt></dl></dd><dt><span class="section"><a href="#manipulating-branches">Manipulating branches</a></span></dt><dt><span class="section"><a href="#detached-head">Examining an old version without creating a new branch</a></span></dt><dt><span class="section"><a href="#examining-remote-branches">Examining branches from a remote repository</a></span></dt><dt><span class="section"><a href="#how-git-stores-references">Naming branches, tags, and other references</a></span></dt><dt><span class="section"><a href="#Updating-a-repository-With-git-fetch">Updating a repository with git fetch</a></span></dt><dt><span class="section"><a href="#fetching-branches">Fetching branches from other repositories</a></span></dt></dl></div><div class="section" title="How to get a git repository"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="how-to-get-a-git-repository"></a>How to get a git repository</h2></div></div></div><p>It will be useful to have a git repository to experiment with as you
read this manual.</p><p>The best way to get one is by using the <a class="ulink" href="git-clone.html" target="_top">git-clone(1)</a> command to
download a copy of an existing repository. If you don’t already have a
project in mind, here are some interesting examples:</p><div class="literallayout"><p> # git itself (approx. 10MB download):<br>
$ git clone git://git.kernel.org/pub/scm/git/git.git<br>
# the Linux kernel (approx. 150MB download):<br>
$ git clone git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6.git</p></div><p>The initial clone may be time-consuming for a large project, but you
will only need to clone once.</p><p>The clone command creates a new directory named after the project ("git"
or "linux-2.6" in the examples above). After you cd into this
directory, you will see that it contains a copy of the project files,
called the <a class="link" href="#def_working_tree">working tree</a>, together with a special
top-level directory named ".git", which contains all the information
about the history of the project.</p></div><div class="section" title="How to check out a different version of a project"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="how-to-check-out"></a>How to check out a different version of a project</h2></div></div></div><p>Git is best thought of as a tool for storing the history of a collection
of files. It stores the history as a compressed collection of
interrelated snapshots of the project’s contents. In git each such
version is called a <a class="link" href="#def_commit">commit</a>.</p><p>Those snapshots aren’t necessarily all arranged in a single line from
oldest to newest; instead, work may simultaneously proceed along
parallel lines of development, called <a class="link" href="#def_branch">branches</a>, which may
merge and diverge.</p><p>A single git repository can track development on multiple branches. It
does this by keeping a list of <a class="link" href="#def_head">heads</a> which reference the
latest commit on each branch; the <a class="ulink" href="git-branch.html" target="_top">git-branch(1)</a> command shows
you the list of branch heads:</p><div class="literallayout"><p>$ git branch<br>
* master</p></div><p>A freshly cloned repository contains a single branch head, by default
named "master", with the working directory initialized to the state of
the project referred to by that branch head.</p><p>Most projects also use <a class="link" href="#def_tag">tags</a>. Tags, like heads, are
references into the project’s history, and can be listed using the
<a class="ulink" href="git-tag.html" target="_top">git-tag(1)</a> command:</p><div class="literallayout"><p>$ git tag -l<br>
v2.6.11<br>
v2.6.11-tree<br>
v2.6.12<br>
v2.6.12-rc2<br>
v2.6.12-rc3<br>
v2.6.12-rc4<br>
v2.6.12-rc5<br>
v2.6.12-rc6<br>
v2.6.13<br>
...</p></div><p>Tags are expected to always point at the same version of a project,
while heads are expected to advance as development progresses.</p><p>Create a new branch head pointing to one of these versions and check it
out using <a class="ulink" href="git-checkout.html" target="_top">git-checkout(1)</a>:</p><div class="literallayout"><p>$ git checkout -b new v2.6.13</p></div><p>The working directory then reflects the contents that the project had
when it was tagged v2.6.13, and <a class="ulink" href="git-branch.html" target="_top">git-branch(1)</a> shows two
branches, with an asterisk marking the currently checked-out branch:</p><div class="literallayout"><p>$ git branch<br>
master<br>
* new</p></div><p>If you decide that you’d rather see version 2.6.17, you can modify
the current branch to point at v2.6.17 instead, with</p><div class="literallayout"><p>$ git reset --hard v2.6.17</p></div><p>Note that if the current branch head was your only reference to a
particular point in history, then resetting that branch may leave you
with no way to find the history it used to point to; so use this command
carefully.</p></div><div class="section" title="Understanding History: Commits"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="understanding-commits"></a>Understanding History: Commits</h2></div></div></div><p>Every change in the history of a project is represented by a commit.
The <a class="ulink" href="git-show.html" target="_top">git-show(1)</a> command shows the most recent commit on the
current branch:</p><div class="literallayout"><p>$ git show<br>
commit 17cf781661e6d38f737f15f53ab552f1e95960d7<br>
Author: Linus Torvalds <torvalds@ppc970.osdl.org.(none)><br>
Date: Tue Apr 19 14:11:06 2005 -0700<br>
<br>
Remove duplicate getenv(DB_ENVIRONMENT) call<br>
<br>
Noted by Tony Luck.<br>
<br>
diff --git a/init-db.c b/init-db.c<br>
index 65898fa..b002dc6 100644<br>
--- a/init-db.c<br>
+++ b/init-db.c<br>
@@ -7,7 +7,7 @@<br>
<br>
int main(int argc, char **argv)<br>
{<br>
- char *sha1_dir = getenv(DB_ENVIRONMENT), *path;<br>
+ char *sha1_dir, *path;<br>
int len, i;<br>
<br>
if (mkdir(".git", 0755) < 0) {</p></div><p>As you can see, a commit shows who made the latest change, what they
did, and why.</p><p>Every commit has a 40-hexdigit id, sometimes called the "object name" or the
"SHA-1 id", shown on the first line of the "git show" output. You can usually
refer to a commit by a shorter name, such as a tag or a branch name, but this
longer name can also be useful. Most importantly, it is a globally unique
name for this commit: so if you tell somebody else the object name (for
example in email), then you are guaranteed that name will refer to the same
commit in their repository that it does in yours (assuming their repository
has that commit at all). Since the object name is computed as a hash over the
contents of the commit, you are guaranteed that the commit can never change
without its name also changing.</p><p>In fact, in <a class="xref" href="#git-concepts" title="Chapter 7. Git concepts">Chapter 7, <i>Git concepts</i></a> we shall see that everything stored in git
history, including file data and directory contents, is stored in an object
with a name that is a hash of its contents.</p><div class="section" title="Understanding history: commits, parents, and reachability"><div class="titlepage"><div><div><h3 class="title"><a name="understanding-reachability"></a>Understanding history: commits, parents, and reachability</h3></div></div></div><p>Every commit (except the very first commit in a project) also has a
parent commit which shows what happened before this commit.
Following the chain of parents will eventually take you back to the
beginning of the project.</p><p>However, the commits do not form a simple list; git allows lines of
development to diverge and then reconverge, and the point where two
lines of development reconverge is called a "merge". The commit
representing a merge can therefore have more than one parent, with
each parent representing the most recent commit on one of the lines
of development leading to that point.</p><p>The best way to see how this works is using the <a class="ulink" href="gitk.html" target="_top">gitk(1)</a>
command; running gitk now on a git repository and looking for merge
commits will help understand how the git organizes history.</p><p>In the following, we say that commit X is "reachable" from commit Y
if commit X is an ancestor of commit Y. Equivalently, you could say
that Y is a descendant of X, or that there is a chain of parents
leading from commit Y to commit X.</p></div><div class="section" title="Understanding history: History diagrams"><div class="titlepage"><div><div><h3 class="title"><a name="history-diagrams"></a>Understanding history: History diagrams</h3></div></div></div><p>We will sometimes represent git history using diagrams like the one
below. Commits are shown as "o", and the links between them with
lines drawn with - / and \. Time goes left to right:</p><pre class="literallayout"> o--o--o <-- Branch A
/
o--o--o <-- master
\
o--o--o <-- Branch B</pre><p>If we need to talk about a particular commit, the character "o" may
be replaced with another letter or number.</p></div><div class="section" title="Understanding history: What is a branch?"><div class="titlepage"><div><div><h3 class="title"><a name="what-is-a-branch"></a>Understanding history: What is a branch?</h3></div></div></div><p>When we need to be precise, we will use the word "branch" to mean a line
of development, and "branch head" (or just "head") to mean a reference
to the most recent commit on a branch. In the example above, the branch
head named "A" is a pointer to one particular commit, but we refer to
the line of three commits leading up to that point as all being part of
"branch A".</p><p>However, when no confusion will result, we often just use the term
"branch" both for branches and for branch heads.</p></div></div><div class="section" title="Manipulating branches"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="manipulating-branches"></a>Manipulating branches</h2></div></div></div><p>Creating, deleting, and modifying branches is quick and easy; here’s
a summary of the commands:</p><div class="variablelist"><dl><dt><span class="term">
git branch
</span></dt><dd>
list all branches
</dd><dt><span class="term">
git branch <branch>
</span></dt><dd>
create a new branch named <branch>, referencing the same
point in history as the current branch
</dd><dt><span class="term">
git branch <branch> <start-point>
</span></dt><dd>
create a new branch named <branch>, referencing
<start-point>, which may be specified any way you like,
including using a branch name or a tag name
</dd><dt><span class="term">
git branch -d <branch>
</span></dt><dd>
delete the branch <branch>; if the branch you are deleting
points to a commit which is not reachable from the current
branch, this command will fail with a warning.
</dd><dt><span class="term">
git branch -D <branch>
</span></dt><dd>
even if the branch points to a commit not reachable
from the current branch, you may know that that commit
is still reachable from some other branch or tag. In that
case it is safe to use this command to force git to delete
the branch.
</dd><dt><span class="term">
git checkout <branch>
</span></dt><dd>
make the current branch <branch>, updating the working
directory to reflect the version referenced by <branch>
</dd><dt><span class="term">
git checkout -b <new> <start-point>
</span></dt><dd>
create a new branch <new> referencing <start-point>, and
check it out.
</dd></dl></div><p>The special symbol "HEAD" can always be used to refer to the current
branch. In fact, git uses a file named "HEAD" in the .git directory to
remember which branch is current:</p><div class="literallayout"><p>$ cat .git/HEAD<br>
ref: refs/heads/master</p></div></div><div class="section" title="Examining an old version without creating a new branch"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="detached-head"></a>Examining an old version without creating a new branch</h2></div></div></div><p>The <code class="literal">git checkout</code> command normally expects a branch head, but will also
accept an arbitrary commit; for example, you can check out the commit
referenced by a tag:</p><div class="literallayout"><p>$ git checkout v2.6.17<br>
Note: moving to "v2.6.17" which isn't a local branch<br>
If you want to create a new branch from this checkout, you may do so<br>
(now or later) by using -b with the checkout command again. Example:<br>
git checkout -b <new_branch_name><br>
HEAD is now at 427abfa... Linux v2.6.17</p></div><p>The HEAD then refers to the SHA-1 of the commit instead of to a branch,
and git branch shows that you are no longer on a branch:</p><div class="literallayout"><p>$ cat .git/HEAD<br>
427abfa28afedffadfca9dd8b067eb6d36bac53f<br>
$ git branch<br>
* (no branch)<br>
master</p></div><p>In this case we say that the HEAD is "detached".</p><p>This is an easy way to check out a particular version without having to
make up a name for the new branch. You can still create a new branch
(or tag) for this version later if you decide to.</p></div><div class="section" title="Examining branches from a remote repository"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="examining-remote-branches"></a>Examining branches from a remote repository</h2></div></div></div><p>The "master" branch that was created at the time you cloned is a copy
of the HEAD in the repository that you cloned from. That repository
may also have had other branches, though, and your local repository
keeps branches which track each of those remote branches, called
remote-tracking branches, which you
can view using the "-r" option to <a class="ulink" href="git-branch.html" target="_top">git-branch(1)</a>:</p><div class="literallayout"><p>$ git branch -r<br>
origin/HEAD<br>
origin/html<br>
origin/maint<br>
origin/man<br>
origin/master<br>
origin/next<br>
origin/pu<br>
origin/todo</p></div><p>In this example, "origin" is called a remote repository, or "remote"
for short. The branches of this repository are called "remote
branches" from our point of view. The remote-tracking branches listed
above were created based on the remote branches at clone time and will
be updated by "git fetch" (hence "git pull") and "git push". See
<a class="xref" href="#Updating-a-repository-With-git-fetch" title="Updating a repository with git fetch">the section called “Updating a repository with git fetch”</a> for details.</p><p>You might want to build on one of these remote-tracking branches
on a branch of your own, just as you would for a tag:</p><div class="literallayout"><p>$ git checkout -b my-todo-copy origin/todo</p></div><p>You can also check out "origin/todo" directly to examine it or
write a one-off patch. See <a class="link" href="#detached-head" title="Examining an old version without creating a new branch">detached head</a>.</p><p>Note that the name "origin" is just the name that git uses by default
to refer to the repository that you cloned from.</p></div><div class="section" title="Naming branches, tags, and other references"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="how-git-stores-references"></a>Naming branches, tags, and other references</h2></div></div></div><p>Branches, remote-tracking branches, and tags are all references to
commits. All references are named with a slash-separated path name
starting with "refs"; the names we’ve been using so far are actually
shorthand:</p><div class="itemizedlist"><ul class="itemizedlist" type="disc"><li class="listitem">
The branch "test" is short for "refs/heads/test".
</li><li class="listitem">
The tag "v2.6.18" is short for "refs/tags/v2.6.18".
</li><li class="listitem">
"origin/master" is short for "refs/remotes/origin/master".
</li></ul></div><p>The full name is occasionally useful if, for example, there ever
exists a tag and a branch with the same name.</p><p>(Newly created refs are actually stored in the .git/refs directory,
under the path given by their name. However, for efficiency reasons
they may also be packed together in a single file; see
<a class="ulink" href="git-pack-refs.html" target="_top">git-pack-refs(1)</a>).</p><p>As another useful shortcut, the "HEAD" of a repository can be referred
to just using the name of that repository. So, for example, "origin"
is usually a shortcut for the HEAD branch in the repository "origin".</p><p>For the complete list of paths which git checks for references, and
the order it uses to decide which to choose when there are multiple
references with the same shorthand name, see the "SPECIFYING
REVISIONS" section of <a class="ulink" href="gitrevisions.html" target="_top">gitrevisions(7)</a>.</p></div><div class="section" title="Updating a repository with git fetch"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="Updating-a-repository-With-git-fetch"></a>Updating a repository with git fetch</h2></div></div></div><p>Eventually the developer cloned from will do additional work in her
repository, creating new commits and advancing the branches to point
at the new commits.</p><p>The command "git fetch", with no arguments, will update all of the
remote-tracking branches to the latest version found in her
repository. It will not touch any of your own branches—not even the
"master" branch that was created for you on clone.</p></div><div class="section" title="Fetching branches from other repositories"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="fetching-branches"></a>Fetching branches from other repositories</h2></div></div></div><p>You can also track branches from repositories other than the one you
cloned from, using <a class="ulink" href="git-remote.html" target="_top">git-remote(1)</a>:</p><div class="literallayout"><p>$ git remote add linux-nfs git://linux-nfs.org/pub/nfs-2.6.git<br>
$ git fetch linux-nfs<br>
* refs/remotes/linux-nfs/master: storing branch 'master' ...<br>
commit: bf81b46</p></div><p>New remote-tracking branches will be stored under the shorthand name
that you gave "git remote add", in this case linux-nfs:</p><div class="literallayout"><p>$ git branch -r<br>
linux-nfs/master<br>
origin/master</p></div><p>If you run "git fetch <remote>" later, the remote-tracking branches for the
named <remote> will be updated.</p><p>If you examine the file .git/config, you will see that git has added
a new stanza:</p><div class="literallayout"><p>$ cat .git/config<br>
...<br>
[remote "linux-nfs"]<br>
url = git://linux-nfs.org/pub/nfs-2.6.git<br>
fetch = +refs/heads/*:refs/remotes/linux-nfs/*<br>
...</p></div><p>This is what causes git to track the remote’s branches; you may modify
or delete these configuration options by editing .git/config with a
text editor. (See the "CONFIGURATION FILE" section of
<a class="ulink" href="git-config.html" target="_top">git-config(1)</a> for details.)</p></div></div><div class="chapter" title="Chapter 2. Exploring git history"><div class="titlepage"><div><div><h2 class="title"><a name="exploring-git-history"></a>Chapter 2. Exploring git history</h2></div></div></div><div class="toc"><p><b>Table of Contents</b></p><dl><dt><span class="section"><a href="#using-bisect">How to use bisect to find a regression</a></span></dt><dt><span class="section"><a href="#naming-commits">Naming commits</a></span></dt><dt><span class="section"><a href="#creating-tags">Creating tags</a></span></dt><dt><span class="section"><a href="#browsing-revisions">Browsing revisions</a></span></dt><dt><span class="section"><a href="#generating-diffs">Generating diffs</a></span></dt><dt><span class="section"><a href="#viewing-old-file-versions">Viewing old file versions</a></span></dt><dt><span class="section"><a href="#history-examples">Examples</a></span></dt><dd><dl><dt><span class="section"><a href="#counting-commits-on-a-branch">Counting the number of commits on a branch</a></span></dt><dt><span class="section"><a href="#checking-for-equal-branches">Check whether two branches point at the same history</a></span></dt><dt><span class="section"><a href="#finding-tagged-descendants">Find first tagged version including a given fix</a></span></dt><dt><span class="section"><a href="#showing-commits-unique-to-a-branch">Showing commits unique to a given branch</a></span></dt><dt><span class="section"><a href="#making-a-release">Creating a changelog and tarball for a software release</a></span></dt><dt><span class="section"><a href="#Finding-commits-With-given-Content">Finding commits referencing a file with given content</a></span></dt></dl></dd></dl></div><p>Git is best thought of as a tool for storing the history of a
collection of files. It does this by storing compressed snapshots of
the contents of a file hierarchy, together with "commits" which show
the relationships between these snapshots.</p><p>Git provides extremely flexible and fast tools for exploring the
history of a project.</p><p>We start with one specialized tool that is useful for finding the
commit that introduced a bug into a project.</p><div class="section" title="How to use bisect to find a regression"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="using-bisect"></a>How to use bisect to find a regression</h2></div></div></div><p>Suppose version 2.6.18 of your project worked, but the version at
"master" crashes. Sometimes the best way to find the cause of such a
regression is to perform a brute-force search through the project’s
history to find the particular commit that caused the problem. The
<a class="ulink" href="git-bisect.html" target="_top">git-bisect(1)</a> command can help you do this:</p><div class="literallayout"><p>$ git bisect start<br>
$ git bisect good v2.6.18<br>
$ git bisect bad master<br>
Bisecting: 3537 revisions left to test after this<br>
[65934a9a028b88e83e2b0f8b36618fe503349f8e] BLOCK: Make USB storage depend on SCSI rather than selecting it [try #6]</p></div><p>If you run "git branch" at this point, you’ll see that git has
temporarily moved you in "(no branch)". HEAD is now detached from any
branch and points directly to a commit (with commit id 65934…) that
is reachable from "master" but not from v2.6.18. Compile and test it,
and see whether it crashes. Assume it does crash. Then:</p><div class="literallayout"><p>$ git bisect bad<br>
Bisecting: 1769 revisions left to test after this<br>
[7eff82c8b1511017ae605f0c99ac275a7e21b867] i2c-core: Drop useless bitmaskings</p></div><p>checks out an older version. Continue like this, telling git at each
stage whether the version it gives you is good or bad, and notice
that the number of revisions left to test is cut approximately in
half each time.</p><p>After about 13 tests (in this case), it will output the commit id of
the guilty commit. You can then examine the commit with
<a class="ulink" href="git-show.html" target="_top">git-show(1)</a>, find out who wrote it, and mail them your bug
report with the commit id. Finally, run</p><div class="literallayout"><p>$ git bisect reset</p></div><p>to return you to the branch you were on before.</p><p>Note that the version which <code class="literal">git bisect</code> checks out for you at each
point is just a suggestion, and you’re free to try a different
version if you think it would be a good idea. For example,
occasionally you may land on a commit that broke something unrelated;
run</p><div class="literallayout"><p>$ git bisect visualize</p></div><p>which will run gitk and label the commit it chose with a marker that
says "bisect". Choose a safe-looking commit nearby, note its commit
id, and check it out with:</p><div class="literallayout"><p>$ git reset --hard fb47ddb2db...</p></div><p>then test, run "bisect good" or "bisect bad" as appropriate, and
continue.</p><p>Instead of "git bisect visualize" and then "git reset --hard
fb47ddb2db…", you might just want to tell git that you want to skip
the current commit:</p><div class="literallayout"><p>$ git bisect skip</p></div><p>In this case, though, git may not eventually be able to tell the first
bad one between some first skipped commits and a later bad commit.</p><p>There are also ways to automate the bisecting process if you have a
test script that can tell a good from a bad commit. See
<a class="ulink" href="git-bisect.html" target="_top">git-bisect(1)</a> for more information about this and other "git
bisect" features.</p></div><div class="section" title="Naming commits"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="naming-commits"></a>Naming commits</h2></div></div></div><p>We have seen several ways of naming commits already:</p><div class="itemizedlist"><ul class="itemizedlist" type="disc"><li class="listitem">
40-hexdigit object name
</li><li class="listitem">
branch name: refers to the commit at the head of the given
branch
</li><li class="listitem">
tag name: refers to the commit pointed to by the given tag
(we’ve seen branches and tags are special cases of
<a class="link" href="#how-git-stores-references" title="Naming branches, tags, and other references">references</a>).
</li><li class="listitem">
HEAD: refers to the head of the current branch
</li></ul></div><p>There are many more; see the "SPECIFYING REVISIONS" section of the
<a class="ulink" href="gitrevisions.html" target="_top">gitrevisions(7)</a> man page for the complete list of ways to
name revisions. Some examples:</p><div class="literallayout"><p>$ git show fb47ddb2 # the first few characters of the object name<br>
# are usually enough to specify it uniquely<br>
$ git show HEAD^ # the parent of the HEAD commit<br>
$ git show HEAD^^ # the grandparent<br>
$ git show HEAD~4 # the great-great-grandparent</p></div><p>Recall that merge commits may have more than one parent; by default,
^ and ~ follow the first parent listed in the commit, but you can
also choose:</p><div class="literallayout"><p>$ git show HEAD^1 # show the first parent of HEAD<br>
$ git show HEAD^2 # show the second parent of HEAD</p></div><p>In addition to HEAD, there are several other special names for
commits:</p><p>Merges (to be discussed later), as well as operations such as
<code class="literal">git reset</code>, which change the currently checked-out commit, generally
set ORIG_HEAD to the value HEAD had before the current operation.</p><p>The <code class="literal">git fetch</code> operation always stores the head of the last fetched
branch in FETCH_HEAD. For example, if you run <code class="literal">git fetch</code> without
specifying a local branch as the target of the operation</p><div class="literallayout"><p>$ git fetch git://example.com/proj.git theirbranch</p></div><p>the fetched commits will still be available from FETCH_HEAD.</p><p>When we discuss merges we’ll also see the special name MERGE_HEAD,
which refers to the other branch that we’re merging in to the current
branch.</p><p>The <a class="ulink" href="git-rev-parse.html" target="_top">git-rev-parse(1)</a> command is a low-level command that is
occasionally useful for translating some name for a commit to the object
name for that commit:</p><div class="literallayout"><p>$ git rev-parse origin<br>
e05db0fd4f31dde7005f075a84f96b360d05984b</p></div></div><div class="section" title="Creating tags"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="creating-tags"></a>Creating tags</h2></div></div></div><p>We can also create a tag to refer to a particular commit; after
running</p><div class="literallayout"><p>$ git tag stable-1 1b2e1d63ff</p></div><p>You can use stable-1 to refer to the commit 1b2e1d63ff.</p><p>This creates a "lightweight" tag. If you would also like to include a
comment with the tag, and possibly sign it cryptographically, then you
should create a tag object instead; see the <a class="ulink" href="git-tag.html" target="_top">git-tag(1)</a> man page
for details.</p></div><div class="section" title="Browsing revisions"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="browsing-revisions"></a>Browsing revisions</h2></div></div></div><p>The <a class="ulink" href="git-log.html" target="_top">git-log(1)</a> command can show lists of commits. On its
own, it shows all commits reachable from the parent commit; but you
can also make more specific requests:</p><div class="literallayout"><p>$ git log v2.5.. # commits since (not reachable from) v2.5<br>
$ git log test..master # commits reachable from master but not test<br>
$ git log master..test # ...reachable from test but not master<br>
$ git log master...test # ...reachable from either test or master,<br>
# but not both<br>
$ git log --since="2 weeks ago" # commits from the last 2 weeks<br>
$ git log Makefile # commits which modify Makefile<br>
$ git log fs/ # ... which modify any file under fs/<br>
$ git log -S'foo()' # commits which add or remove any file data<br>
# matching the string 'foo()'</p></div><p>And of course you can combine all of these; the following finds
commits since v2.5 which touch the Makefile or any file under fs:</p><div class="literallayout"><p>$ git log v2.5.. Makefile fs/</p></div><p>You can also ask git log to show patches:</p><div class="literallayout"><p>$ git log -p</p></div><p>See the "--pretty" option in the <a class="ulink" href="git-log.html" target="_top">git-log(1)</a> man page for more
display options.</p><p>Note that git log starts with the most recent commit and works
backwards through the parents; however, since git history can contain
multiple independent lines of development, the particular order that
commits are listed in may be somewhat arbitrary.</p></div><div class="section" title="Generating diffs"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="generating-diffs"></a>Generating diffs</h2></div></div></div><p>You can generate diffs between any two versions using
<a class="ulink" href="git-diff.html" target="_top">git-diff(1)</a>:</p><div class="literallayout"><p>$ git diff master..test</p></div><p>That will produce the diff between the tips of the two branches. If
you’d prefer to find the diff from their common ancestor to test, you
can use three dots instead of two:</p><div class="literallayout"><p>$ git diff master...test</p></div><p>Sometimes what you want instead is a set of patches; for this you can
use <a class="ulink" href="git-format-patch.html" target="_top">git-format-patch(1)</a>:</p><div class="literallayout"><p>$ git format-patch master..test</p></div><p>will generate a file with a patch for each commit reachable from test
but not from master.</p></div><div class="section" title="Viewing old file versions"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="viewing-old-file-versions"></a>Viewing old file versions</h2></div></div></div><p>You can always view an old version of a file by just checking out the
correct revision first. But sometimes it is more convenient to be
able to view an old version of a single file without checking
anything out; this command does that:</p><div class="literallayout"><p>$ git show v2.5:fs/locks.c</p></div><p>Before the colon may be anything that names a commit, and after it
may be any path to a file tracked by git.</p></div><div class="section" title="Examples"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="history-examples"></a>Examples</h2></div></div></div><div class="section" title="Counting the number of commits on a branch"><div class="titlepage"><div><div><h3 class="title"><a name="counting-commits-on-a-branch"></a>Counting the number of commits on a branch</h3></div></div></div><p>Suppose you want to know how many commits you’ve made on "mybranch"
since it diverged from "origin":</p><div class="literallayout"><p>$ git log --pretty=oneline origin..mybranch | wc -l</p></div><p>Alternatively, you may often see this sort of thing done with the
lower-level command <a class="ulink" href="git-rev-list.html" target="_top">git-rev-list(1)</a>, which just lists the SHA-1’s
of all the given commits:</p><div class="literallayout"><p>$ git rev-list origin..mybranch | wc -l</p></div></div><div class="section" title="Check whether two branches point at the same history"><div class="titlepage"><div><div><h3 class="title"><a name="checking-for-equal-branches"></a>Check whether two branches point at the same history</h3></div></div></div><p>Suppose you want to check whether two branches point at the same point
in history.</p><div class="literallayout"><p>$ git diff origin..master</p></div><p>will tell you whether the contents of the project are the same at the
two branches; in theory, however, it’s possible that the same project
contents could have been arrived at by two different historical
routes. You could compare the object names:</p><div class="literallayout"><p>$ git rev-list origin<br>
e05db0fd4f31dde7005f075a84f96b360d05984b<br>
$ git rev-list master<br>
e05db0fd4f31dde7005f075a84f96b360d05984b</p></div><p>Or you could recall that the … operator selects all commits
contained reachable from either one reference or the other but not
both: so</p><div class="literallayout"><p>$ git log origin...master</p></div><p>will return no commits when the two branches are equal.</p></div><div class="section" title="Find first tagged version including a given fix"><div class="titlepage"><div><div><h3 class="title"><a name="finding-tagged-descendants"></a>Find first tagged version including a given fix</h3></div></div></div><p>Suppose you know that the commit e05db0fd fixed a certain problem.
You’d like to find the earliest tagged release that contains that
fix.</p><p>Of course, there may be more than one answer—if the history branched
after commit e05db0fd, then there could be multiple "earliest" tagged
releases.</p><p>You could just visually inspect the commits since e05db0fd:</p><div class="literallayout"><p>$ gitk e05db0fd..</p></div><p>Or you can use <a class="ulink" href="git-name-rev.html" target="_top">git-name-rev(1)</a>, which will give the commit a
name based on any tag it finds pointing to one of the commit’s
descendants:</p><div class="literallayout"><p>$ git name-rev --tags e05db0fd<br>
e05db0fd tags/v1.5.0-rc1^0~23</p></div><p>The <a class="ulink" href="git-describe.html" target="_top">git-describe(1)</a> command does the opposite, naming the
revision using a tag on which the given commit is based:</p><div class="literallayout"><p>$ git describe e05db0fd<br>
v1.5.0-rc0-260-ge05db0f</p></div><p>but that may sometimes help you guess which tags might come after the
given commit.</p><p>If you just want to verify whether a given tagged version contains a
given commit, you could use <a class="ulink" href="git-merge-base.html" target="_top">git-merge-base(1)</a>:</p><div class="literallayout"><p>$ git merge-base e05db0fd v1.5.0-rc1<br>
e05db0fd4f31dde7005f075a84f96b360d05984b</p></div><p>The merge-base command finds a common ancestor of the given commits,
and always returns one or the other in the case where one is a
descendant of the other; so the above output shows that e05db0fd
actually is an ancestor of v1.5.0-rc1.</p><p>Alternatively, note that</p><div class="literallayout"><p>$ git log v1.5.0-rc1..e05db0fd</p></div><p>will produce empty output if and only if v1.5.0-rc1 includes e05db0fd,
because it outputs only commits that are not reachable from v1.5.0-rc1.</p><p>As yet another alternative, the <a class="ulink" href="git-show-branch.html" target="_top">git-show-branch(1)</a> command lists
the commits reachable from its arguments with a display on the left-hand
side that indicates which arguments that commit is reachable from. So,
you can run something like</p><div class="literallayout"><p>$ git show-branch e05db0fd v1.5.0-rc0 v1.5.0-rc1 v1.5.0-rc2<br>
! [e05db0fd] Fix warnings in sha1_file.c - use C99 printf format if<br>
available<br>
! [v1.5.0-rc0] GIT v1.5.0 preview<br>
! [v1.5.0-rc1] GIT v1.5.0-rc1<br>
! [v1.5.0-rc2] GIT v1.5.0-rc2<br>
...</p></div><p>then search for a line that looks like</p><div class="literallayout"><p>+ ++ [e05db0fd] Fix warnings in sha1_file.c - use C99 printf format if<br>
available</p></div><p>Which shows that e05db0fd is reachable from itself, from v1.5.0-rc1, and
from v1.5.0-rc2, but not from v1.5.0-rc0.</p></div><div class="section" title="Showing commits unique to a given branch"><div class="titlepage"><div><div><h3 class="title"><a name="showing-commits-unique-to-a-branch"></a>Showing commits unique to a given branch</h3></div></div></div><p>Suppose you would like to see all the commits reachable from the branch
head named "master" but not from any other head in your repository.</p><p>We can list all the heads in this repository with
<a class="ulink" href="git-show-ref.html" target="_top">git-show-ref(1)</a>:</p><div class="literallayout"><p>$ git show-ref --heads<br>
bf62196b5e363d73353a9dcf094c59595f3153b7 refs/heads/core-tutorial<br>
db768d5504c1bb46f63ee9d6e1772bd047e05bf9 refs/heads/maint<br>
a07157ac624b2524a059a3414e99f6f44bebc1e7 refs/heads/master<br>
24dbc180ea14dc1aebe09f14c8ecf32010690627 refs/heads/tutorial-2<br>
1e87486ae06626c2f31eaa63d26fc0fd646c8af2 refs/heads/tutorial-fixes</p></div><p>We can get just the branch-head names, and remove "master", with
the help of the standard utilities cut and grep:</p><div class="literallayout"><p>$ git show-ref --heads | cut -d' ' -f2 | grep -v '^refs/heads/master'<br>
refs/heads/core-tutorial<br>
refs/heads/maint<br>
refs/heads/tutorial-2<br>
refs/heads/tutorial-fixes</p></div><p>And then we can ask to see all the commits reachable from master
but not from these other heads:</p><div class="literallayout"><p>$ gitk master --not $( git show-ref --heads | cut -d' ' -f2 |<br>
grep -v '^refs/heads/master' )</p></div><p>Obviously, endless variations are possible; for example, to see all
commits reachable from some head but not from any tag in the repository:</p><div class="literallayout"><p>$ gitk $( git show-ref --heads ) --not $( git show-ref --tags )</p></div><p>(See <a class="ulink" href="gitrevisions.html" target="_top">gitrevisions(7)</a> for explanations of commit-selecting
syntax such as <code class="literal">--not</code>.)</p></div><div class="section" title="Creating a changelog and tarball for a software release"><div class="titlepage"><div><div><h3 class="title"><a name="making-a-release"></a>Creating a changelog and tarball for a software release</h3></div></div></div><p>The <a class="ulink" href="git-archive.html" target="_top">git-archive(1)</a> command can create a tar or zip archive from
any version of a project; for example:</p><div class="literallayout"><p>$ git archive --format=tar --prefix=project/ HEAD | gzip >latest.tar.gz</p></div><p>will use HEAD to produce a tar archive in which each filename is
preceded by "project/".</p><p>If you’re releasing a new version of a software project, you may want
to simultaneously make a changelog to include in the release
announcement.</p><p>Linus Torvalds, for example, makes new kernel releases by tagging them,
then running:</p><div class="literallayout"><p>$ release-script 2.6.12 2.6.13-rc6 2.6.13-rc7</p></div><p>where release-script is a shell script that looks like:</p><div class="literallayout"><p>#!/bin/sh<br>
stable="$1"<br>
last="$2"<br>
new="$3"<br>
echo "# git tag v$new"<br>
echo "git archive --prefix=linux-$new/ v$new | gzip -9 > ../linux-$new.tar.gz"<br>
echo "git diff v$stable v$new | gzip -9 > ../patch-$new.gz"<br>
echo "git log --no-merges v$new ^v$last > ../ChangeLog-$new"<br>
echo "git shortlog --no-merges v$new ^v$last > ../ShortLog"<br>
echo "git diff --stat --summary -M v$last v$new > ../diffstat-$new"</p></div><p>and then he just cut-and-pastes the output commands after verifying that
they look OK.</p></div><div class="section" title="Finding commits referencing a file with given content"><div class="titlepage"><div><div><h3 class="title"><a name="Finding-commits-With-given-Content"></a>Finding commits referencing a file with given content</h3></div></div></div><p>Somebody hands you a copy of a file, and asks which commits modified a
file such that it contained the given content either before or after the
commit. You can find out with this:</p><div class="literallayout"><p>$ git log --raw --abbrev=40 --pretty=oneline |<br>
grep -B 1 `git hash-object filename`</p></div><p>Figuring out why this works is left as an exercise to the (advanced)
student. The <a class="ulink" href="git-log.html" target="_top">git-log(1)</a>, <a class="ulink" href="git-diff-tree.html" target="_top">git-diff-tree(1)</a>, and
<a class="ulink" href="git-hash-object.html" target="_top">git-hash-object(1)</a> man pages may prove helpful.</p></div></div></div><div class="chapter" title="Chapter 3. Developing with git"><div class="titlepage"><div><div><h2 class="title"><a name="Developing-With-git"></a>Chapter 3. Developing with git</h2></div></div></div><div class="toc"><p><b>Table of Contents</b></p><dl><dt><span class="section"><a href="#telling-git-your-name">Telling git your name</a></span></dt><dt><span class="section"><a href="#creating-a-new-repository">Creating a new repository</a></span></dt><dt><span class="section"><a href="#how-to-make-a-commit">How to make a commit</a></span></dt><dt><span class="section"><a href="#creating-good-commit-messages">Creating good commit messages</a></span></dt><dt><span class="section"><a href="#ignoring-files">Ignoring files</a></span></dt><dt><span class="section"><a href="#how-to-merge">How to merge</a></span></dt><dt><span class="section"><a href="#resolving-a-merge">Resolving a merge</a></span></dt><dd><dl><dt><span class="section"><a href="#conflict-resolution">Getting conflict-resolution help during a merge</a></span></dt></dl></dd><dt><span class="section"><a href="#undoing-a-merge">Undoing a merge</a></span></dt><dt><span class="section"><a href="#fast-forwards">Fast-forward merges</a></span></dt><dt><span class="section"><a href="#fixing-mistakes">Fixing mistakes</a></span></dt><dd><dl><dt><span class="section"><a href="#reverting-a-commit">Fixing a mistake with a new commit</a></span></dt><dt><span class="section"><a href="#fixing-a-mistake-by-rewriting-history">Fixing a mistake by rewriting history</a></span></dt><dt><span class="section"><a href="#checkout-of-path">Checking out an old version of a file</a></span></dt><dt><span class="section"><a href="#interrupted-work">Temporarily setting aside work in progress</a></span></dt></dl></dd><dt><span class="section"><a href="#ensuring-good-performance">Ensuring good performance</a></span></dt><dt><span class="section"><a href="#ensuring-reliability">Ensuring reliability</a></span></dt><dd><dl><dt><span class="section"><a href="#checking-for-corruption">Checking the repository for corruption</a></span></dt><dt><span class="section"><a href="#recovering-lost-changes">Recovering lost changes</a></span></dt></dl></dd></dl></div><div class="section" title="Telling git your name"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="telling-git-your-name"></a>Telling git your name</h2></div></div></div><p>Before creating any commits, you should introduce yourself to git. The
easiest way to do so is to make sure the following lines appear in a
file named .gitconfig in your home directory:</p><div class="literallayout"><p>[user]<br>
name = Your Name Comes Here<br>
email = you@yourdomain.example.com</p></div><p>(See the "CONFIGURATION FILE" section of <a class="ulink" href="git-config.html" target="_top">git-config(1)</a> for
details on the configuration file.)</p></div><div class="section" title="Creating a new repository"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="creating-a-new-repository"></a>Creating a new repository</h2></div></div></div><p>Creating a new repository from scratch is very easy:</p><div class="literallayout"><p>$ mkdir project<br>
$ cd project<br>
$ git init</p></div><p>If you have some initial content (say, a tarball):</p><div class="literallayout"><p>$ tar xzvf project.tar.gz<br>
$ cd project<br>
$ git init<br>
$ git add . # include everything below ./ in the first commit:<br>
$ git commit</p></div></div><div class="section" title="How to make a commit"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="how-to-make-a-commit"></a>How to make a commit</h2></div></div></div><p>Creating a new commit takes three steps:</p><div class="orderedlist"><ol class="orderedlist" type="1"><li class="listitem">
Making some changes to the working directory using your
favorite editor.
</li><li class="listitem">
Telling git about your changes.
</li><li class="listitem">
Creating the commit using the content you told git about
in step 2.
</li></ol></div><p>In practice, you can interleave and repeat steps 1 and 2 as many
times as you want: in order to keep track of what you want committed
at step 3, git maintains a snapshot of the tree’s contents in a
special staging area called "the index."</p><p>At the beginning, the content of the index will be identical to
that of the HEAD. The command "git diff --cached", which shows
the difference between the HEAD and the index, should therefore
produce no output at that point.</p><p>Modifying the index is easy:</p><p>To update the index with the new contents of a modified file, use</p><div class="literallayout"><p>$ git add path/to/file</p></div><p>To add the contents of a new file to the index, use</p><div class="literallayout"><p>$ git add path/to/file</p></div><p>To remove a file from the index and from the working tree,</p><div class="literallayout"><p>$ git rm path/to/file</p></div><p>After each step you can verify that</p><div class="literallayout"><p>$ git diff --cached</p></div><p>always shows the difference between the HEAD and the index file—this
is what you’d commit if you created the commit now—and that</p><div class="literallayout"><p>$ git diff</p></div><p>shows the difference between the working tree and the index file.</p><p>Note that "git add" always adds just the current contents of a file
to the index; further changes to the same file will be ignored unless
you run <code class="literal">git add</code> on the file again.</p><p>When you’re ready, just run</p><div class="literallayout"><p>$ git commit</p></div><p>and git will prompt you for a commit message and then create the new
commit. Check to make sure it looks like what you expected with</p><div class="literallayout"><p>$ git show</p></div><p>As a special shortcut,</p><div class="literallayout"><p>$ git commit -a</p></div><p>will update the index with any files that you’ve modified or removed
and create a commit, all in one step.</p><p>A number of commands are useful for keeping track of what you’re
about to commit:</p><div class="literallayout"><p>$ git diff --cached # difference between HEAD and the index; what<br>
# would be committed if you ran "commit" now.<br>
$ git diff # difference between the index file and your<br>
# working directory; changes that would not<br>
# be included if you ran "commit" now.<br>
$ git diff HEAD # difference between HEAD and working tree; what<br>
# would be committed if you ran "commit -a" now.<br>
$ git status # a brief per-file summary of the above.</p></div><p>You can also use <a class="ulink" href="git-gui.html" target="_top">git-gui(1)</a> to create commits, view changes in
the index and the working tree files, and individually select diff hunks
for inclusion in the index (by right-clicking on the diff hunk and
choosing "Stage Hunk For Commit").</p></div><div class="section" title="Creating good commit messages"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="creating-good-commit-messages"></a>Creating good commit messages</h2></div></div></div><p>Though not required, it’s a good idea to begin the commit message
with a single short (less than 50 character) line summarizing the
change, followed by a blank line and then a more thorough
description. Tools that turn commits into email, for example, use
the first line on the Subject line and the rest of the commit in the
body.</p></div><div class="section" title="Ignoring files"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="ignoring-files"></a>Ignoring files</h2></div></div></div><p>A project will often generate files that you do <span class="emphasis"><em>not</em></span> want to track with git.
This typically includes files generated by a build process or temporary
backup files made by your editor. Of course, <span class="emphasis"><em>not</em></span> tracking files with git
is just a matter of <span class="emphasis"><em>not</em></span> calling <code class="literal">git add</code> on them. But it quickly becomes
annoying to have these untracked files lying around; e.g. they make
<code class="literal">git add .</code> practically useless, and they keep showing up in the output of
<code class="literal">git status</code>.</p><p>You can tell git to ignore certain files by creating a file called .gitignore
in the top level of your working directory, with contents such as:</p><div class="literallayout"><p># Lines starting with '#' are considered comments.<br>
# Ignore any file named foo.txt.<br>
foo.txt<br>
# Ignore (generated) html files,<br>
*.html<br>
# except foo.html which is maintained by hand.<br>
!foo.html<br>
# Ignore objects and archives.<br>
*.[oa]</p></div><p>See <a class="ulink" href="gitignore.html" target="_top">gitignore(5)</a> for a detailed explanation of the syntax. You can
also place .gitignore files in other directories in your working tree, and they
will apply to those directories and their subdirectories. The <code class="literal">.gitignore</code>
files can be added to your repository like any other files (just run <code class="literal">git add
.gitignore</code> and <code class="literal">git commit</code>, as usual), which is convenient when the exclude
patterns (such as patterns matching build output files) would also make sense
for other users who clone your repository.</p><p>If you wish the exclude patterns to affect only certain repositories
(instead of every repository for a given project), you may instead put
them in a file in your repository named .git/info/exclude, or in any file
specified by the <code class="literal">core.excludesfile</code> configuration variable. Some git
commands can also take exclude patterns directly on the command line.
See <a class="ulink" href="gitignore.html" target="_top">gitignore(5)</a> for the details.</p></div><div class="section" title="How to merge"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="how-to-merge"></a>How to merge</h2></div></div></div><p>You can rejoin two diverging branches of development using
<a class="ulink" href="git-merge.html" target="_top">git-merge(1)</a>:</p><div class="literallayout"><p>$ git merge branchname</p></div><p>merges the development in the branch "branchname" into the current
branch.</p><p>A merge is made by combining the changes made in "branchname" and the
changes made up to the latest commit in your current branch since
their histories forked. The work tree is overwritten by the result of
the merge when this combining is done cleanly, or overwritten by a
half-merged results when this combining results in conflicts.
Therefore, if you have uncommitted changes touching the same files as
the ones impacted by the merge, Git will refuse to proceed. Most of
the time, you will want to commit your changes before you can merge,
and if you don’t, then <a class="ulink" href="git-stash.html" target="_top">git-stash(1)</a> can take these changes
away while you’re doing the merge, and reapply them afterwards.</p><p>If the changes are independent enough, Git will automatically complete
the merge and commit the result (or reuse an existing commit in case
of <a class="link" href="#fast-forwards" title="Fast-forward merges">fast-forward</a>, see below). On the other hand,
if there are conflicts—for example, if the same file is
modified in two different ways in the remote branch and the local
branch—then you are warned; the output may look something like this:</p><div class="literallayout"><p>$ git merge next<br>
100% (4/4) done<br>
Auto-merged file.txt<br>
CONFLICT (content): Merge conflict in file.txt<br>
Automatic merge failed; fix conflicts and then commit the result.</p></div><p>Conflict markers are left in the problematic files, and after
you resolve the conflicts manually, you can update the index
with the contents and run git commit, as you normally would when
creating a new file.</p><p>If you examine the resulting commit using gitk, you will see that it
has two parents, one pointing to the top of the current branch, and
one to the top of the other branch.</p></div><div class="section" title="Resolving a merge"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="resolving-a-merge"></a>Resolving a merge</h2></div></div></div><p>When a merge isn’t resolved automatically, git leaves the index and
the working tree in a special state that gives you all the
information you need to help resolve the merge.</p><p>Files with conflicts are marked specially in the index, so until you
resolve the problem and update the index, <a class="ulink" href="git-commit.html" target="_top">git-commit(1)</a> will
fail:</p><div class="literallayout"><p>$ git commit<br>
file.txt: needs merge</p></div><p>Also, <a class="ulink" href="git-status.html" target="_top">git-status(1)</a> will list those files as "unmerged", and the
files with conflicts will have conflict markers added, like this:</p><div class="literallayout"><p><<<<<<< HEAD:file.txt<br>
Hello world<br>
=======<br>
Goodbye<br>
>>>>>>> 77976da35a11db4580b80ae27e8d65caf5208086:file.txt</p></div><p>All you need to do is edit the files to resolve the conflicts, and then</p><div class="literallayout"><p>$ git add file.txt<br>
$ git commit</p></div><p>Note that the commit message will already be filled in for you with
some information about the merge. Normally you can just use this
default message unchanged, but you may add additional commentary of
your own if desired.</p><p>The above is all you need to know to resolve a simple merge. But git
also provides more information to help resolve conflicts:</p><div class="section" title="Getting conflict-resolution help during a merge"><div class="titlepage"><div><div><h3 class="title"><a name="conflict-resolution"></a>Getting conflict-resolution help during a merge</h3></div></div></div><p>All of the changes that git was able to merge automatically are
already added to the index file, so <a class="ulink" href="git-diff.html" target="_top">git-diff(1)</a> shows only
the conflicts. It uses an unusual syntax:</p><div class="literallayout"><p>$ git diff<br>
diff --cc file.txt<br>
index 802992c,2b60207..0000000<br>
--- a/file.txt<br>
+++ b/file.txt<br>
@@@ -1,1 -1,1 +1,5 @@@<br>
++<<<<<<< HEAD:file.txt<br>
+Hello world<br>
++=======<br>
+ Goodbye<br>
++>>>>>>> 77976da35a11db4580b80ae27e8d65caf5208086:file.txt</p></div><p>Recall that the commit which will be committed after we resolve this
conflict will have two parents instead of the usual one: one parent
will be HEAD, the tip of the current branch; the other will be the
tip of the other branch, which is stored temporarily in MERGE_HEAD.</p><p>During the merge, the index holds three versions of each file. Each of
these three "file stages" represents a different version of the file:</p><div class="literallayout"><p>$ git show :1:file.txt # the file in a common ancestor of both branches<br>
$ git show :2:file.txt # the version from HEAD.<br>
$ git show :3:file.txt # the version from MERGE_HEAD.</p></div><p>When you ask <a class="ulink" href="git-diff.html" target="_top">git-diff(1)</a> to show the conflicts, it runs a
three-way diff between the conflicted merge results in the work tree with
stages 2 and 3 to show only hunks whose contents come from both sides,
mixed (in other words, when a hunk’s merge results come only from stage 2,
that part is not conflicting and is not shown. Same for stage 3).</p><p>The diff above shows the differences between the working-tree version of
file.txt and the stage 2 and stage 3 versions. So instead of preceding
each line by a single "+" or "-", it now uses two columns: the first
column is used for differences between the first parent and the working
directory copy, and the second for differences between the second parent
and the working directory copy. (See the "COMBINED DIFF FORMAT" section
of <a class="ulink" href="git-diff-files.html" target="_top">git-diff-files(1)</a> for a details of the format.)</p><p>After resolving the conflict in the obvious way (but before updating the
index), the diff will look like:</p><div class="literallayout"><p>$ git diff<br>
diff --cc file.txt<br>
index 802992c,2b60207..0000000<br>
--- a/file.txt<br>
+++ b/file.txt<br>
@@@ -1,1 -1,1 +1,1 @@@<br>
- Hello world<br>
-Goodbye<br>
++Goodbye world</p></div><p>This shows that our resolved version deleted "Hello world" from the
first parent, deleted "Goodbye" from the second parent, and added
"Goodbye world", which was previously absent from both.</p><p>Some special diff options allow diffing the working directory against
any of these stages:</p><div class="literallayout"><p>$ git diff -1 file.txt # diff against stage 1<br>
$ git diff --base file.txt # same as the above<br>
$ git diff -2 file.txt # diff against stage 2<br>
$ git diff --ours file.txt # same as the above<br>
$ git diff -3 file.txt # diff against stage 3<br>
$ git diff --theirs file.txt # same as the above.</p></div><p>The <a class="ulink" href="git-log.html" target="_top">git-log(1)</a> and <a class="ulink" href="gitk.html" target="_top">gitk(1)</a> commands also provide special help
for merges:</p><div class="literallayout"><p>$ git log --merge<br>
$ gitk --merge</p></div><p>These will display all commits which exist only on HEAD or on
MERGE_HEAD, and which touch an unmerged file.</p><p>You may also use <a class="ulink" href="git-mergetool.html" target="_top">git-mergetool(1)</a>, which lets you merge the
unmerged files using external tools such as Emacs or kdiff3.</p><p>Each time you resolve the conflicts in a file and update the index:</p><div class="literallayout"><p>$ git add file.txt</p></div><p>the different stages of that file will be "collapsed", after which
<code class="literal">git diff</code> will (by default) no longer show diffs for that file.</p></div></div><div class="section" title="Undoing a merge"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="undoing-a-merge"></a>Undoing a merge</h2></div></div></div><p>If you get stuck and decide to just give up and throw the whole mess
away, you can always return to the pre-merge state with</p><div class="literallayout"><p>$ git reset --hard HEAD</p></div><p>Or, if you’ve already committed the merge that you want to throw away,</p><div class="literallayout"><p>$ git reset --hard ORIG_HEAD</p></div><p>However, this last command can be dangerous in some cases—never
throw away a commit you have already committed if that commit may
itself have been merged into another branch, as doing so may confuse
further merges.</p></div><div class="section" title="Fast-forward merges"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="fast-forwards"></a>Fast-forward merges</h2></div></div></div><p>There is one special case not mentioned above, which is treated
differently. Normally, a merge results in a merge commit, with two
parents, one pointing at each of the two lines of development that
were merged.</p><p>However, if the current branch is a descendant of the other—so every
commit present in the one is already contained in the other—then git
just performs a "fast-forward"; the head of the current branch is moved
forward to point at the head of the merged-in branch, without any new
commits being created.</p></div><div class="section" title="Fixing mistakes"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="fixing-mistakes"></a>Fixing mistakes</h2></div></div></div><p>If you’ve messed up the working tree, but haven’t yet committed your
mistake, you can return the entire working tree to the last committed
state with</p><div class="literallayout"><p>$ git reset --hard HEAD</p></div><p>If you make a commit that you later wish you hadn’t, there are two
fundamentally different ways to fix the problem:</p><div class="orderedlist"><ol class="orderedlist" type="1"><li class="listitem">
You can create a new commit that undoes whatever was done
by the old commit. This is the correct thing if your
mistake has already been made public.
</li><li class="listitem">
You can go back and modify the old commit. You should
never do this if you have already made the history public;
git does not normally expect the "history" of a project to
change, and cannot correctly perform repeated merges from
a branch that has had its history changed.
</li></ol></div><div class="section" title="Fixing a mistake with a new commit"><div class="titlepage"><div><div><h3 class="title"><a name="reverting-a-commit"></a>Fixing a mistake with a new commit</h3></div></div></div><p>Creating a new commit that reverts an earlier change is very easy;
just pass the <a class="ulink" href="git-revert.html" target="_top">git-revert(1)</a> command a reference to the bad
commit; for example, to revert the most recent commit:</p><div class="literallayout"><p>$ git revert HEAD</p></div><p>This will create a new commit which undoes the change in HEAD. You
will be given a chance to edit the commit message for the new commit.</p><p>You can also revert an earlier change, for example, the next-to-last:</p><div class="literallayout"><p>$ git revert HEAD^</p></div><p>In this case git will attempt to undo the old change while leaving
intact any changes made since then. If more recent changes overlap
with the changes to be reverted, then you will be asked to fix
conflicts manually, just as in the case of <a class="link" href="#resolving-a-merge" title="Resolving a merge">resolving a merge</a>.</p></div><div class="section" title="Fixing a mistake by rewriting history"><div class="titlepage"><div><div><h3 class="title"><a name="fixing-a-mistake-by-rewriting-history"></a>Fixing a mistake by rewriting history</h3></div></div></div><p>If the problematic commit is the most recent commit, and you have not
yet made that commit public, then you may just
<a class="link" href="#undoing-a-merge" title="Undoing a merge">destroy it using <code class="literal">git reset</code></a>.</p><p>Alternatively, you
can edit the working directory and update the index to fix your
mistake, just as if you were going to <a class="link" href="#how-to-make-a-commit" title="How to make a commit">create a new commit</a>, then run</p><div class="literallayout"><p>$ git commit --amend</p></div><p>which will replace the old commit by a new commit incorporating your
changes, giving you a chance to edit the old commit message first.</p><p>Again, you should never do this to a commit that may already have
been merged into another branch; use <a class="ulink" href="git-revert.html" target="_top">git-revert(1)</a> instead in
that case.</p><p>It is also possible to replace commits further back in the history, but
this is an advanced topic to be left for
<a class="link" href="#cleaning-up-history" title="Chapter 5. Rewriting history and maintaining patch series">another chapter</a>.</p></div><div class="section" title="Checking out an old version of a file"><div class="titlepage"><div><div><h3 class="title"><a name="checkout-of-path"></a>Checking out an old version of a file</h3></div></div></div><p>In the process of undoing a previous bad change, you may find it
useful to check out an older version of a particular file using
<a class="ulink" href="git-checkout.html" target="_top">git-checkout(1)</a>. We’ve used <code class="literal">git checkout</code> before to switch
branches, but it has quite different behavior if it is given a path
name: the command</p><div class="literallayout"><p>$ git checkout HEAD^ path/to/file</p></div><p>replaces path/to/file by the contents it had in the commit HEAD^, and
also updates the index to match. It does not change branches.</p><p>If you just want to look at an old version of the file, without
modifying the working directory, you can do that with
<a class="ulink" href="git-show.html" target="_top">git-show(1)</a>:</p><div class="literallayout"><p>$ git show HEAD^:path/to/file</p></div><p>which will display the given version of the file.</p></div><div class="section" title="Temporarily setting aside work in progress"><div class="titlepage"><div><div><h3 class="title"><a name="interrupted-work"></a>Temporarily setting aside work in progress</h3></div></div></div><p>While you are in the middle of working on something complicated, you
find an unrelated but obvious and trivial bug. You would like to fix it
before continuing. You can use <a class="ulink" href="git-stash.html" target="_top">git-stash(1)</a> to save the current
state of your work, and after fixing the bug (or, optionally after doing
so on a different branch and then coming back), unstash the
work-in-progress changes.</p><div class="literallayout"><p>$ git stash save "work in progress for foo feature"</p></div><p>This command will save your changes away to the <code class="literal">stash</code>, and
reset your working tree and the index to match the tip of your
current branch. Then you can make your fix as usual.</p><div class="literallayout"><p>... edit and test ...<br>
$ git commit -a -m "blorpl: typofix"</p></div><p>After that, you can go back to what you were working on with
<code class="literal">git stash pop</code>:</p><div class="literallayout"><p>$ git stash pop</p></div></div></div><div class="section" title="Ensuring good performance"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="ensuring-good-performance"></a>Ensuring good performance</h2></div></div></div><p>On large repositories, git depends on compression to keep the history
information from taking up too much space on disk or in memory.</p><p>This compression is not performed automatically. Therefore you
should occasionally run <a class="ulink" href="git-gc.html" target="_top">git-gc(1)</a>:</p><div class="literallayout"><p>$ git gc</p></div><p>to recompress the archive. This can be very time-consuming, so
you may prefer to run <code class="literal">git gc</code> when you are not doing other work.</p></div><div class="section" title="Ensuring reliability"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="ensuring-reliability"></a>Ensuring reliability</h2></div></div></div><div class="section" title="Checking the repository for corruption"><div class="titlepage"><div><div><h3 class="title"><a name="checking-for-corruption"></a>Checking the repository for corruption</h3></div></div></div><p>The <a class="ulink" href="git-fsck.html" target="_top">git-fsck(1)</a> command runs a number of self-consistency checks
on the repository, and reports on any problems. This may take some
time.</p><div class="literallayout"><p>$ git fsck<br>
dangling commit 7281251ddd2a61e38657c827739c57015671a6b3<br>
dangling commit 2706a059f258c6b245f298dc4ff2ccd30ec21a63<br>
dangling commit 13472b7c4b80851a1bc551779171dcb03655e9b5<br>
dangling blob 218761f9d90712d37a9c5e36f406f92202db07eb<br>
dangling commit bf093535a34a4d35731aa2bd90fe6b176302f14f<br>
dangling commit 8e4bec7f2ddaa268bef999853c25755452100f8e<br>
dangling tree d50bb86186bf27b681d25af89d3b5b68382e4085<br>
dangling tree b24c2473f1fd3d91352a624795be026d64c8841f<br>
...</p></div><p>You will see informational messages on dangling objects. They are objects
that still exist in the repository but are no longer referenced by any of
your branches, and can (and will) be removed after a while with "gc".
You can run <code class="literal">git fsck --no-dangling</code> to suppress these messages, and still
view real errors.</p></div><div class="section" title="Recovering lost changes"><div class="titlepage"><div><div><h3 class="title"><a name="recovering-lost-changes"></a>Recovering lost changes</h3></div></div></div><div class="section" title="Reflogs"><div class="titlepage"><div><div><h4 class="title"><a name="reflogs"></a>Reflogs</h4></div></div></div><p>Say you modify a branch with <code class="literal"><a class="ulink" href="git-reset.html" target="_top">git-reset(1)</a> --hard</code>, and then
realize that the branch was the only reference you had to that point in
history.</p><p>Fortunately, git also keeps a log, called a "reflog", of all the
previous values of each branch. So in this case you can still find the
old history using, for example,</p><div class="literallayout"><p>$ git log master@{1}</p></div><p>This lists the commits reachable from the previous version of the
"master" branch head. This syntax can be used with any git command
that accepts a commit, not just with git log. Some other examples:</p><div class="literallayout"><p>$ git show master@{2} # See where the branch pointed 2,<br>
$ git show master@{3} # 3, ... changes ago.<br>
$ gitk master@{yesterday} # See where it pointed yesterday,<br>
$ gitk master@{"1 week ago"} # ... or last week<br>
$ git log --walk-reflogs master # show reflog entries for master</p></div><p>A separate reflog is kept for the HEAD, so</p><div class="literallayout"><p>$ git show HEAD@{"1 week ago"}</p></div><p>will show what HEAD pointed to one week ago, not what the current branch
pointed to one week ago. This allows you to see the history of what
you’ve checked out.</p><p>The reflogs are kept by default for 30 days, after which they may be
pruned. See <a class="ulink" href="git-reflog.html" target="_top">git-reflog(1)</a> and <a class="ulink" href="git-gc.html" target="_top">git-gc(1)</a> to learn
how to control this pruning, and see the "SPECIFYING REVISIONS"
section of <a class="ulink" href="gitrevisions.html" target="_top">gitrevisions(7)</a> for details.</p><p>Note that the reflog history is very different from normal git history.
While normal history is shared by every repository that works on the
same project, the reflog history is not shared: it tells you only about
how the branches in your local repository have changed over time.</p></div><div class="section" title="Examining dangling objects"><div class="titlepage"><div><div><h4 class="title"><a name="dangling-object-recovery"></a>Examining dangling objects</h4></div></div></div><p>In some situations the reflog may not be able to save you. For example,
suppose you delete a branch, then realize you need the history it
contained. The reflog is also deleted; however, if you have not yet
pruned the repository, then you may still be able to find the lost
commits in the dangling objects that <code class="literal">git fsck</code> reports. See
<a class="xref" href="#dangling-objects" title="Dangling objects">the section called “Dangling objects”</a> for the details.</p><div class="literallayout"><p>$ git fsck<br>
dangling commit 7281251ddd2a61e38657c827739c57015671a6b3<br>
dangling commit 2706a059f258c6b245f298dc4ff2ccd30ec21a63<br>
dangling commit 13472b7c4b80851a1bc551779171dcb03655e9b5<br>
...</p></div><p>You can examine
one of those dangling commits with, for example,</p><div class="literallayout"><p>$ gitk 7281251ddd --not --all</p></div><p>which does what it sounds like: it says that you want to see the commit
history that is described by the dangling commit(s), but not the
history that is described by all your existing branches and tags. Thus
you get exactly the history reachable from that commit that is lost.
(And notice that it might not be just one commit: we only report the
"tip of the line" as being dangling, but there might be a whole deep
and complex commit history that was dropped.)</p><p>If you decide you want the history back, you can always create a new
reference pointing to it, for example, a new branch:</p><div class="literallayout"><p>$ git branch recovered-branch 7281251ddd</p></div><p>Other types of dangling objects (blobs and trees) are also possible, and
dangling objects can arise in other situations.</p></div></div></div></div><div class="chapter" title="Chapter 4. Sharing development with others"><div class="titlepage"><div><div><h2 class="title"><a name="sharing-development"></a>Chapter 4. Sharing development with others</h2></div></div></div><div class="toc"><p><b>Table of Contents</b></p><dl><dt><span class="section"><a href="#getting-updates-With-git-pull">Getting updates with git pull</a></span></dt><dt><span class="section"><a href="#submitting-patches">Submitting patches to a project</a></span></dt><dt><span class="section"><a href="#importing-patches">Importing patches to a project</a></span></dt><dt><span class="section"><a href="#public-repositories">Public git repositories</a></span></dt><dd><dl><dt><span class="section"><a href="#setting-up-a-public-repository">Setting up a public repository</a></span></dt><dt><span class="section"><a href="#exporting-via-git">Exporting a git repository via the git protocol</a></span></dt><dt><span class="section"><a href="#exporting-via-http">Exporting a git repository via http</a></span></dt><dt><span class="section"><a href="#pushing-changes-to-a-public-repository">Pushing changes to a public repository</a></span></dt><dt><span class="section"><a href="#forcing-push">What to do when a push fails</a></span></dt><dt><span class="section"><a href="#setting-up-a-shared-repository">Setting up a shared repository</a></span></dt><dt><span class="section"><a href="#setting-up-gitweb">Allowing web browsing of a repository</a></span></dt></dl></dd><dt><span class="section"><a href="#sharing-development-examples">Examples</a></span></dt><dd><dl><dt><span class="section"><a href="#maintaining-topic-branches">Maintaining topic branches for a Linux subsystem maintainer</a></span></dt></dl></dd></dl></div><div class="section" title="Getting updates with git pull"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="getting-updates-With-git-pull"></a>Getting updates with git pull</h2></div></div></div><p>After you clone a repository and commit a few changes of your own, you
may wish to check the original repository for updates and merge them
into your own work.</p><p>We have already seen <a class="link" href="#Updating-a-repository-With-git-fetch" title="Updating a repository with git fetch">how to keep remote-tracking branches up to date</a> with <a class="ulink" href="git-fetch.html" target="_top">git-fetch(1)</a>,
and how to merge two branches. So you can merge in changes from the
original repository’s master branch with:</p><div class="literallayout"><p>$ git fetch<br>
$ git merge origin/master</p></div><p>However, the <a class="ulink" href="git-pull.html" target="_top">git-pull(1)</a> command provides a way to do this in
one step:</p><div class="literallayout"><p>$ git pull origin master</p></div><p>In fact, if you have "master" checked out, then this branch has been
configured by "git clone" to get changes from the HEAD branch of the
origin repository. So often you can
accomplish the above with just a simple</p><div class="literallayout"><p>$ git pull</p></div><p>This command will fetch changes from the remote branches to your
remote-tracking branches <code class="literal">origin/*</code>, and merge the default branch into
the current branch.</p><p>More generally, a branch that is created from a remote-tracking branch
will pull
by default from that branch. See the descriptions of the
branch.<name>.remote and branch.<name>.merge options in
<a class="ulink" href="git-config.html" target="_top">git-config(1)</a>, and the discussion of the <code class="literal">--track</code> option in
<a class="ulink" href="git-checkout.html" target="_top">git-checkout(1)</a>, to learn how to control these defaults.</p><p>In addition to saving you keystrokes, "git pull" also helps you by
producing a default commit message documenting the branch and
repository that you pulled from.</p><p>(But note that no such commit will be created in the case of a
<a class="link" href="#fast-forwards" title="Fast-forward merges">fast-forward</a>; instead, your branch will just be
updated to point to the latest commit from the upstream branch.)</p><p>The <code class="literal">git pull</code> command can also be given "." as the "remote" repository,
in which case it just merges in a branch from the current repository; so
the commands</p><div class="literallayout"><p>$ git pull . branch<br>
$ git merge branch</p></div><p>are roughly equivalent. The former is actually very commonly used.</p></div><div class="section" title="Submitting patches to a project"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="submitting-patches"></a>Submitting patches to a project</h2></div></div></div><p>If you just have a few changes, the simplest way to submit them may
just be to send them as patches in email:</p><p>First, use <a class="ulink" href="git-format-patch.html" target="_top">git-format-patch(1)</a>; for example:</p><div class="literallayout"><p>$ git format-patch origin</p></div><p>will produce a numbered series of files in the current directory, one
for each patch in the current branch but not in origin/HEAD.</p><p>You can then import these into your mail client and send them by
hand. However, if you have a lot to send at once, you may prefer to
use the <a class="ulink" href="git-send-email.html" target="_top">git-send-email(1)</a> script to automate the process.
Consult the mailing list for your project first to determine how they
prefer such patches be handled.</p></div><div class="section" title="Importing patches to a project"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="importing-patches"></a>Importing patches to a project</h2></div></div></div><p>Git also provides a tool called <a class="ulink" href="git-am.html" target="_top">git-am(1)</a> (am stands for
"apply mailbox"), for importing such an emailed series of patches.
Just save all of the patch-containing messages, in order, into a
single mailbox file, say "patches.mbox", then run</p><div class="literallayout"><p>$ git am -3 patches.mbox</p></div><p>Git will apply each patch in order; if any conflicts are found, it
will stop, and you can fix the conflicts as described in
"<a class="link" href="#resolving-a-merge" title="Resolving a merge">Resolving a merge</a>". (The "-3" option tells
git to perform a merge; if you would prefer it just to abort and
leave your tree and index untouched, you may omit that option.)</p><p>Once the index is updated with the results of the conflict
resolution, instead of creating a new commit, just run</p><div class="literallayout"><p>$ git am --resolved</p></div><p>and git will create the commit for you and continue applying the
remaining patches from the mailbox.</p><p>The final result will be a series of commits, one for each patch in
the original mailbox, with authorship and commit log message each
taken from the message containing each patch.</p></div><div class="section" title="Public git repositories"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="public-repositories"></a>Public git repositories</h2></div></div></div><p>Another way to submit changes to a project is to tell the maintainer
of that project to pull the changes from your repository using
<a class="ulink" href="git-pull.html" target="_top">git-pull(1)</a>. In the section "<a class="link" href="#getting-updates-With-git-pull" title="Getting updates with git pull">Getting updates with <code class="literal">git pull</code></a>" we described this as a way to get
updates from the "main" repository, but it works just as well in the
other direction.</p><p>If you and the maintainer both have accounts on the same machine, then
you can just pull changes from each other’s repositories directly;
commands that accept repository URLs as arguments will also accept a
local directory name:</p><div class="literallayout"><p>$ git clone /path/to/repository<br>
$ git pull /path/to/other/repository</p></div><p>or an ssh URL:</p><div class="literallayout"><p>$ git clone ssh://yourhost/~you/repository</p></div><p>For projects with few developers, or for synchronizing a few private
repositories, this may be all you need.</p><p>However, the more common way to do this is to maintain a separate public
repository (usually on a different host) for others to pull changes
from. This is usually more convenient, and allows you to cleanly
separate private work in progress from publicly visible work.</p><p>You will continue to do your day-to-day work in your personal
repository, but periodically "push" changes from your personal
repository into your public repository, allowing other developers to
pull from that repository. So the flow of changes, in a situation
where there is one other developer with a public repository, looks
like this:</p><pre class="literallayout"> you push
your personal repo ------------------> your public repo
^ |
| |
| you pull | they pull
| |
| |
| they push V
their public repo <------------------- their repo</pre><p>We explain how to do this in the following sections.</p><div class="section" title="Setting up a public repository"><div class="titlepage"><div><div><h3 class="title"><a name="setting-up-a-public-repository"></a>Setting up a public repository</h3></div></div></div><p>Assume your personal repository is in the directory ~/proj. We
first create a new clone of the repository and tell <code class="literal">git daemon</code> that it
is meant to be public:</p><div class="literallayout"><p>$ git clone --bare ~/proj proj.git<br>
$ touch proj.git/git-daemon-export-ok</p></div><p>The resulting directory proj.git contains a "bare" git repository—it is
just the contents of the ".git" directory, without any files checked out
around it.</p><p>Next, copy proj.git to the server where you plan to host the
public repository. You can use scp, rsync, or whatever is most
convenient.</p></div><div class="section" title="Exporting a git repository via the git protocol"><div class="titlepage"><div><div><h3 class="title"><a name="exporting-via-git"></a>Exporting a git repository via the git protocol</h3></div></div></div><p>This is the preferred method.</p><p>If someone else administers the server, they should tell you what
directory to put the repository in, and what git:// URL it will appear
at. You can then skip to the section
"<a class="link" href="#pushing-changes-to-a-public-repository" title="Pushing changes to a public repository">Pushing changes to a public repository</a>", below.</p><p>Otherwise, all you need to do is start <a class="ulink" href="git-daemon.html" target="_top">git-daemon(1)</a>; it will
listen on port 9418. By default, it will allow access to any directory
that looks like a git directory and contains the magic file
git-daemon-export-ok. Passing some directory paths as <code class="literal">git daemon</code>
arguments will further restrict the exports to those paths.</p><p>You can also run <code class="literal">git daemon</code> as an inetd service; see the
<a class="ulink" href="git-daemon.html" target="_top">git-daemon(1)</a> man page for details. (See especially the
examples section.)</p></div><div class="section" title="Exporting a git repository via http"><div class="titlepage"><div><div><h3 class="title"><a name="exporting-via-http"></a>Exporting a git repository via http</h3></div></div></div><p>The git protocol gives better performance and reliability, but on a
host with a web server set up, http exports may be simpler to set up.</p><p>All you need to do is place the newly created bare git repository in
a directory that is exported by the web server, and make some
adjustments to give web clients some extra information they need:</p><div class="literallayout"><p>$ mv proj.git /home/you/public_html/proj.git<br>
$ cd proj.git<br>
$ git --bare update-server-info<br>
$ mv hooks/post-update.sample hooks/post-update</p></div><p>(For an explanation of the last two lines, see
<a class="ulink" href="git-update-server-info.html" target="_top">git-update-server-info(1)</a> and <a class="ulink" href="githooks.html" target="_top">githooks(5)</a>.)</p><p>Advertise the URL of proj.git. Anybody else should then be able to
clone or pull from that URL, for example with a command line like:</p><div class="literallayout"><p>$ git clone http://yourserver.com/~you/proj.git</p></div><p>(See also
<a class="ulink" href="howto/setup-git-server-over-http.txt" target="_top">setup-git-server-over-http</a>
for a slightly more sophisticated setup using WebDAV which also
allows pushing over http.)</p></div><div class="section" title="Pushing changes to a public repository"><div class="titlepage"><div><div><h3 class="title"><a name="pushing-changes-to-a-public-repository"></a>Pushing changes to a public repository</h3></div></div></div><p>Note that the two techniques outlined above (exporting via
<a class="link" href="#exporting-via-http" title="Exporting a git repository via http">http</a> or <a class="link" href="#exporting-via-git" title="Exporting a git repository via the git protocol">git</a>) allow other
maintainers to fetch your latest changes, but they do not allow write
access, which you will need to update the public repository with the
latest changes created in your private repository.</p><p>The simplest way to do this is using <a class="ulink" href="git-push.html" target="_top">git-push(1)</a> and ssh; to
update the remote branch named "master" with the latest state of your
branch named "master", run</p><div class="literallayout"><p>$ git push ssh://yourserver.com/~you/proj.git master:master</p></div><p>or just</p><div class="literallayout"><p>$ git push ssh://yourserver.com/~you/proj.git master</p></div><p>As with <code class="literal">git fetch</code>, <code class="literal">git push</code> will complain if this does not result in a
<a class="link" href="#fast-forwards" title="Fast-forward merges">fast-forward</a>; see the following section for details on
handling this case.</p><p>Note that the target of a "push" is normally a
<a class="link" href="#def_bare_repository">bare</a> repository. You can also push to a
repository that has a checked-out working tree, but the working tree
will not be updated by the push. This may lead to unexpected results if
the branch you push to is the currently checked-out branch!</p><p>As with <code class="literal">git fetch</code>, you may also set up configuration options to
save typing; so, for example, after</p><div class="literallayout"><p>$ cat >>.git/config <<EOF<br>
[remote "public-repo"]<br>
url = ssh://yourserver.com/~you/proj.git<br>
EOF</p></div><p>you should be able to perform the above push with just</p><div class="literallayout"><p>$ git push public-repo master</p></div><p>See the explanations of the remote.<name>.url, branch.<name>.remote,
and remote.<name>.push options in <a class="ulink" href="git-config.html" target="_top">git-config(1)</a> for
details.</p></div><div class="section" title="What to do when a push fails"><div class="titlepage"><div><div><h3 class="title"><a name="forcing-push"></a>What to do when a push fails</h3></div></div></div><p>If a push would not result in a <a class="link" href="#fast-forwards" title="Fast-forward merges">fast-forward</a> of the
remote branch, then it will fail with an error like:</p><div class="literallayout"><p>error: remote 'refs/heads/master' is not an ancestor of<br>
local 'refs/heads/master'.<br>
Maybe you are not up-to-date and need to pull first?<br>
error: failed to push to 'ssh://yourserver.com/~you/proj.git'</p></div><p>This can happen, for example, if you:</p><div class="itemizedlist"><ul class="itemizedlist" type="disc"><li class="listitem">
use <code class="literal">git reset --hard</code> to remove already-published commits, or
</li><li class="listitem">
use <code class="literal">git commit --amend</code> to replace already-published commits
(as in <a class="xref" href="#fixing-a-mistake-by-rewriting-history" title="Fixing a mistake by rewriting history">the section called “Fixing a mistake by rewriting history”</a>), or
</li><li class="listitem">
use <code class="literal">git rebase</code> to rebase any already-published commits (as
in <a class="xref" href="#using-git-rebase" title="Keeping a patch series up to date using git rebase">the section called “Keeping a patch series up to date using git rebase”</a>).
</li></ul></div><p>You may force <code class="literal">git push</code> to perform the update anyway by preceding the
branch name with a plus sign:</p><div class="literallayout"><p>$ git push ssh://yourserver.com/~you/proj.git +master</p></div><p>Normally whenever a branch head in a public repository is modified, it
is modified to point to a descendant of the commit that it pointed to
before. By forcing a push in this situation, you break that convention.
(See <a class="xref" href="#problems-With-rewriting-history" title="Problems with rewriting history">the section called “Problems with rewriting history”</a>.)</p><p>Nevertheless, this is a common practice for people that need a simple
way to publish a work-in-progress patch series, and it is an acceptable
compromise as long as you warn other developers that this is how you
intend to manage the branch.</p><p>It’s also possible for a push to fail in this way when other people have
the right to push to the same repository. In that case, the correct
solution is to retry the push after first updating your work: either by a
pull, or by a fetch followed by a rebase; see the
<a class="link" href="#setting-up-a-shared-repository" title="Setting up a shared repository">next section</a> and
<a class="ulink" href="gitcvs-migration.html" target="_top">gitcvs-migration(7)</a> for more.</p></div><div class="section" title="Setting up a shared repository"><div class="titlepage"><div><div><h3 class="title"><a name="setting-up-a-shared-repository"></a>Setting up a shared repository</h3></div></div></div><p>Another way to collaborate is by using a model similar to that
commonly used in CVS, where several developers with special rights
all push to and pull from a single shared repository. See
<a class="ulink" href="gitcvs-migration.html" target="_top">gitcvs-migration(7)</a> for instructions on how to
set this up.</p><p>However, while there is nothing wrong with git’s support for shared
repositories, this mode of operation is not generally recommended,
simply because the mode of collaboration that git supports—by
exchanging patches and pulling from public repositories—has so many
advantages over the central shared repository:</p><div class="itemizedlist"><ul class="itemizedlist" type="disc"><li class="listitem">
Git’s ability to quickly import and merge patches allows a
single maintainer to process incoming changes even at very
high rates. And when that becomes too much, <code class="literal">git pull</code> provides
an easy way for that maintainer to delegate this job to other
maintainers while still allowing optional review of incoming
changes.
</li><li class="listitem">
Since every developer’s repository has the same complete copy
of the project history, no repository is special, and it is
trivial for another developer to take over maintenance of a
project, either by mutual agreement, or because a maintainer
becomes unresponsive or difficult to work with.
</li><li class="listitem">
The lack of a central group of "committers" means there is
less need for formal decisions about who is "in" and who is
"out".
</li></ul></div></div><div class="section" title="Allowing web browsing of a repository"><div class="titlepage"><div><div><h3 class="title"><a name="setting-up-gitweb"></a>Allowing web browsing of a repository</h3></div></div></div><p>The gitweb cgi script provides users an easy way to browse your
project’s files and history without having to install git; see the file
gitweb/INSTALL in the git source tree for instructions on setting it up.</p></div></div><div class="section" title="Examples"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="sharing-development-examples"></a>Examples</h2></div></div></div><div class="section" title="Maintaining topic branches for a Linux subsystem maintainer"><div class="titlepage"><div><div><h3 class="title"><a name="maintaining-topic-branches"></a>Maintaining topic branches for a Linux subsystem maintainer</h3></div></div></div><p>This describes how Tony Luck uses git in his role as maintainer of the
IA64 architecture for the Linux kernel.</p><p>He uses two public branches:</p><div class="itemizedlist"><ul class="itemizedlist" type="disc"><li class="listitem">
A "test" tree into which patches are initially placed so that they
can get some exposure when integrated with other ongoing development.
This tree is available to Andrew for pulling into -mm whenever he
wants.
</li><li class="listitem">
A "release" tree into which tested patches are moved for final sanity
checking, and as a vehicle to send them upstream to Linus (by sending
him a "please pull" request.)
</li></ul></div><p>He also uses a set of temporary branches ("topic branches"), each
containing a logical grouping of patches.</p><p>To set this up, first create your work tree by cloning Linus’s public
tree:</p><div class="literallayout"><p>$ git clone git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6.git work<br>
$ cd work</p></div><p>Linus’s tree will be stored in the remote-tracking branch named origin/master,
and can be updated using <a class="ulink" href="git-fetch.html" target="_top">git-fetch(1)</a>; you can track other
public trees using <a class="ulink" href="git-remote.html" target="_top">git-remote(1)</a> to set up a "remote" and
<a class="ulink" href="git-fetch.html" target="_top">git-fetch(1)</a> to keep them up-to-date; see
<a class="xref" href="#repositories-and-branches" title="Chapter 1. Repositories and Branches">Chapter 1, <i>Repositories and Branches</i></a>.</p><p>Now create the branches in which you are going to work; these start out
at the current tip of origin/master branch, and should be set up (using
the --track option to <a class="ulink" href="git-branch.html" target="_top">git-branch(1)</a>) to merge changes in from
Linus by default.</p><div class="literallayout"><p>$ git branch --track test origin/master<br>
$ git branch --track release origin/master</p></div><p>These can be easily kept up to date using <a class="ulink" href="git-pull.html" target="_top">git-pull(1)</a>.</p><div class="literallayout"><p>$ git checkout test && git pull<br>
$ git checkout release && git pull</p></div><p>Important note! If you have any local changes in these branches, then
this merge will create a commit object in the history (with no local
changes git will simply do a "fast-forward" merge). Many people dislike
the "noise" that this creates in the Linux history, so you should avoid
doing this capriciously in the "release" branch, as these noisy commits
will become part of the permanent history when you ask Linus to pull
from the release branch.</p><p>A few configuration variables (see <a class="ulink" href="git-config.html" target="_top">git-config(1)</a>) can
make it easy to push both branches to your public tree. (See
<a class="xref" href="#setting-up-a-public-repository" title="Setting up a public repository">the section called “Setting up a public repository”</a>.)</p><div class="literallayout"><p>$ cat >> .git/config <<EOF<br>
[remote "mytree"]<br>
url = master.kernel.org:/pub/scm/linux/kernel/git/aegl/linux-2.6.git<br>
push = release<br>
push = test<br>
EOF</p></div><p>Then you can push both the test and release trees using
<a class="ulink" href="git-push.html" target="_top">git-push(1)</a>:</p><div class="literallayout"><p>$ git push mytree</p></div><p>or push just one of the test and release branches using:</p><div class="literallayout"><p>$ git push mytree test</p></div><p>or</p><div class="literallayout"><p>$ git push mytree release</p></div><p>Now to apply some patches from the community. Think of a short
snappy name for a branch to hold this patch (or related group of
patches), and create a new branch from a recent stable tag of
Linus’s branch. Picking a stable base for your branch will:
1) help you: by avoiding inclusion of unrelated and perhaps lightly
tested changes
2) help future bug hunters that use "git bisect" to find problems</p><div class="literallayout"><p>$ git checkout -b speed-up-spinlocks v2.6.35</p></div><p>Now you apply the patch(es), run some tests, and commit the change(s). If
the patch is a multi-part series, then you should apply each as a separate
commit to this branch.</p><div class="literallayout"><p>$ ... patch ... test ... commit [ ... patch ... test ... commit ]*</p></div><p>When you are happy with the state of this change, you can pull it into the
"test" branch in preparation to make it public:</p><div class="literallayout"><p>$ git checkout test && git pull . speed-up-spinlocks</p></div><p>It is unlikely that you would have any conflicts here … but you might if you
spent a while on this step and had also pulled new versions from upstream.</p><p>Some time later when enough time has passed and testing done, you can pull the
same branch into the "release" tree ready to go upstream. This is where you
see the value of keeping each patch (or patch series) in its own branch. It
means that the patches can be moved into the "release" tree in any order.</p><div class="literallayout"><p>$ git checkout release && git pull . speed-up-spinlocks</p></div><p>After a while, you will have a number of branches, and despite the
well chosen names you picked for each of them, you may forget what
they are for, or what status they are in. To get a reminder of what
changes are in a specific branch, use:</p><div class="literallayout"><p>$ git log linux..branchname | git shortlog</p></div><p>To see whether it has already been merged into the test or release branches,
use:</p><div class="literallayout"><p>$ git log test..branchname</p></div><p>or</p><div class="literallayout"><p>$ git log release..branchname</p></div><p>(If this branch has not yet been merged, you will see some log entries.
If it has been merged, then there will be no output.)</p><p>Once a patch completes the great cycle (moving from test to release,
then pulled by Linus, and finally coming back into your local
"origin/master" branch), the branch for this change is no longer needed.
You detect this when the output from:</p><div class="literallayout"><p>$ git log origin..branchname</p></div><p>is empty. At this point the branch can be deleted:</p><div class="literallayout"><p>$ git branch -d branchname</p></div><p>Some changes are so trivial that it is not necessary to create a separate
branch and then merge into each of the test and release branches. For
these changes, just apply directly to the "release" branch, and then
merge that into the "test" branch.</p><p>To create diffstat and shortlog summaries of changes to include in a "please
pull" request to Linus you can use:</p><div class="literallayout"><p>$ git diff --stat origin..release</p></div><p>and</p><div class="literallayout"><p>$ git log -p origin..release | git shortlog</p></div><p>Here are some of the scripts that simplify all this even further.</p><div class="literallayout"><p>==== update script ====<br>
# Update a branch in my GIT tree. If the branch to be updated<br>
# is origin, then pull from kernel.org. Otherwise merge<br>
# origin/master branch into test|release branch<br>
<br>
case "$1" in<br>
test|release)<br>
git checkout $1 && git pull . origin<br>
;;<br>
origin)<br>
before=$(git rev-parse refs/remotes/origin/master)<br>
git fetch origin<br>
after=$(git rev-parse refs/remotes/origin/master)<br>
if [ $before != $after ]<br>
then<br>
git log $before..$after | git shortlog<br>
fi<br>
;;<br>
*)<br>
echo "Usage: $0 origin|test|release" 1>&2<br>
exit 1<br>
;;<br>
esac</p></div><div class="literallayout"><p>==== merge script ====<br>
# Merge a branch into either the test or release branch<br>
<br>
pname=$0<br>
<br>
usage()<br>
{<br>
echo "Usage: $pname branch test|release" 1>&2<br>
exit 1<br>
}<br>
<br>
git show-ref -q --verify -- refs/heads/"$1" || {<br>
echo "Can't see branch <$1>" 1>&2<br>
usage<br>
}<br>
<br>
case "$2" in<br>
test|release)<br>
if [ $(git log $2..$1 | wc -c) -eq 0 ]<br>
then<br>
echo $1 already merged into $2 1>&2<br>
exit 1<br>
fi<br>
git checkout $2 && git pull . $1<br>
;;<br>
*)<br>
usage<br>
;;<br>
esac</p></div><div class="literallayout"><p>==== status script ====<br>
# report on status of my ia64 GIT tree<br>
<br>
gb=$(tput setab 2)<br>
rb=$(tput setab 1)<br>
restore=$(tput setab 9)<br>
<br>
if [ `git rev-list test..release | wc -c` -gt 0 ]<br>
then<br>
echo $rb Warning: commits in release that are not in test $restore<br>
git log test..release<br>
fi<br>
<br>
for branch in `git show-ref --heads | sed 's|^.*/||'`<br>
do<br>
if [ $branch = test -o $branch = release ]<br>
then<br>
continue<br>
fi<br>
<br>
echo -n $gb ======= $branch ====== $restore " "<br>
status=<br>
for ref in test release origin/master<br>
do<br>
if [ `git rev-list $ref..$branch | wc -c` -gt 0 ]<br>
then<br>
status=$status${ref:0:1}<br>
fi<br>
done<br>
case $status in<br>
trl)<br>
echo $rb Need to pull into test $restore<br>
;;<br>
rl)<br>
echo "In test"<br>
;;<br>
l)<br>
echo "Waiting for linus"<br>
;;<br>
"")<br>
echo $rb All done $restore<br>
;;<br>
*)<br>
echo $rb "<$status>" $restore<br>
;;<br>
esac<br>
git log origin/master..$branch | git shortlog<br>
done</p></div></div></div></div><div class="chapter" title="Chapter 5. Rewriting history and maintaining patch series"><div class="titlepage"><div><div><h2 class="title"><a name="cleaning-up-history"></a>Chapter 5. Rewriting history and maintaining patch series</h2></div></div></div><div class="toc"><p><b>Table of Contents</b></p><dl><dt><span class="section"><a href="#patch-series">Creating the perfect patch series</a></span></dt><dt><span class="section"><a href="#using-git-rebase">Keeping a patch series up to date using git rebase</a></span></dt><dt><span class="section"><a href="#rewriting-one-commit">Rewriting a single commit</a></span></dt><dt><span class="section"><a href="#reordering-patch-series">Reordering or selecting from a patch series</a></span></dt><dt><span class="section"><a href="#patch-series-tools">Other tools</a></span></dt><dt><span class="section"><a href="#problems-With-rewriting-history">Problems with rewriting history</a></span></dt><dt><span class="section"><a href="#bisect-merges">Why bisecting merge commits can be harder than bisecting linear history</a></span></dt></dl></div><p>Normally commits are only added to a project, never taken away or
replaced. Git is designed with this assumption, and violating it will