]> git.ipfire.org Git - thirdparty/git.git/blob - Documentation/user-manual.txt
Merge branch 'master' of git://git.kernel.org/pub/scm/gitk/gitk
[thirdparty/git.git] / Documentation / user-manual.txt
1 Git User's Manual (for version 1.5.1 or newer)
2 ______________________________________________
3
4
5 Git is a fast distributed revision control system.
6
7 This manual is designed to be readable by someone with basic unix
8 command-line skills, but no previous knowledge of git.
9
10 <<repositories-and-branches>> and <<exploring-git-history>> explain how
11 to fetch and study a project using git--read these chapters to learn how
12 to build and test a particular version of a software project, search for
13 regressions, and so on.
14
15 People needing to do actual development will also want to read
16 <<Developing-with-git>> and <<sharing-development>>.
17
18 Further chapters cover more specialized topics.
19
20 Comprehensive reference documentation is available through the man
21 pages. For a command such as "git clone", just use
22
23 ------------------------------------------------
24 $ man git-clone
25 ------------------------------------------------
26
27 See also <<git-quick-start>> for a brief overview of git commands,
28 without any explanation.
29
30 Finally, see <<todo>> for ways that you can help make this manual more
31 complete.
32
33
34 [[repositories-and-branches]]
35 Repositories and Branches
36 =========================
37
38 [[how-to-get-a-git-repository]]
39 How to get a git repository
40 ---------------------------
41
42 It will be useful to have a git repository to experiment with as you
43 read this manual.
44
45 The best way to get one is by using the gitlink:git-clone[1] command
46 to download a copy of an existing repository for a project that you
47 are interested in. If you don't already have a project in mind, here
48 are some interesting examples:
49
50 ------------------------------------------------
51 # git itself (approx. 10MB download):
52 $ git clone git://git.kernel.org/pub/scm/git/git.git
53 # the linux kernel (approx. 150MB download):
54 $ git clone git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6.git
55 ------------------------------------------------
56
57 The initial clone may be time-consuming for a large project, but you
58 will only need to clone once.
59
60 The clone command creates a new directory named after the project
61 ("git" or "linux-2.6" in the examples above). After you cd into this
62 directory, you will see that it contains a copy of the project files,
63 together with a special top-level directory named ".git", which
64 contains all the information about the history of the project.
65
66 In most of the following, examples will be taken from one of the two
67 repositories above.
68
69 [[how-to-check-out]]
70 How to check out a different version of a project
71 -------------------------------------------------
72
73 Git is best thought of as a tool for storing the history of a
74 collection of files. It stores the history as a compressed
75 collection of interrelated snapshots (versions) of the project's
76 contents.
77
78 A single git repository may contain multiple branches. It keeps track
79 of them by keeping a list of <<def_head,heads>> which reference the
80 latest version on each branch; the gitlink:git-branch[1] command shows
81 you the list of branch heads:
82
83 ------------------------------------------------
84 $ git branch
85 * master
86 ------------------------------------------------
87
88 A freshly cloned repository contains a single branch head, by default
89 named "master", with the working directory initialized to the state of
90 the project referred to by that branch head.
91
92 Most projects also use <<def_tag,tags>>. Tags, like heads, are
93 references into the project's history, and can be listed using the
94 gitlink:git-tag[1] command:
95
96 ------------------------------------------------
97 $ git tag -l
98 v2.6.11
99 v2.6.11-tree
100 v2.6.12
101 v2.6.12-rc2
102 v2.6.12-rc3
103 v2.6.12-rc4
104 v2.6.12-rc5
105 v2.6.12-rc6
106 v2.6.13
107 ...
108 ------------------------------------------------
109
110 Tags are expected to always point at the same version of a project,
111 while heads are expected to advance as development progresses.
112
113 Create a new branch head pointing to one of these versions and check it
114 out using gitlink:git-checkout[1]:
115
116 ------------------------------------------------
117 $ git checkout -b new v2.6.13
118 ------------------------------------------------
119
120 The working directory then reflects the contents that the project had
121 when it was tagged v2.6.13, and gitlink:git-branch[1] shows two
122 branches, with an asterisk marking the currently checked-out branch:
123
124 ------------------------------------------------
125 $ git branch
126 master
127 * new
128 ------------------------------------------------
129
130 If you decide that you'd rather see version 2.6.17, you can modify
131 the current branch to point at v2.6.17 instead, with
132
133 ------------------------------------------------
134 $ git reset --hard v2.6.17
135 ------------------------------------------------
136
137 Note that if the current branch head was your only reference to a
138 particular point in history, then resetting that branch may leave you
139 with no way to find the history it used to point to; so use this command
140 carefully.
141
142 [[understanding-commits]]
143 Understanding History: Commits
144 ------------------------------
145
146 Every change in the history of a project is represented by a commit.
147 The gitlink:git-show[1] command shows the most recent commit on the
148 current branch:
149
150 ------------------------------------------------
151 $ git show
152 commit 2b5f6dcce5bf94b9b119e9ed8d537098ec61c3d2
153 Author: Jamal Hadi Salim <hadi@cyberus.ca>
154 Date: Sat Dec 2 22:22:25 2006 -0800
155
156 [XFRM]: Fix aevent structuring to be more complete.
157
158 aevents can not uniquely identify an SA. We break the ABI with this
159 patch, but consensus is that since it is not yet utilized by any
160 (known) application then it is fine (better do it now than later).
161
162 Signed-off-by: Jamal Hadi Salim <hadi@cyberus.ca>
163 Signed-off-by: David S. Miller <davem@davemloft.net>
164
165 diff --git a/Documentation/networking/xfrm_sync.txt b/Documentation/networking/xfrm_sync.txt
166 index 8be626f..d7aac9d 100644
167 --- a/Documentation/networking/xfrm_sync.txt
168 +++ b/Documentation/networking/xfrm_sync.txt
169 @@ -47,10 +47,13 @@ aevent_id structure looks like:
170
171 struct xfrm_aevent_id {
172 struct xfrm_usersa_id sa_id;
173 + xfrm_address_t saddr;
174 __u32 flags;
175 + __u32 reqid;
176 };
177 ...
178 ------------------------------------------------
179
180 As you can see, a commit shows who made the latest change, what they
181 did, and why.
182
183 Every commit has a 40-hexdigit id, sometimes called the "object name" or the
184 "SHA1 id", shown on the first line of the "git show" output. You can usually
185 refer to a commit by a shorter name, such as a tag or a branch name, but this
186 longer name can also be useful. Most importantly, it is a globally unique
187 name for this commit: so if you tell somebody else the object name (for
188 example in email), then you are guaranteed that name will refer to the same
189 commit in their repository that it does in yours (assuming their repository
190 has that commit at all). Since the object name is computed as a hash over the
191 contents of the commit, you are guaranteed that the commit can never change
192 without its name also changing.
193
194 In fact, in <<git-internals>> we shall see that everything stored in git
195 history, including file data and directory contents, is stored in an object
196 with a name that is a hash of its contents.
197
198 [[understanding-reachability]]
199 Understanding history: commits, parents, and reachability
200 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
201
202 Every commit (except the very first commit in a project) also has a
203 parent commit which shows what happened before this commit.
204 Following the chain of parents will eventually take you back to the
205 beginning of the project.
206
207 However, the commits do not form a simple list; git allows lines of
208 development to diverge and then reconverge, and the point where two
209 lines of development reconverge is called a "merge". The commit
210 representing a merge can therefore have more than one parent, with
211 each parent representing the most recent commit on one of the lines
212 of development leading to that point.
213
214 The best way to see how this works is using the gitlink:gitk[1]
215 command; running gitk now on a git repository and looking for merge
216 commits will help understand how the git organizes history.
217
218 In the following, we say that commit X is "reachable" from commit Y
219 if commit X is an ancestor of commit Y. Equivalently, you could say
220 that Y is a descendent of X, or that there is a chain of parents
221 leading from commit Y to commit X.
222
223 [[history-diagrams]]
224 Understanding history: History diagrams
225 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
226
227 We will sometimes represent git history using diagrams like the one
228 below. Commits are shown as "o", and the links between them with
229 lines drawn with - / and \. Time goes left to right:
230
231
232 ................................................
233 o--o--o <-- Branch A
234 /
235 o--o--o <-- master
236 \
237 o--o--o <-- Branch B
238 ................................................
239
240 If we need to talk about a particular commit, the character "o" may
241 be replaced with another letter or number.
242
243 [[what-is-a-branch]]
244 Understanding history: What is a branch?
245 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
246
247 When we need to be precise, we will use the word "branch" to mean a line
248 of development, and "branch head" (or just "head") to mean a reference
249 to the most recent commit on a branch. In the example above, the branch
250 head named "A" is a pointer to one particular commit, but we refer to
251 the line of three commits leading up to that point as all being part of
252 "branch A".
253
254 However, when no confusion will result, we often just use the term
255 "branch" both for branches and for branch heads.
256
257 [[manipulating-branches]]
258 Manipulating branches
259 ---------------------
260
261 Creating, deleting, and modifying branches is quick and easy; here's
262 a summary of the commands:
263
264 git branch::
265 list all branches
266 git branch <branch>::
267 create a new branch named <branch>, referencing the same
268 point in history as the current branch
269 git branch <branch> <start-point>::
270 create a new branch named <branch>, referencing
271 <start-point>, which may be specified any way you like,
272 including using a branch name or a tag name
273 git branch -d <branch>::
274 delete the branch <branch>; if the branch you are deleting
275 points to a commit which is not reachable from the current
276 branch, this command will fail with a warning.
277 git branch -D <branch>::
278 even if the branch points to a commit not reachable
279 from the current branch, you may know that that commit
280 is still reachable from some other branch or tag. In that
281 case it is safe to use this command to force git to delete
282 the branch.
283 git checkout <branch>::
284 make the current branch <branch>, updating the working
285 directory to reflect the version referenced by <branch>
286 git checkout -b <new> <start-point>::
287 create a new branch <new> referencing <start-point>, and
288 check it out.
289
290 The special symbol "HEAD" can always be used to refer to the current
291 branch. In fact, git uses a file named "HEAD" in the .git directory to
292 remember which branch is current:
293
294 ------------------------------------------------
295 $ cat .git/HEAD
296 ref: refs/heads/master
297 ------------------------------------------------
298
299 [[detached-head]]
300 Examining an old version without creating a new branch
301 ------------------------------------------------------
302
303 The git-checkout command normally expects a branch head, but will also
304 accept an arbitrary commit; for example, you can check out the commit
305 referenced by a tag:
306
307 ------------------------------------------------
308 $ git checkout v2.6.17
309 Note: moving to "v2.6.17" which isn't a local branch
310 If you want to create a new branch from this checkout, you may do so
311 (now or later) by using -b with the checkout command again. Example:
312 git checkout -b <new_branch_name>
313 HEAD is now at 427abfa... Linux v2.6.17
314 ------------------------------------------------
315
316 The HEAD then refers to the SHA1 of the commit instead of to a branch,
317 and git branch shows that you are no longer on a branch:
318
319 ------------------------------------------------
320 $ cat .git/HEAD
321 427abfa28afedffadfca9dd8b067eb6d36bac53f
322 $ git branch
323 * (no branch)
324 master
325 ------------------------------------------------
326
327 In this case we say that the HEAD is "detached".
328
329 This is an easy way to check out a particular version without having to
330 make up a name for the new branch. You can still create a new branch
331 (or tag) for this version later if you decide to.
332
333 [[examining-remote-branches]]
334 Examining branches from a remote repository
335 -------------------------------------------
336
337 The "master" branch that was created at the time you cloned is a copy
338 of the HEAD in the repository that you cloned from. That repository
339 may also have had other branches, though, and your local repository
340 keeps branches which track each of those remote branches, which you
341 can view using the "-r" option to gitlink:git-branch[1]:
342
343 ------------------------------------------------
344 $ git branch -r
345 origin/HEAD
346 origin/html
347 origin/maint
348 origin/man
349 origin/master
350 origin/next
351 origin/pu
352 origin/todo
353 ------------------------------------------------
354
355 You cannot check out these remote-tracking branches, but you can
356 examine them on a branch of your own, just as you would a tag:
357
358 ------------------------------------------------
359 $ git checkout -b my-todo-copy origin/todo
360 ------------------------------------------------
361
362 Note that the name "origin" is just the name that git uses by default
363 to refer to the repository that you cloned from.
364
365 [[how-git-stores-references]]
366 Naming branches, tags, and other references
367 -------------------------------------------
368
369 Branches, remote-tracking branches, and tags are all references to
370 commits. All references are named with a slash-separated path name
371 starting with "refs"; the names we've been using so far are actually
372 shorthand:
373
374 - The branch "test" is short for "refs/heads/test".
375 - The tag "v2.6.18" is short for "refs/tags/v2.6.18".
376 - "origin/master" is short for "refs/remotes/origin/master".
377
378 The full name is occasionally useful if, for example, there ever
379 exists a tag and a branch with the same name.
380
381 As another useful shortcut, the "HEAD" of a repository can be referred
382 to just using the name of that repository. So, for example, "origin"
383 is usually a shortcut for the HEAD branch in the repository "origin".
384
385 For the complete list of paths which git checks for references, and
386 the order it uses to decide which to choose when there are multiple
387 references with the same shorthand name, see the "SPECIFYING
388 REVISIONS" section of gitlink:git-rev-parse[1].
389
390 [[Updating-a-repository-with-git-fetch]]
391 Updating a repository with git fetch
392 ------------------------------------
393
394 Eventually the developer cloned from will do additional work in her
395 repository, creating new commits and advancing the branches to point
396 at the new commits.
397
398 The command "git fetch", with no arguments, will update all of the
399 remote-tracking branches to the latest version found in her
400 repository. It will not touch any of your own branches--not even the
401 "master" branch that was created for you on clone.
402
403 [[fetching-branches]]
404 Fetching branches from other repositories
405 -----------------------------------------
406
407 You can also track branches from repositories other than the one you
408 cloned from, using gitlink:git-remote[1]:
409
410 -------------------------------------------------
411 $ git remote add linux-nfs git://linux-nfs.org/pub/nfs-2.6.git
412 $ git fetch linux-nfs
413 * refs/remotes/linux-nfs/master: storing branch 'master' ...
414 commit: bf81b46
415 -------------------------------------------------
416
417 New remote-tracking branches will be stored under the shorthand name
418 that you gave "git remote add", in this case linux-nfs:
419
420 -------------------------------------------------
421 $ git branch -r
422 linux-nfs/master
423 origin/master
424 -------------------------------------------------
425
426 If you run "git fetch <remote>" later, the tracking branches for the
427 named <remote> will be updated.
428
429 If you examine the file .git/config, you will see that git has added
430 a new stanza:
431
432 -------------------------------------------------
433 $ cat .git/config
434 ...
435 [remote "linux-nfs"]
436 url = git://linux-nfs.org/pub/nfs-2.6.git
437 fetch = +refs/heads/*:refs/remotes/linux-nfs/*
438 ...
439 -------------------------------------------------
440
441 This is what causes git to track the remote's branches; you may modify
442 or delete these configuration options by editing .git/config with a
443 text editor. (See the "CONFIGURATION FILE" section of
444 gitlink:git-config[1] for details.)
445
446 [[exploring-git-history]]
447 Exploring git history
448 =====================
449
450 Git is best thought of as a tool for storing the history of a
451 collection of files. It does this by storing compressed snapshots of
452 the contents of a file heirarchy, together with "commits" which show
453 the relationships between these snapshots.
454
455 Git provides extremely flexible and fast tools for exploring the
456 history of a project.
457
458 We start with one specialized tool that is useful for finding the
459 commit that introduced a bug into a project.
460
461 [[using-bisect]]
462 How to use bisect to find a regression
463 --------------------------------------
464
465 Suppose version 2.6.18 of your project worked, but the version at
466 "master" crashes. Sometimes the best way to find the cause of such a
467 regression is to perform a brute-force search through the project's
468 history to find the particular commit that caused the problem. The
469 gitlink:git-bisect[1] command can help you do this:
470
471 -------------------------------------------------
472 $ git bisect start
473 $ git bisect good v2.6.18
474 $ git bisect bad master
475 Bisecting: 3537 revisions left to test after this
476 [65934a9a028b88e83e2b0f8b36618fe503349f8e] BLOCK: Make USB storage depend on SCSI rather than selecting it [try #6]
477 -------------------------------------------------
478
479 If you run "git branch" at this point, you'll see that git has
480 temporarily moved you to a new branch named "bisect". This branch
481 points to a commit (with commit id 65934...) that is reachable from
482 v2.6.19 but not from v2.6.18. Compile and test it, and see whether
483 it crashes. Assume it does crash. Then:
484
485 -------------------------------------------------
486 $ git bisect bad
487 Bisecting: 1769 revisions left to test after this
488 [7eff82c8b1511017ae605f0c99ac275a7e21b867] i2c-core: Drop useless bitmaskings
489 -------------------------------------------------
490
491 checks out an older version. Continue like this, telling git at each
492 stage whether the version it gives you is good or bad, and notice
493 that the number of revisions left to test is cut approximately in
494 half each time.
495
496 After about 13 tests (in this case), it will output the commit id of
497 the guilty commit. You can then examine the commit with
498 gitlink:git-show[1], find out who wrote it, and mail them your bug
499 report with the commit id. Finally, run
500
501 -------------------------------------------------
502 $ git bisect reset
503 -------------------------------------------------
504
505 to return you to the branch you were on before and delete the
506 temporary "bisect" branch.
507
508 Note that the version which git-bisect checks out for you at each
509 point is just a suggestion, and you're free to try a different
510 version if you think it would be a good idea. For example,
511 occasionally you may land on a commit that broke something unrelated;
512 run
513
514 -------------------------------------------------
515 $ git bisect visualize
516 -------------------------------------------------
517
518 which will run gitk and label the commit it chose with a marker that
519 says "bisect". Chose a safe-looking commit nearby, note its commit
520 id, and check it out with:
521
522 -------------------------------------------------
523 $ git reset --hard fb47ddb2db...
524 -------------------------------------------------
525
526 then test, run "bisect good" or "bisect bad" as appropriate, and
527 continue.
528
529 [[naming-commits]]
530 Naming commits
531 --------------
532
533 We have seen several ways of naming commits already:
534
535 - 40-hexdigit object name
536 - branch name: refers to the commit at the head of the given
537 branch
538 - tag name: refers to the commit pointed to by the given tag
539 (we've seen branches and tags are special cases of
540 <<how-git-stores-references,references>>).
541 - HEAD: refers to the head of the current branch
542
543 There are many more; see the "SPECIFYING REVISIONS" section of the
544 gitlink:git-rev-parse[1] man page for the complete list of ways to
545 name revisions. Some examples:
546
547 -------------------------------------------------
548 $ git show fb47ddb2 # the first few characters of the object name
549 # are usually enough to specify it uniquely
550 $ git show HEAD^ # the parent of the HEAD commit
551 $ git show HEAD^^ # the grandparent
552 $ git show HEAD~4 # the great-great-grandparent
553 -------------------------------------------------
554
555 Recall that merge commits may have more than one parent; by default,
556 ^ and ~ follow the first parent listed in the commit, but you can
557 also choose:
558
559 -------------------------------------------------
560 $ git show HEAD^1 # show the first parent of HEAD
561 $ git show HEAD^2 # show the second parent of HEAD
562 -------------------------------------------------
563
564 In addition to HEAD, there are several other special names for
565 commits:
566
567 Merges (to be discussed later), as well as operations such as
568 git-reset, which change the currently checked-out commit, generally
569 set ORIG_HEAD to the value HEAD had before the current operation.
570
571 The git-fetch operation always stores the head of the last fetched
572 branch in FETCH_HEAD. For example, if you run git fetch without
573 specifying a local branch as the target of the operation
574
575 -------------------------------------------------
576 $ git fetch git://example.com/proj.git theirbranch
577 -------------------------------------------------
578
579 the fetched commits will still be available from FETCH_HEAD.
580
581 When we discuss merges we'll also see the special name MERGE_HEAD,
582 which refers to the other branch that we're merging in to the current
583 branch.
584
585 The gitlink:git-rev-parse[1] command is a low-level command that is
586 occasionally useful for translating some name for a commit to the object
587 name for that commit:
588
589 -------------------------------------------------
590 $ git rev-parse origin
591 e05db0fd4f31dde7005f075a84f96b360d05984b
592 -------------------------------------------------
593
594 [[creating-tags]]
595 Creating tags
596 -------------
597
598 We can also create a tag to refer to a particular commit; after
599 running
600
601 -------------------------------------------------
602 $ git tag stable-1 1b2e1d63ff
603 -------------------------------------------------
604
605 You can use stable-1 to refer to the commit 1b2e1d63ff.
606
607 This creates a "lightweight" tag. If you would also like to include a
608 comment with the tag, and possibly sign it cryptographically, then you
609 should create a tag object instead; see the gitlink:git-tag[1] man page
610 for details.
611
612 [[browsing-revisions]]
613 Browsing revisions
614 ------------------
615
616 The gitlink:git-log[1] command can show lists of commits. On its
617 own, it shows all commits reachable from the parent commit; but you
618 can also make more specific requests:
619
620 -------------------------------------------------
621 $ git log v2.5.. # commits since (not reachable from) v2.5
622 $ git log test..master # commits reachable from master but not test
623 $ git log master..test # ...reachable from test but not master
624 $ git log master...test # ...reachable from either test or master,
625 # but not both
626 $ git log --since="2 weeks ago" # commits from the last 2 weeks
627 $ git log Makefile # commits which modify Makefile
628 $ git log fs/ # ... which modify any file under fs/
629 $ git log -S'foo()' # commits which add or remove any file data
630 # matching the string 'foo()'
631 -------------------------------------------------
632
633 And of course you can combine all of these; the following finds
634 commits since v2.5 which touch the Makefile or any file under fs:
635
636 -------------------------------------------------
637 $ git log v2.5.. Makefile fs/
638 -------------------------------------------------
639
640 You can also ask git log to show patches:
641
642 -------------------------------------------------
643 $ git log -p
644 -------------------------------------------------
645
646 See the "--pretty" option in the gitlink:git-log[1] man page for more
647 display options.
648
649 Note that git log starts with the most recent commit and works
650 backwards through the parents; however, since git history can contain
651 multiple independent lines of development, the particular order that
652 commits are listed in may be somewhat arbitrary.
653
654 [[generating-diffs]]
655 Generating diffs
656 ----------------
657
658 You can generate diffs between any two versions using
659 gitlink:git-diff[1]:
660
661 -------------------------------------------------
662 $ git diff master..test
663 -------------------------------------------------
664
665 Sometimes what you want instead is a set of patches:
666
667 -------------------------------------------------
668 $ git format-patch master..test
669 -------------------------------------------------
670
671 will generate a file with a patch for each commit reachable from test
672 but not from master. Note that if master also has commits which are
673 not reachable from test, then the combined result of these patches
674 will not be the same as the diff produced by the git-diff example.
675
676 [[viewing-old-file-versions]]
677 Viewing old file versions
678 -------------------------
679
680 You can always view an old version of a file by just checking out the
681 correct revision first. But sometimes it is more convenient to be
682 able to view an old version of a single file without checking
683 anything out; this command does that:
684
685 -------------------------------------------------
686 $ git show v2.5:fs/locks.c
687 -------------------------------------------------
688
689 Before the colon may be anything that names a commit, and after it
690 may be any path to a file tracked by git.
691
692 [[history-examples]]
693 Examples
694 --------
695
696 [[counting-commits-on-a-branch]]
697 Counting the number of commits on a branch
698 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
699
700 Suppose you want to know how many commits you've made on "mybranch"
701 since it diverged from "origin":
702
703 -------------------------------------------------
704 $ git log --pretty=oneline origin..mybranch | wc -l
705 -------------------------------------------------
706
707 Alternatively, you may often see this sort of thing done with the
708 lower-level command gitlink:git-rev-list[1], which just lists the SHA1's
709 of all the given commits:
710
711 -------------------------------------------------
712 $ git rev-list origin..mybranch | wc -l
713 -------------------------------------------------
714
715 [[checking-for-equal-branches]]
716 Check whether two branches point at the same history
717 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
718
719 Suppose you want to check whether two branches point at the same point
720 in history.
721
722 -------------------------------------------------
723 $ git diff origin..master
724 -------------------------------------------------
725
726 will tell you whether the contents of the project are the same at the
727 two branches; in theory, however, it's possible that the same project
728 contents could have been arrived at by two different historical
729 routes. You could compare the object names:
730
731 -------------------------------------------------
732 $ git rev-list origin
733 e05db0fd4f31dde7005f075a84f96b360d05984b
734 $ git rev-list master
735 e05db0fd4f31dde7005f075a84f96b360d05984b
736 -------------------------------------------------
737
738 Or you could recall that the ... operator selects all commits
739 contained reachable from either one reference or the other but not
740 both: so
741
742 -------------------------------------------------
743 $ git log origin...master
744 -------------------------------------------------
745
746 will return no commits when the two branches are equal.
747
748 [[finding-tagged-descendants]]
749 Find first tagged version including a given fix
750 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
751
752 Suppose you know that the commit e05db0fd fixed a certain problem.
753 You'd like to find the earliest tagged release that contains that
754 fix.
755
756 Of course, there may be more than one answer--if the history branched
757 after commit e05db0fd, then there could be multiple "earliest" tagged
758 releases.
759
760 You could just visually inspect the commits since e05db0fd:
761
762 -------------------------------------------------
763 $ gitk e05db0fd..
764 -------------------------------------------------
765
766 Or you can use gitlink:git-name-rev[1], which will give the commit a
767 name based on any tag it finds pointing to one of the commit's
768 descendants:
769
770 -------------------------------------------------
771 $ git name-rev --tags e05db0fd
772 e05db0fd tags/v1.5.0-rc1^0~23
773 -------------------------------------------------
774
775 The gitlink:git-describe[1] command does the opposite, naming the
776 revision using a tag on which the given commit is based:
777
778 -------------------------------------------------
779 $ git describe e05db0fd
780 v1.5.0-rc0-260-ge05db0f
781 -------------------------------------------------
782
783 but that may sometimes help you guess which tags might come after the
784 given commit.
785
786 If you just want to verify whether a given tagged version contains a
787 given commit, you could use gitlink:git-merge-base[1]:
788
789 -------------------------------------------------
790 $ git merge-base e05db0fd v1.5.0-rc1
791 e05db0fd4f31dde7005f075a84f96b360d05984b
792 -------------------------------------------------
793
794 The merge-base command finds a common ancestor of the given commits,
795 and always returns one or the other in the case where one is a
796 descendant of the other; so the above output shows that e05db0fd
797 actually is an ancestor of v1.5.0-rc1.
798
799 Alternatively, note that
800
801 -------------------------------------------------
802 $ git log v1.5.0-rc1..e05db0fd
803 -------------------------------------------------
804
805 will produce empty output if and only if v1.5.0-rc1 includes e05db0fd,
806 because it outputs only commits that are not reachable from v1.5.0-rc1.
807
808 As yet another alternative, the gitlink:git-show-branch[1] command lists
809 the commits reachable from its arguments with a display on the left-hand
810 side that indicates which arguments that commit is reachable from. So,
811 you can run something like
812
813 -------------------------------------------------
814 $ git show-branch e05db0fd v1.5.0-rc0 v1.5.0-rc1 v1.5.0-rc2
815 ! [e05db0fd] Fix warnings in sha1_file.c - use C99 printf format if
816 available
817 ! [v1.5.0-rc0] GIT v1.5.0 preview
818 ! [v1.5.0-rc1] GIT v1.5.0-rc1
819 ! [v1.5.0-rc2] GIT v1.5.0-rc2
820 ...
821 -------------------------------------------------
822
823 then search for a line that looks like
824
825 -------------------------------------------------
826 + ++ [e05db0fd] Fix warnings in sha1_file.c - use C99 printf format if
827 available
828 -------------------------------------------------
829
830 Which shows that e05db0fd is reachable from itself, from v1.5.0-rc1, and
831 from v1.5.0-rc2, but not from v1.5.0-rc0.
832
833 [[showing-commits-unique-to-a-branch]]
834 Showing commits unique to a given branch
835 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
836
837 Suppose you would like to see all the commits reachable from the branch
838 head named "master" but not from any other head in your repository.
839
840 We can list all the heads in this repository with
841 gitlink:git-show-ref[1]:
842
843 -------------------------------------------------
844 $ git show-ref --heads
845 bf62196b5e363d73353a9dcf094c59595f3153b7 refs/heads/core-tutorial
846 db768d5504c1bb46f63ee9d6e1772bd047e05bf9 refs/heads/maint
847 a07157ac624b2524a059a3414e99f6f44bebc1e7 refs/heads/master
848 24dbc180ea14dc1aebe09f14c8ecf32010690627 refs/heads/tutorial-2
849 1e87486ae06626c2f31eaa63d26fc0fd646c8af2 refs/heads/tutorial-fixes
850 -------------------------------------------------
851
852 We can get just the branch-head names, and remove "master", with
853 the help of the standard utilities cut and grep:
854
855 -------------------------------------------------
856 $ git show-ref --heads | cut -d' ' -f2 | grep -v '^refs/heads/master'
857 refs/heads/core-tutorial
858 refs/heads/maint
859 refs/heads/tutorial-2
860 refs/heads/tutorial-fixes
861 -------------------------------------------------
862
863 And then we can ask to see all the commits reachable from master
864 but not from these other heads:
865
866 -------------------------------------------------
867 $ gitk master --not $( git show-ref --heads | cut -d' ' -f2 |
868 grep -v '^refs/heads/master' )
869 -------------------------------------------------
870
871 Obviously, endless variations are possible; for example, to see all
872 commits reachable from some head but not from any tag in the repository:
873
874 -------------------------------------------------
875 $ gitk $( git show-ref --heads ) --not $( git show-ref --tags )
876 -------------------------------------------------
877
878 (See gitlink:git-rev-parse[1] for explanations of commit-selecting
879 syntax such as `--not`.)
880
881 [[making-a-release]]
882 Creating a changelog and tarball for a software release
883 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
884
885 The gitlink:git-archive[1] command can create a tar or zip archive from
886 any version of a project; for example:
887
888 -------------------------------------------------
889 $ git archive --format=tar --prefix=project/ HEAD | gzip >latest.tar.gz
890 -------------------------------------------------
891
892 will use HEAD to produce a tar archive in which each filename is
893 preceded by "project/".
894
895 If you're releasing a new version of a software project, you may want
896 to simultaneously make a changelog to include in the release
897 announcement.
898
899 Linus Torvalds, for example, makes new kernel releases by tagging them,
900 then running:
901
902 -------------------------------------------------
903 $ release-script 2.6.12 2.6.13-rc6 2.6.13-rc7
904 -------------------------------------------------
905
906 where release-script is a shell script that looks like:
907
908 -------------------------------------------------
909 #!/bin/sh
910 stable="$1"
911 last="$2"
912 new="$3"
913 echo "# git tag v$new"
914 echo "git archive --prefix=linux-$new/ v$new | gzip -9 > ../linux-$new.tar.gz"
915 echo "git diff v$stable v$new | gzip -9 > ../patch-$new.gz"
916 echo "git log --no-merges v$new ^v$last > ../ChangeLog-$new"
917 echo "git shortlog --no-merges v$new ^v$last > ../ShortLog"
918 echo "git diff --stat --summary -M v$last v$new > ../diffstat-$new"
919 -------------------------------------------------
920
921 and then he just cut-and-pastes the output commands after verifying that
922 they look OK.
923
924 [[Finding-comments-with-given-content]]
925 Finding commits referencing a file with given content
926 -----------------------------------------------------
927
928 Somebody hands you a copy of a file, and asks which commits modified a
929 file such that it contained the given content either before or after the
930 commit. You can find out with this:
931
932 -------------------------------------------------
933 $ git log --raw -r --abbrev=40 --pretty=oneline -- filename |
934 grep -B 1 `git hash-object filename`
935 -------------------------------------------------
936
937 Figuring out why this works is left as an exercise to the (advanced)
938 student. The gitlink:git-log[1], gitlink:git-diff-tree[1], and
939 gitlink:git-hash-object[1] man pages may prove helpful.
940
941 [[Developing-with-git]]
942 Developing with git
943 ===================
944
945 [[telling-git-your-name]]
946 Telling git your name
947 ---------------------
948
949 Before creating any commits, you should introduce yourself to git. The
950 easiest way to do so is to make sure the following lines appear in a
951 file named .gitconfig in your home directory:
952
953 ------------------------------------------------
954 [user]
955 name = Your Name Comes Here
956 email = you@yourdomain.example.com
957 ------------------------------------------------
958
959 (See the "CONFIGURATION FILE" section of gitlink:git-config[1] for
960 details on the configuration file.)
961
962
963 [[creating-a-new-repository]]
964 Creating a new repository
965 -------------------------
966
967 Creating a new repository from scratch is very easy:
968
969 -------------------------------------------------
970 $ mkdir project
971 $ cd project
972 $ git init
973 -------------------------------------------------
974
975 If you have some initial content (say, a tarball):
976
977 -------------------------------------------------
978 $ tar -xzvf project.tar.gz
979 $ cd project
980 $ git init
981 $ git add . # include everything below ./ in the first commit:
982 $ git commit
983 -------------------------------------------------
984
985 [[how-to-make-a-commit]]
986 How to make a commit
987 --------------------
988
989 Creating a new commit takes three steps:
990
991 1. Making some changes to the working directory using your
992 favorite editor.
993 2. Telling git about your changes.
994 3. Creating the commit using the content you told git about
995 in step 2.
996
997 In practice, you can interleave and repeat steps 1 and 2 as many
998 times as you want: in order to keep track of what you want committed
999 at step 3, git maintains a snapshot of the tree's contents in a
1000 special staging area called "the index."
1001
1002 At the beginning, the content of the index will be identical to
1003 that of the HEAD. The command "git diff --cached", which shows
1004 the difference between the HEAD and the index, should therefore
1005 produce no output at that point.
1006
1007 Modifying the index is easy:
1008
1009 To update the index with the new contents of a modified file, use
1010
1011 -------------------------------------------------
1012 $ git add path/to/file
1013 -------------------------------------------------
1014
1015 To add the contents of a new file to the index, use
1016
1017 -------------------------------------------------
1018 $ git add path/to/file
1019 -------------------------------------------------
1020
1021 To remove a file from the index and from the working tree,
1022
1023 -------------------------------------------------
1024 $ git rm path/to/file
1025 -------------------------------------------------
1026
1027 After each step you can verify that
1028
1029 -------------------------------------------------
1030 $ git diff --cached
1031 -------------------------------------------------
1032
1033 always shows the difference between the HEAD and the index file--this
1034 is what you'd commit if you created the commit now--and that
1035
1036 -------------------------------------------------
1037 $ git diff
1038 -------------------------------------------------
1039
1040 shows the difference between the working tree and the index file.
1041
1042 Note that "git add" always adds just the current contents of a file
1043 to the index; further changes to the same file will be ignored unless
1044 you run git-add on the file again.
1045
1046 When you're ready, just run
1047
1048 -------------------------------------------------
1049 $ git commit
1050 -------------------------------------------------
1051
1052 and git will prompt you for a commit message and then create the new
1053 commit. Check to make sure it looks like what you expected with
1054
1055 -------------------------------------------------
1056 $ git show
1057 -------------------------------------------------
1058
1059 As a special shortcut,
1060
1061 -------------------------------------------------
1062 $ git commit -a
1063 -------------------------------------------------
1064
1065 will update the index with any files that you've modified or removed
1066 and create a commit, all in one step.
1067
1068 A number of commands are useful for keeping track of what you're
1069 about to commit:
1070
1071 -------------------------------------------------
1072 $ git diff --cached # difference between HEAD and the index; what
1073 # would be commited if you ran "commit" now.
1074 $ git diff # difference between the index file and your
1075 # working directory; changes that would not
1076 # be included if you ran "commit" now.
1077 $ git diff HEAD # difference between HEAD and working tree; what
1078 # would be committed if you ran "commit -a" now.
1079 $ git status # a brief per-file summary of the above.
1080 -------------------------------------------------
1081
1082 [[creating-good-commit-messages]]
1083 Creating good commit messages
1084 -----------------------------
1085
1086 Though not required, it's a good idea to begin the commit message
1087 with a single short (less than 50 character) line summarizing the
1088 change, followed by a blank line and then a more thorough
1089 description. Tools that turn commits into email, for example, use
1090 the first line on the Subject line and the rest of the commit in the
1091 body.
1092
1093 [[ignoring-files]]
1094 Ignoring files
1095 --------------
1096
1097 A project will often generate files that you do 'not' want to track with git.
1098 This typically includes files generated by a build process or temporary
1099 backup files made by your editor. Of course, 'not' tracking files with git
1100 is just a matter of 'not' calling "`git add`" on them. But it quickly becomes
1101 annoying to have these untracked files lying around; e.g. they make
1102 "`git add .`" and "`git commit -a`" practically useless, and they keep
1103 showing up in the output of "`git status`", etc.
1104
1105 Git therefore provides "exclude patterns" for telling git which files to
1106 actively ignore. Exclude patterns are thoroughly explained in the
1107 gitlink:gitignore[5] manual page, but the heart of the concept is simply
1108 a list of files which git should ignore. Entries in the list may contain
1109 globs to specify multiple files, or may be prefixed by "`!`" to
1110 explicitly include (un-ignore) a previously excluded (ignored) file
1111 (i.e. later exclude patterns override earlier ones). The following
1112 example should illustrate such patterns:
1113
1114 -------------------------------------------------
1115 # Lines starting with '#' are considered comments.
1116 # Ignore foo.txt.
1117 foo.txt
1118 # Ignore (generated) html files,
1119 *.html
1120 # except foo.html which is maintained by hand.
1121 !foo.html
1122 # Ignore objects and archives.
1123 *.[oa]
1124 -------------------------------------------------
1125
1126 The next question is where to put these exclude patterns so that git can
1127 find them. Git looks for exclude patterns in the following files:
1128
1129 `.gitignore` files in your working tree:::
1130 You may store multiple `.gitignore` files at various locations in your
1131 working tree. Each `.gitignore` file is applied to the directory where
1132 it's located, including its subdirectories. Furthermore, the
1133 `.gitignore` files can be tracked like any other files in your working
1134 tree; just do a "`git add .gitignore`" and commit. `.gitignore` is
1135 therefore the right place to put exclude patterns that are meant to
1136 be shared between all project participants, such as build output files
1137 (e.g. `\*.o`), etc.
1138 `.git/info/exclude` in your repo:::
1139 Exclude patterns in this file are applied to the working tree as a
1140 whole. Since the file is not located in your working tree, it does
1141 not follow push/pull/clone like `.gitignore` can do. This is therefore
1142 the place to put exclude patterns that are local to your copy of the
1143 repo (i.e. 'not' shared between project participants), such as
1144 temporary backup files made by your editor (e.g. `\*~`), etc.
1145 The file specified by the `core.excludesfile` config directive:::
1146 By setting the `core.excludesfile` config directive you can tell git
1147 where to find more exclude patterns (see gitlink:git-config[1] for
1148 more information on configuration options). This config directive
1149 can be set in the per-repo `.git/config` file, in which case the
1150 exclude patterns will apply to that repo only. Alternatively, you
1151 can set the directive in the global `~/.gitconfig` file to apply
1152 the exclude pattern to all your git repos. As with the above
1153 `.git/info/exclude` (and, indeed, with git config directives in
1154 general), this directive does not follow push/pull/clone, but remain
1155 local to your repo(s).
1156
1157 [NOTE]
1158 In addition to the above alternatives, there are git commands that can take
1159 exclude patterns directly on the command line. See gitlink:git-ls-files[1]
1160 for an example of this.
1161
1162 [[how-to-merge]]
1163 How to merge
1164 ------------
1165
1166 You can rejoin two diverging branches of development using
1167 gitlink:git-merge[1]:
1168
1169 -------------------------------------------------
1170 $ git merge branchname
1171 -------------------------------------------------
1172
1173 merges the development in the branch "branchname" into the current
1174 branch. If there are conflicts--for example, if the same file is
1175 modified in two different ways in the remote branch and the local
1176 branch--then you are warned; the output may look something like this:
1177
1178 -------------------------------------------------
1179 $ git merge next
1180 100% (4/4) done
1181 Auto-merged file.txt
1182 CONFLICT (content): Merge conflict in file.txt
1183 Automatic merge failed; fix conflicts and then commit the result.
1184 -------------------------------------------------
1185
1186 Conflict markers are left in the problematic files, and after
1187 you resolve the conflicts manually, you can update the index
1188 with the contents and run git commit, as you normally would when
1189 creating a new file.
1190
1191 If you examine the resulting commit using gitk, you will see that it
1192 has two parents, one pointing to the top of the current branch, and
1193 one to the top of the other branch.
1194
1195 [[resolving-a-merge]]
1196 Resolving a merge
1197 -----------------
1198
1199 When a merge isn't resolved automatically, git leaves the index and
1200 the working tree in a special state that gives you all the
1201 information you need to help resolve the merge.
1202
1203 Files with conflicts are marked specially in the index, so until you
1204 resolve the problem and update the index, gitlink:git-commit[1] will
1205 fail:
1206
1207 -------------------------------------------------
1208 $ git commit
1209 file.txt: needs merge
1210 -------------------------------------------------
1211
1212 Also, gitlink:git-status[1] will list those files as "unmerged", and the
1213 files with conflicts will have conflict markers added, like this:
1214
1215 -------------------------------------------------
1216 <<<<<<< HEAD:file.txt
1217 Hello world
1218 =======
1219 Goodbye
1220 >>>>>>> 77976da35a11db4580b80ae27e8d65caf5208086:file.txt
1221 -------------------------------------------------
1222
1223 All you need to do is edit the files to resolve the conflicts, and then
1224
1225 -------------------------------------------------
1226 $ git add file.txt
1227 $ git commit
1228 -------------------------------------------------
1229
1230 Note that the commit message will already be filled in for you with
1231 some information about the merge. Normally you can just use this
1232 default message unchanged, but you may add additional commentary of
1233 your own if desired.
1234
1235 The above is all you need to know to resolve a simple merge. But git
1236 also provides more information to help resolve conflicts:
1237
1238 [[conflict-resolution]]
1239 Getting conflict-resolution help during a merge
1240 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1241
1242 All of the changes that git was able to merge automatically are
1243 already added to the index file, so gitlink:git-diff[1] shows only
1244 the conflicts. It uses an unusual syntax:
1245
1246 -------------------------------------------------
1247 $ git diff
1248 diff --cc file.txt
1249 index 802992c,2b60207..0000000
1250 --- a/file.txt
1251 +++ b/file.txt
1252 @@@ -1,1 -1,1 +1,5 @@@
1253 ++<<<<<<< HEAD:file.txt
1254 +Hello world
1255 ++=======
1256 + Goodbye
1257 ++>>>>>>> 77976da35a11db4580b80ae27e8d65caf5208086:file.txt
1258 -------------------------------------------------
1259
1260 Recall that the commit which will be commited after we resolve this
1261 conflict will have two parents instead of the usual one: one parent
1262 will be HEAD, the tip of the current branch; the other will be the
1263 tip of the other branch, which is stored temporarily in MERGE_HEAD.
1264
1265 During the merge, the index holds three versions of each file. Each of
1266 these three "file stages" represents a different version of the file:
1267
1268 -------------------------------------------------
1269 $ git show :1:file.txt # the file in a common ancestor of both branches
1270 $ git show :2:file.txt # the version from HEAD, but including any
1271 # nonconflicting changes from MERGE_HEAD
1272 $ git show :3:file.txt # the version from MERGE_HEAD, but including any
1273 # nonconflicting changes from HEAD.
1274 -------------------------------------------------
1275
1276 Since the stage 2 and stage 3 versions have already been updated with
1277 nonconflicting changes, the only remaining differences between them are
1278 the important ones; thus gitlink:git-diff[1] can use the information in
1279 the index to show only those conflicts.
1280
1281 The diff above shows the differences between the working-tree version of
1282 file.txt and the stage 2 and stage 3 versions. So instead of preceding
1283 each line by a single "+" or "-", it now uses two columns: the first
1284 column is used for differences between the first parent and the working
1285 directory copy, and the second for differences between the second parent
1286 and the working directory copy. (See the "COMBINED DIFF FORMAT" section
1287 of gitlink:git-diff-files[1] for a details of the format.)
1288
1289 After resolving the conflict in the obvious way (but before updating the
1290 index), the diff will look like:
1291
1292 -------------------------------------------------
1293 $ git diff
1294 diff --cc file.txt
1295 index 802992c,2b60207..0000000
1296 --- a/file.txt
1297 +++ b/file.txt
1298 @@@ -1,1 -1,1 +1,1 @@@
1299 - Hello world
1300 -Goodbye
1301 ++Goodbye world
1302 -------------------------------------------------
1303
1304 This shows that our resolved version deleted "Hello world" from the
1305 first parent, deleted "Goodbye" from the second parent, and added
1306 "Goodbye world", which was previously absent from both.
1307
1308 Some special diff options allow diffing the working directory against
1309 any of these stages:
1310
1311 -------------------------------------------------
1312 $ git diff -1 file.txt # diff against stage 1
1313 $ git diff --base file.txt # same as the above
1314 $ git diff -2 file.txt # diff against stage 2
1315 $ git diff --ours file.txt # same as the above
1316 $ git diff -3 file.txt # diff against stage 3
1317 $ git diff --theirs file.txt # same as the above.
1318 -------------------------------------------------
1319
1320 The gitlink:git-log[1] and gitk[1] commands also provide special help
1321 for merges:
1322
1323 -------------------------------------------------
1324 $ git log --merge
1325 $ gitk --merge
1326 -------------------------------------------------
1327
1328 These will display all commits which exist only on HEAD or on
1329 MERGE_HEAD, and which touch an unmerged file.
1330
1331 You may also use gitlink:git-mergetool[1], which lets you merge the
1332 unmerged files using external tools such as emacs or kdiff3.
1333
1334 Each time you resolve the conflicts in a file and update the index:
1335
1336 -------------------------------------------------
1337 $ git add file.txt
1338 -------------------------------------------------
1339
1340 the different stages of that file will be "collapsed", after which
1341 git-diff will (by default) no longer show diffs for that file.
1342
1343 [[undoing-a-merge]]
1344 Undoing a merge
1345 ---------------
1346
1347 If you get stuck and decide to just give up and throw the whole mess
1348 away, you can always return to the pre-merge state with
1349
1350 -------------------------------------------------
1351 $ git reset --hard HEAD
1352 -------------------------------------------------
1353
1354 Or, if you've already commited the merge that you want to throw away,
1355
1356 -------------------------------------------------
1357 $ git reset --hard ORIG_HEAD
1358 -------------------------------------------------
1359
1360 However, this last command can be dangerous in some cases--never
1361 throw away a commit you have already committed if that commit may
1362 itself have been merged into another branch, as doing so may confuse
1363 further merges.
1364
1365 [[fast-forwards]]
1366 Fast-forward merges
1367 -------------------
1368
1369 There is one special case not mentioned above, which is treated
1370 differently. Normally, a merge results in a merge commit, with two
1371 parents, one pointing at each of the two lines of development that
1372 were merged.
1373
1374 However, if the current branch is a descendant of the other--so every
1375 commit present in the one is already contained in the other--then git
1376 just performs a "fast forward"; the head of the current branch is moved
1377 forward to point at the head of the merged-in branch, without any new
1378 commits being created.
1379
1380 [[fixing-mistakes]]
1381 Fixing mistakes
1382 ---------------
1383
1384 If you've messed up the working tree, but haven't yet committed your
1385 mistake, you can return the entire working tree to the last committed
1386 state with
1387
1388 -------------------------------------------------
1389 $ git reset --hard HEAD
1390 -------------------------------------------------
1391
1392 If you make a commit that you later wish you hadn't, there are two
1393 fundamentally different ways to fix the problem:
1394
1395 1. You can create a new commit that undoes whatever was done
1396 by the previous commit. This is the correct thing if your
1397 mistake has already been made public.
1398
1399 2. You can go back and modify the old commit. You should
1400 never do this if you have already made the history public;
1401 git does not normally expect the "history" of a project to
1402 change, and cannot correctly perform repeated merges from
1403 a branch that has had its history changed.
1404
1405 [[reverting-a-commit]]
1406 Fixing a mistake with a new commit
1407 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1408
1409 Creating a new commit that reverts an earlier change is very easy;
1410 just pass the gitlink:git-revert[1] command a reference to the bad
1411 commit; for example, to revert the most recent commit:
1412
1413 -------------------------------------------------
1414 $ git revert HEAD
1415 -------------------------------------------------
1416
1417 This will create a new commit which undoes the change in HEAD. You
1418 will be given a chance to edit the commit message for the new commit.
1419
1420 You can also revert an earlier change, for example, the next-to-last:
1421
1422 -------------------------------------------------
1423 $ git revert HEAD^
1424 -------------------------------------------------
1425
1426 In this case git will attempt to undo the old change while leaving
1427 intact any changes made since then. If more recent changes overlap
1428 with the changes to be reverted, then you will be asked to fix
1429 conflicts manually, just as in the case of <<resolving-a-merge,
1430 resolving a merge>>.
1431
1432 [[fixing-a-mistake-by-editing-history]]
1433 Fixing a mistake by editing history
1434 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1435
1436 If the problematic commit is the most recent commit, and you have not
1437 yet made that commit public, then you may just
1438 <<undoing-a-merge,destroy it using git-reset>>.
1439
1440 Alternatively, you
1441 can edit the working directory and update the index to fix your
1442 mistake, just as if you were going to <<how-to-make-a-commit,create a
1443 new commit>>, then run
1444
1445 -------------------------------------------------
1446 $ git commit --amend
1447 -------------------------------------------------
1448
1449 which will replace the old commit by a new commit incorporating your
1450 changes, giving you a chance to edit the old commit message first.
1451
1452 Again, you should never do this to a commit that may already have
1453 been merged into another branch; use gitlink:git-revert[1] instead in
1454 that case.
1455
1456 It is also possible to edit commits further back in the history, but
1457 this is an advanced topic to be left for
1458 <<cleaning-up-history,another chapter>>.
1459
1460 [[checkout-of-path]]
1461 Checking out an old version of a file
1462 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1463
1464 In the process of undoing a previous bad change, you may find it
1465 useful to check out an older version of a particular file using
1466 gitlink:git-checkout[1]. We've used git checkout before to switch
1467 branches, but it has quite different behavior if it is given a path
1468 name: the command
1469
1470 -------------------------------------------------
1471 $ git checkout HEAD^ path/to/file
1472 -------------------------------------------------
1473
1474 replaces path/to/file by the contents it had in the commit HEAD^, and
1475 also updates the index to match. It does not change branches.
1476
1477 If you just want to look at an old version of the file, without
1478 modifying the working directory, you can do that with
1479 gitlink:git-show[1]:
1480
1481 -------------------------------------------------
1482 $ git show HEAD^:path/to/file
1483 -------------------------------------------------
1484
1485 which will display the given version of the file.
1486
1487 [[ensuring-good-performance]]
1488 Ensuring good performance
1489 -------------------------
1490
1491 On large repositories, git depends on compression to keep the history
1492 information from taking up to much space on disk or in memory.
1493
1494 This compression is not performed automatically. Therefore you
1495 should occasionally run gitlink:git-gc[1]:
1496
1497 -------------------------------------------------
1498 $ git gc
1499 -------------------------------------------------
1500
1501 to recompress the archive. This can be very time-consuming, so
1502 you may prefer to run git-gc when you are not doing other work.
1503
1504
1505 [[ensuring-reliability]]
1506 Ensuring reliability
1507 --------------------
1508
1509 [[checking-for-corruption]]
1510 Checking the repository for corruption
1511 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1512
1513 The gitlink:git-fsck[1] command runs a number of self-consistency checks
1514 on the repository, and reports on any problems. This may take some
1515 time. The most common warning by far is about "dangling" objects:
1516
1517 -------------------------------------------------
1518 $ git fsck
1519 dangling commit 7281251ddd2a61e38657c827739c57015671a6b3
1520 dangling commit 2706a059f258c6b245f298dc4ff2ccd30ec21a63
1521 dangling commit 13472b7c4b80851a1bc551779171dcb03655e9b5
1522 dangling blob 218761f9d90712d37a9c5e36f406f92202db07eb
1523 dangling commit bf093535a34a4d35731aa2bd90fe6b176302f14f
1524 dangling commit 8e4bec7f2ddaa268bef999853c25755452100f8e
1525 dangling tree d50bb86186bf27b681d25af89d3b5b68382e4085
1526 dangling tree b24c2473f1fd3d91352a624795be026d64c8841f
1527 ...
1528 -------------------------------------------------
1529
1530 Dangling objects are not a problem. At worst they may take up a little
1531 extra disk space. They can sometimes provide a last-resort method for
1532 recovering lost work--see <<dangling-objects>> for details. However, if
1533 you wish, you can remove them with gitlink:git-prune[1] or the --prune
1534 option to gitlink:git-gc[1]:
1535
1536 -------------------------------------------------
1537 $ git gc --prune
1538 -------------------------------------------------
1539
1540 This may be time-consuming. Unlike most other git operations (including
1541 git-gc when run without any options), it is not safe to prune while
1542 other git operations are in progress in the same repository.
1543
1544 [[recovering-lost-changes]]
1545 Recovering lost changes
1546 ~~~~~~~~~~~~~~~~~~~~~~~
1547
1548 [[reflogs]]
1549 Reflogs
1550 ^^^^^^^
1551
1552 Say you modify a branch with gitlink:git-reset[1] --hard, and then
1553 realize that the branch was the only reference you had to that point in
1554 history.
1555
1556 Fortunately, git also keeps a log, called a "reflog", of all the
1557 previous values of each branch. So in this case you can still find the
1558 old history using, for example,
1559
1560 -------------------------------------------------
1561 $ git log master@{1}
1562 -------------------------------------------------
1563
1564 This lists the commits reachable from the previous version of the head.
1565 This syntax can be used to with any git command that accepts a commit,
1566 not just with git log. Some other examples:
1567
1568 -------------------------------------------------
1569 $ git show master@{2} # See where the branch pointed 2,
1570 $ git show master@{3} # 3, ... changes ago.
1571 $ gitk master@{yesterday} # See where it pointed yesterday,
1572 $ gitk master@{"1 week ago"} # ... or last week
1573 $ git log --walk-reflogs master # show reflog entries for master
1574 -------------------------------------------------
1575
1576 A separate reflog is kept for the HEAD, so
1577
1578 -------------------------------------------------
1579 $ git show HEAD@{"1 week ago"}
1580 -------------------------------------------------
1581
1582 will show what HEAD pointed to one week ago, not what the current branch
1583 pointed to one week ago. This allows you to see the history of what
1584 you've checked out.
1585
1586 The reflogs are kept by default for 30 days, after which they may be
1587 pruned. See gitlink:git-reflog[1] and gitlink:git-gc[1] to learn
1588 how to control this pruning, and see the "SPECIFYING REVISIONS"
1589 section of gitlink:git-rev-parse[1] for details.
1590
1591 Note that the reflog history is very different from normal git history.
1592 While normal history is shared by every repository that works on the
1593 same project, the reflog history is not shared: it tells you only about
1594 how the branches in your local repository have changed over time.
1595
1596 [[dangling-object-recovery]]
1597 Examining dangling objects
1598 ^^^^^^^^^^^^^^^^^^^^^^^^^^
1599
1600 In some situations the reflog may not be able to save you. For example,
1601 suppose you delete a branch, then realize you need the history it
1602 contained. The reflog is also deleted; however, if you have not yet
1603 pruned the repository, then you may still be able to find the lost
1604 commits in the dangling objects that git-fsck reports. See
1605 <<dangling-objects>> for the details.
1606
1607 -------------------------------------------------
1608 $ git fsck
1609 dangling commit 7281251ddd2a61e38657c827739c57015671a6b3
1610 dangling commit 2706a059f258c6b245f298dc4ff2ccd30ec21a63
1611 dangling commit 13472b7c4b80851a1bc551779171dcb03655e9b5
1612 ...
1613 -------------------------------------------------
1614
1615 You can examine
1616 one of those dangling commits with, for example,
1617
1618 ------------------------------------------------
1619 $ gitk 7281251ddd --not --all
1620 ------------------------------------------------
1621
1622 which does what it sounds like: it says that you want to see the commit
1623 history that is described by the dangling commit(s), but not the
1624 history that is described by all your existing branches and tags. Thus
1625 you get exactly the history reachable from that commit that is lost.
1626 (And notice that it might not be just one commit: we only report the
1627 "tip of the line" as being dangling, but there might be a whole deep
1628 and complex commit history that was dropped.)
1629
1630 If you decide you want the history back, you can always create a new
1631 reference pointing to it, for example, a new branch:
1632
1633 ------------------------------------------------
1634 $ git branch recovered-branch 7281251ddd
1635 ------------------------------------------------
1636
1637 Other types of dangling objects (blobs and trees) are also possible, and
1638 dangling objects can arise in other situations.
1639
1640
1641 [[sharing-development]]
1642 Sharing development with others
1643 ===============================
1644
1645 [[getting-updates-with-git-pull]]
1646 Getting updates with git pull
1647 -----------------------------
1648
1649 After you clone a repository and make a few changes of your own, you
1650 may wish to check the original repository for updates and merge them
1651 into your own work.
1652
1653 We have already seen <<Updating-a-repository-with-git-fetch,how to
1654 keep remote tracking branches up to date>> with gitlink:git-fetch[1],
1655 and how to merge two branches. So you can merge in changes from the
1656 original repository's master branch with:
1657
1658 -------------------------------------------------
1659 $ git fetch
1660 $ git merge origin/master
1661 -------------------------------------------------
1662
1663 However, the gitlink:git-pull[1] command provides a way to do this in
1664 one step:
1665
1666 -------------------------------------------------
1667 $ git pull origin master
1668 -------------------------------------------------
1669
1670 In fact, "origin" is normally the default repository to pull from,
1671 and the default branch is normally the HEAD of the remote repository,
1672 so often you can accomplish the above with just
1673
1674 -------------------------------------------------
1675 $ git pull
1676 -------------------------------------------------
1677
1678 See the descriptions of the branch.<name>.remote and branch.<name>.merge
1679 options in gitlink:git-config[1] to learn how to control these defaults
1680 depending on the current branch. Also note that the --track option to
1681 gitlink:git-branch[1] and gitlink:git-checkout[1] can be used to
1682 automatically set the default remote branch to pull from at the time
1683 that a branch is created:
1684
1685 -------------------------------------------------
1686 $ git checkout --track -b maint origin/maint
1687 -------------------------------------------------
1688
1689 In addition to saving you keystrokes, "git pull" also helps you by
1690 producing a default commit message documenting the branch and
1691 repository that you pulled from.
1692
1693 (But note that no such commit will be created in the case of a
1694 <<fast-forwards,fast forward>>; instead, your branch will just be
1695 updated to point to the latest commit from the upstream branch.)
1696
1697 The git-pull command can also be given "." as the "remote" repository,
1698 in which case it just merges in a branch from the current repository; so
1699 the commands
1700
1701 -------------------------------------------------
1702 $ git pull . branch
1703 $ git merge branch
1704 -------------------------------------------------
1705
1706 are roughly equivalent. The former is actually very commonly used.
1707
1708 [[submitting-patches]]
1709 Submitting patches to a project
1710 -------------------------------
1711
1712 If you just have a few changes, the simplest way to submit them may
1713 just be to send them as patches in email:
1714
1715 First, use gitlink:git-format-patch[1]; for example:
1716
1717 -------------------------------------------------
1718 $ git format-patch origin
1719 -------------------------------------------------
1720
1721 will produce a numbered series of files in the current directory, one
1722 for each patch in the current branch but not in origin/HEAD.
1723
1724 You can then import these into your mail client and send them by
1725 hand. However, if you have a lot to send at once, you may prefer to
1726 use the gitlink:git-send-email[1] script to automate the process.
1727 Consult the mailing list for your project first to determine how they
1728 prefer such patches be handled.
1729
1730 [[importing-patches]]
1731 Importing patches to a project
1732 ------------------------------
1733
1734 Git also provides a tool called gitlink:git-am[1] (am stands for
1735 "apply mailbox"), for importing such an emailed series of patches.
1736 Just save all of the patch-containing messages, in order, into a
1737 single mailbox file, say "patches.mbox", then run
1738
1739 -------------------------------------------------
1740 $ git am -3 patches.mbox
1741 -------------------------------------------------
1742
1743 Git will apply each patch in order; if any conflicts are found, it
1744 will stop, and you can fix the conflicts as described in
1745 "<<resolving-a-merge,Resolving a merge>>". (The "-3" option tells
1746 git to perform a merge; if you would prefer it just to abort and
1747 leave your tree and index untouched, you may omit that option.)
1748
1749 Once the index is updated with the results of the conflict
1750 resolution, instead of creating a new commit, just run
1751
1752 -------------------------------------------------
1753 $ git am --resolved
1754 -------------------------------------------------
1755
1756 and git will create the commit for you and continue applying the
1757 remaining patches from the mailbox.
1758
1759 The final result will be a series of commits, one for each patch in
1760 the original mailbox, with authorship and commit log message each
1761 taken from the message containing each patch.
1762
1763 [[public-repositories]]
1764 Public git repositories
1765 -----------------------
1766
1767 Another way to submit changes to a project is to tell the maintainer of
1768 that project to pull the changes from your repository using git-pull[1].
1769 In the section "<<getting-updates-with-git-pull, Getting updates with
1770 git pull>>" we described this as a way to get updates from the "main"
1771 repository, but it works just as well in the other direction.
1772
1773 If you and the maintainer both have accounts on the same machine, then
1774 you can just pull changes from each other's repositories directly;
1775 commands that accept repository URLs as arguments will also accept a
1776 local directory name:
1777
1778 -------------------------------------------------
1779 $ git clone /path/to/repository
1780 $ git pull /path/to/other/repository
1781 -------------------------------------------------
1782
1783 or an ssh url:
1784
1785 -------------------------------------------------
1786 $ git clone ssh://yourhost/~you/repository
1787 -------------------------------------------------
1788
1789 For projects with few developers, or for synchronizing a few private
1790 repositories, this may be all you need.
1791
1792 However, the more common way to do this is to maintain a separate public
1793 repository (usually on a different host) for others to pull changes
1794 from. This is usually more convenient, and allows you to cleanly
1795 separate private work in progress from publicly visible work.
1796
1797 You will continue to do your day-to-day work in your personal
1798 repository, but periodically "push" changes from your personal
1799 repository into your public repository, allowing other developers to
1800 pull from that repository. So the flow of changes, in a situation
1801 where there is one other developer with a public repository, looks
1802 like this:
1803
1804 you push
1805 your personal repo ------------------> your public repo
1806 ^ |
1807 | |
1808 | you pull | they pull
1809 | |
1810 | |
1811 | they push V
1812 their public repo <------------------- their repo
1813
1814 We explain how to do this in the following sections.
1815
1816 [[setting-up-a-public-repository]]
1817 Setting up a public repository
1818 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1819
1820 Assume your personal repository is in the directory ~/proj. We
1821 first create a new clone of the repository and tell git-daemon that it
1822 is meant to be public:
1823
1824 -------------------------------------------------
1825 $ git clone --bare ~/proj proj.git
1826 $ touch proj.git/git-daemon-export-ok
1827 -------------------------------------------------
1828
1829 The resulting directory proj.git contains a "bare" git repository--it is
1830 just the contents of the ".git" directory, without any files checked out
1831 around it.
1832
1833 Next, copy proj.git to the server where you plan to host the
1834 public repository. You can use scp, rsync, or whatever is most
1835 convenient.
1836
1837 [[exporting-via-git]]
1838 Exporting a git repository via the git protocol
1839 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1840
1841 This is the preferred method.
1842
1843 If someone else administers the server, they should tell you what
1844 directory to put the repository in, and what git:// url it will appear
1845 at. You can then skip to the section
1846 "<<pushing-changes-to-a-public-repository,Pushing changes to a public
1847 repository>>", below.
1848
1849 Otherwise, all you need to do is start gitlink:git-daemon[1]; it will
1850 listen on port 9418. By default, it will allow access to any directory
1851 that looks like a git directory and contains the magic file
1852 git-daemon-export-ok. Passing some directory paths as git-daemon
1853 arguments will further restrict the exports to those paths.
1854
1855 You can also run git-daemon as an inetd service; see the
1856 gitlink:git-daemon[1] man page for details. (See especially the
1857 examples section.)
1858
1859 [[exporting-via-http]]
1860 Exporting a git repository via http
1861 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1862
1863 The git protocol gives better performance and reliability, but on a
1864 host with a web server set up, http exports may be simpler to set up.
1865
1866 All you need to do is place the newly created bare git repository in
1867 a directory that is exported by the web server, and make some
1868 adjustments to give web clients some extra information they need:
1869
1870 -------------------------------------------------
1871 $ mv proj.git /home/you/public_html/proj.git
1872 $ cd proj.git
1873 $ git --bare update-server-info
1874 $ chmod a+x hooks/post-update
1875 -------------------------------------------------
1876
1877 (For an explanation of the last two lines, see
1878 gitlink:git-update-server-info[1], and the documentation
1879 link:hooks.html[Hooks used by git].)
1880
1881 Advertise the url of proj.git. Anybody else should then be able to
1882 clone or pull from that url, for example with a commandline like:
1883
1884 -------------------------------------------------
1885 $ git clone http://yourserver.com/~you/proj.git
1886 -------------------------------------------------
1887
1888 (See also
1889 link:howto/setup-git-server-over-http.txt[setup-git-server-over-http]
1890 for a slightly more sophisticated setup using WebDAV which also
1891 allows pushing over http.)
1892
1893 [[pushing-changes-to-a-public-repository]]
1894 Pushing changes to a public repository
1895 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1896
1897 Note that the two techniques outlined above (exporting via
1898 <<exporting-via-http,http>> or <<exporting-via-git,git>>) allow other
1899 maintainers to fetch your latest changes, but they do not allow write
1900 access, which you will need to update the public repository with the
1901 latest changes created in your private repository.
1902
1903 The simplest way to do this is using gitlink:git-push[1] and ssh; to
1904 update the remote branch named "master" with the latest state of your
1905 branch named "master", run
1906
1907 -------------------------------------------------
1908 $ git push ssh://yourserver.com/~you/proj.git master:master
1909 -------------------------------------------------
1910
1911 or just
1912
1913 -------------------------------------------------
1914 $ git push ssh://yourserver.com/~you/proj.git master
1915 -------------------------------------------------
1916
1917 As with git-fetch, git-push will complain if this does not result in
1918 a <<fast-forwards,fast forward>>. Normally this is a sign of
1919 something wrong. However, if you are sure you know what you're
1920 doing, you may force git-push to perform the update anyway by
1921 proceeding the branch name by a plus sign:
1922
1923 -------------------------------------------------
1924 $ git push ssh://yourserver.com/~you/proj.git +master
1925 -------------------------------------------------
1926
1927 Note that the target of a "push" is normally a
1928 <<def_bare_repository,bare>> repository. You can also push to a
1929 repository that has a checked-out working tree, but the working tree
1930 will not be updated by the push. This may lead to unexpected results if
1931 the branch you push to is the currently checked-out branch!
1932
1933 As with git-fetch, you may also set up configuration options to
1934 save typing; so, for example, after
1935
1936 -------------------------------------------------
1937 $ cat >>.git/config <<EOF
1938 [remote "public-repo"]
1939 url = ssh://yourserver.com/~you/proj.git
1940 EOF
1941 -------------------------------------------------
1942
1943 you should be able to perform the above push with just
1944
1945 -------------------------------------------------
1946 $ git push public-repo master
1947 -------------------------------------------------
1948
1949 See the explanations of the remote.<name>.url, branch.<name>.remote,
1950 and remote.<name>.push options in gitlink:git-config[1] for
1951 details.
1952
1953 [[setting-up-a-shared-repository]]
1954 Setting up a shared repository
1955 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1956
1957 Another way to collaborate is by using a model similar to that
1958 commonly used in CVS, where several developers with special rights
1959 all push to and pull from a single shared repository. See
1960 link:cvs-migration.html[git for CVS users] for instructions on how to
1961 set this up.
1962
1963 However, while there is nothing wrong with git's support for shared
1964 repositories, this mode of operation is not generally recommended,
1965 simply because the mode of collaboration that git supports--by
1966 exchanging patches and pulling from public repositories--has so many
1967 advantages over the central shared repository:
1968
1969 - Git's ability to quickly import and merge patches allows a
1970 single maintainer to process incoming changes even at very
1971 high rates. And when that becomes too much, git-pull provides
1972 an easy way for that maintainer to delegate this job to other
1973 maintainers while still allowing optional review of incoming
1974 changes.
1975 - Since every developer's repository has the same complete copy
1976 of the project history, no repository is special, and it is
1977 trivial for another developer to take over maintenance of a
1978 project, either by mutual agreement, or because a maintainer
1979 becomes unresponsive or difficult to work with.
1980 - The lack of a central group of "committers" means there is
1981 less need for formal decisions about who is "in" and who is
1982 "out".
1983
1984 [[setting-up-gitweb]]
1985 Allowing web browsing of a repository
1986 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1987
1988 The gitweb cgi script provides users an easy way to browse your
1989 project's files and history without having to install git; see the file
1990 gitweb/INSTALL in the git source tree for instructions on setting it up.
1991
1992 [[sharing-development-examples]]
1993 Examples
1994 --------
1995
1996 [[maintaining-topic-branches]]
1997 Maintaining topic branches for a Linux subsystem maintainer
1998 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1999
2000 This describes how Tony Luck uses git in his role as maintainer of the
2001 IA64 architecture for the Linux kernel.
2002
2003 He uses two public branches:
2004
2005 - A "test" tree into which patches are initially placed so that they
2006 can get some exposure when integrated with other ongoing development.
2007 This tree is available to Andrew for pulling into -mm whenever he
2008 wants.
2009
2010 - A "release" tree into which tested patches are moved for final sanity
2011 checking, and as a vehicle to send them upstream to Linus (by sending
2012 him a "please pull" request.)
2013
2014 He also uses a set of temporary branches ("topic branches"), each
2015 containing a logical grouping of patches.
2016
2017 To set this up, first create your work tree by cloning Linus's public
2018 tree:
2019
2020 -------------------------------------------------
2021 $ git clone git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6.git work
2022 $ cd work
2023 -------------------------------------------------
2024
2025 Linus's tree will be stored in the remote branch named origin/master,
2026 and can be updated using gitlink:git-fetch[1]; you can track other
2027 public trees using gitlink:git-remote[1] to set up a "remote" and
2028 git-fetch[1] to keep them up-to-date; see <<repositories-and-branches>>.
2029
2030 Now create the branches in which you are going to work; these start out
2031 at the current tip of origin/master branch, and should be set up (using
2032 the --track option to gitlink:git-branch[1]) to merge changes in from
2033 Linus by default.
2034
2035 -------------------------------------------------
2036 $ git branch --track test origin/master
2037 $ git branch --track release origin/master
2038 -------------------------------------------------
2039
2040 These can be easily kept up to date using gitlink:git-pull[1]
2041
2042 -------------------------------------------------
2043 $ git checkout test && git pull
2044 $ git checkout release && git pull
2045 -------------------------------------------------
2046
2047 Important note! If you have any local changes in these branches, then
2048 this merge will create a commit object in the history (with no local
2049 changes git will simply do a "Fast forward" merge). Many people dislike
2050 the "noise" that this creates in the Linux history, so you should avoid
2051 doing this capriciously in the "release" branch, as these noisy commits
2052 will become part of the permanent history when you ask Linus to pull
2053 from the release branch.
2054
2055 A few configuration variables (see gitlink:git-config[1]) can
2056 make it easy to push both branches to your public tree. (See
2057 <<setting-up-a-public-repository>>.)
2058
2059 -------------------------------------------------
2060 $ cat >> .git/config <<EOF
2061 [remote "mytree"]
2062 url = master.kernel.org:/pub/scm/linux/kernel/git/aegl/linux-2.6.git
2063 push = release
2064 push = test
2065 EOF
2066 -------------------------------------------------
2067
2068 Then you can push both the test and release trees using
2069 gitlink:git-push[1]:
2070
2071 -------------------------------------------------
2072 $ git push mytree
2073 -------------------------------------------------
2074
2075 or push just one of the test and release branches using:
2076
2077 -------------------------------------------------
2078 $ git push mytree test
2079 -------------------------------------------------
2080
2081 or
2082
2083 -------------------------------------------------
2084 $ git push mytree release
2085 -------------------------------------------------
2086
2087 Now to apply some patches from the community. Think of a short
2088 snappy name for a branch to hold this patch (or related group of
2089 patches), and create a new branch from the current tip of Linus's
2090 branch:
2091
2092 -------------------------------------------------
2093 $ git checkout -b speed-up-spinlocks origin
2094 -------------------------------------------------
2095
2096 Now you apply the patch(es), run some tests, and commit the change(s). If
2097 the patch is a multi-part series, then you should apply each as a separate
2098 commit to this branch.
2099
2100 -------------------------------------------------
2101 $ ... patch ... test ... commit [ ... patch ... test ... commit ]*
2102 -------------------------------------------------
2103
2104 When you are happy with the state of this change, you can pull it into the
2105 "test" branch in preparation to make it public:
2106
2107 -------------------------------------------------
2108 $ git checkout test && git pull . speed-up-spinlocks
2109 -------------------------------------------------
2110
2111 It is unlikely that you would have any conflicts here ... but you might if you
2112 spent a while on this step and had also pulled new versions from upstream.
2113
2114 Some time later when enough time has passed and testing done, you can pull the
2115 same branch into the "release" tree ready to go upstream. This is where you
2116 see the value of keeping each patch (or patch series) in its own branch. It
2117 means that the patches can be moved into the "release" tree in any order.
2118
2119 -------------------------------------------------
2120 $ git checkout release && git pull . speed-up-spinlocks
2121 -------------------------------------------------
2122
2123 After a while, you will have a number of branches, and despite the
2124 well chosen names you picked for each of them, you may forget what
2125 they are for, or what status they are in. To get a reminder of what
2126 changes are in a specific branch, use:
2127
2128 -------------------------------------------------
2129 $ git log linux..branchname | git-shortlog
2130 -------------------------------------------------
2131
2132 To see whether it has already been merged into the test or release branches
2133 use:
2134
2135 -------------------------------------------------
2136 $ git log test..branchname
2137 -------------------------------------------------
2138
2139 or
2140
2141 -------------------------------------------------
2142 $ git log release..branchname
2143 -------------------------------------------------
2144
2145 (If this branch has not yet been merged you will see some log entries.
2146 If it has been merged, then there will be no output.)
2147
2148 Once a patch completes the great cycle (moving from test to release,
2149 then pulled by Linus, and finally coming back into your local
2150 "origin/master" branch) the branch for this change is no longer needed.
2151 You detect this when the output from:
2152
2153 -------------------------------------------------
2154 $ git log origin..branchname
2155 -------------------------------------------------
2156
2157 is empty. At this point the branch can be deleted:
2158
2159 -------------------------------------------------
2160 $ git branch -d branchname
2161 -------------------------------------------------
2162
2163 Some changes are so trivial that it is not necessary to create a separate
2164 branch and then merge into each of the test and release branches. For
2165 these changes, just apply directly to the "release" branch, and then
2166 merge that into the "test" branch.
2167
2168 To create diffstat and shortlog summaries of changes to include in a "please
2169 pull" request to Linus you can use:
2170
2171 -------------------------------------------------
2172 $ git diff --stat origin..release
2173 -------------------------------------------------
2174
2175 and
2176
2177 -------------------------------------------------
2178 $ git log -p origin..release | git shortlog
2179 -------------------------------------------------
2180
2181 Here are some of the scripts that simplify all this even further.
2182
2183 -------------------------------------------------
2184 ==== update script ====
2185 # Update a branch in my GIT tree. If the branch to be updated
2186 # is origin, then pull from kernel.org. Otherwise merge
2187 # origin/master branch into test|release branch
2188
2189 case "$1" in
2190 test|release)
2191 git checkout $1 && git pull . origin
2192 ;;
2193 origin)
2194 before=$(cat .git/refs/remotes/origin/master)
2195 git fetch origin
2196 after=$(cat .git/refs/remotes/origin/master)
2197 if [ $before != $after ]
2198 then
2199 git log $before..$after | git shortlog
2200 fi
2201 ;;
2202 *)
2203 echo "Usage: $0 origin|test|release" 1>&2
2204 exit 1
2205 ;;
2206 esac
2207 -------------------------------------------------
2208
2209 -------------------------------------------------
2210 ==== merge script ====
2211 # Merge a branch into either the test or release branch
2212
2213 pname=$0
2214
2215 usage()
2216 {
2217 echo "Usage: $pname branch test|release" 1>&2
2218 exit 1
2219 }
2220
2221 if [ ! -f .git/refs/heads/"$1" ]
2222 then
2223 echo "Can't see branch <$1>" 1>&2
2224 usage
2225 fi
2226
2227 case "$2" in
2228 test|release)
2229 if [ $(git log $2..$1 | wc -c) -eq 0 ]
2230 then
2231 echo $1 already merged into $2 1>&2
2232 exit 1
2233 fi
2234 git checkout $2 && git pull . $1
2235 ;;
2236 *)
2237 usage
2238 ;;
2239 esac
2240 -------------------------------------------------
2241
2242 -------------------------------------------------
2243 ==== status script ====
2244 # report on status of my ia64 GIT tree
2245
2246 gb=$(tput setab 2)
2247 rb=$(tput setab 1)
2248 restore=$(tput setab 9)
2249
2250 if [ `git rev-list test..release | wc -c` -gt 0 ]
2251 then
2252 echo $rb Warning: commits in release that are not in test $restore
2253 git log test..release
2254 fi
2255
2256 for branch in `ls .git/refs/heads`
2257 do
2258 if [ $branch = test -o $branch = release ]
2259 then
2260 continue
2261 fi
2262
2263 echo -n $gb ======= $branch ====== $restore " "
2264 status=
2265 for ref in test release origin/master
2266 do
2267 if [ `git rev-list $ref..$branch | wc -c` -gt 0 ]
2268 then
2269 status=$status${ref:0:1}
2270 fi
2271 done
2272 case $status in
2273 trl)
2274 echo $rb Need to pull into test $restore
2275 ;;
2276 rl)
2277 echo "In test"
2278 ;;
2279 l)
2280 echo "Waiting for linus"
2281 ;;
2282 "")
2283 echo $rb All done $restore
2284 ;;
2285 *)
2286 echo $rb "<$status>" $restore
2287 ;;
2288 esac
2289 git log origin/master..$branch | git shortlog
2290 done
2291 -------------------------------------------------
2292
2293
2294 [[cleaning-up-history]]
2295 Rewriting history and maintaining patch series
2296 ==============================================
2297
2298 Normally commits are only added to a project, never taken away or
2299 replaced. Git is designed with this assumption, and violating it will
2300 cause git's merge machinery (for example) to do the wrong thing.
2301
2302 However, there is a situation in which it can be useful to violate this
2303 assumption.
2304
2305 [[patch-series]]
2306 Creating the perfect patch series
2307 ---------------------------------
2308
2309 Suppose you are a contributor to a large project, and you want to add a
2310 complicated feature, and to present it to the other developers in a way
2311 that makes it easy for them to read your changes, verify that they are
2312 correct, and understand why you made each change.
2313
2314 If you present all of your changes as a single patch (or commit), they
2315 may find that it is too much to digest all at once.
2316
2317 If you present them with the entire history of your work, complete with
2318 mistakes, corrections, and dead ends, they may be overwhelmed.
2319
2320 So the ideal is usually to produce a series of patches such that:
2321
2322 1. Each patch can be applied in order.
2323
2324 2. Each patch includes a single logical change, together with a
2325 message explaining the change.
2326
2327 3. No patch introduces a regression: after applying any initial
2328 part of the series, the resulting project still compiles and
2329 works, and has no bugs that it didn't have before.
2330
2331 4. The complete series produces the same end result as your own
2332 (probably much messier!) development process did.
2333
2334 We will introduce some tools that can help you do this, explain how to
2335 use them, and then explain some of the problems that can arise because
2336 you are rewriting history.
2337
2338 [[using-git-rebase]]
2339 Keeping a patch series up to date using git-rebase
2340 --------------------------------------------------
2341
2342 Suppose that you create a branch "mywork" on a remote-tracking branch
2343 "origin", and create some commits on top of it:
2344
2345 -------------------------------------------------
2346 $ git checkout -b mywork origin
2347 $ vi file.txt
2348 $ git commit
2349 $ vi otherfile.txt
2350 $ git commit
2351 ...
2352 -------------------------------------------------
2353
2354 You have performed no merges into mywork, so it is just a simple linear
2355 sequence of patches on top of "origin":
2356
2357 ................................................
2358 o--o--o <-- origin
2359 \
2360 o--o--o <-- mywork
2361 ................................................
2362
2363 Some more interesting work has been done in the upstream project, and
2364 "origin" has advanced:
2365
2366 ................................................
2367 o--o--O--o--o--o <-- origin
2368 \
2369 a--b--c <-- mywork
2370 ................................................
2371
2372 At this point, you could use "pull" to merge your changes back in;
2373 the result would create a new merge commit, like this:
2374
2375 ................................................
2376 o--o--O--o--o--o <-- origin
2377 \ \
2378 a--b--c--m <-- mywork
2379 ................................................
2380
2381 However, if you prefer to keep the history in mywork a simple series of
2382 commits without any merges, you may instead choose to use
2383 gitlink:git-rebase[1]:
2384
2385 -------------------------------------------------
2386 $ git checkout mywork
2387 $ git rebase origin
2388 -------------------------------------------------
2389
2390 This will remove each of your commits from mywork, temporarily saving
2391 them as patches (in a directory named ".dotest"), update mywork to
2392 point at the latest version of origin, then apply each of the saved
2393 patches to the new mywork. The result will look like:
2394
2395
2396 ................................................
2397 o--o--O--o--o--o <-- origin
2398 \
2399 a'--b'--c' <-- mywork
2400 ................................................
2401
2402 In the process, it may discover conflicts. In that case it will stop
2403 and allow you to fix the conflicts; after fixing conflicts, use "git
2404 add" to update the index with those contents, and then, instead of
2405 running git-commit, just run
2406
2407 -------------------------------------------------
2408 $ git rebase --continue
2409 -------------------------------------------------
2410
2411 and git will continue applying the rest of the patches.
2412
2413 At any point you may use the --abort option to abort this process and
2414 return mywork to the state it had before you started the rebase:
2415
2416 -------------------------------------------------
2417 $ git rebase --abort
2418 -------------------------------------------------
2419
2420 [[modifying-one-commit]]
2421 Modifying a single commit
2422 -------------------------
2423
2424 We saw in <<fixing-a-mistake-by-editing-history>> that you can replace the
2425 most recent commit using
2426
2427 -------------------------------------------------
2428 $ git commit --amend
2429 -------------------------------------------------
2430
2431 which will replace the old commit by a new commit incorporating your
2432 changes, giving you a chance to edit the old commit message first.
2433
2434 You can also use a combination of this and gitlink:git-rebase[1] to edit
2435 commits further back in your history. First, tag the problematic commit with
2436
2437 -------------------------------------------------
2438 $ git tag bad mywork~5
2439 -------------------------------------------------
2440
2441 (Either gitk or git-log may be useful for finding the commit.)
2442
2443 Then check out that commit, edit it, and rebase the rest of the series
2444 on top of it (note that we could check out the commit on a temporary
2445 branch, but instead we're using a <<detached-head,detached head>>):
2446
2447 -------------------------------------------------
2448 $ git checkout bad
2449 $ # make changes here and update the index
2450 $ git commit --amend
2451 $ git rebase --onto HEAD bad mywork
2452 -------------------------------------------------
2453
2454 When you're done, you'll be left with mywork checked out, with the top
2455 patches on mywork reapplied on top of your modified commit. You can
2456 then clean up with
2457
2458 -------------------------------------------------
2459 $ git tag -d bad
2460 -------------------------------------------------
2461
2462 Note that the immutable nature of git history means that you haven't really
2463 "modified" existing commits; instead, you have replaced the old commits with
2464 new commits having new object names.
2465
2466 [[reordering-patch-series]]
2467 Reordering or selecting from a patch series
2468 -------------------------------------------
2469
2470 Given one existing commit, the gitlink:git-cherry-pick[1] command
2471 allows you to apply the change introduced by that commit and create a
2472 new commit that records it. So, for example, if "mywork" points to a
2473 series of patches on top of "origin", you might do something like:
2474
2475 -------------------------------------------------
2476 $ git checkout -b mywork-new origin
2477 $ gitk origin..mywork &
2478 -------------------------------------------------
2479
2480 And browse through the list of patches in the mywork branch using gitk,
2481 applying them (possibly in a different order) to mywork-new using
2482 cherry-pick, and possibly modifying them as you go using commit
2483 --amend.
2484
2485 Another technique is to use git-format-patch to create a series of
2486 patches, then reset the state to before the patches:
2487
2488 -------------------------------------------------
2489 $ git format-patch origin
2490 $ git reset --hard origin
2491 -------------------------------------------------
2492
2493 Then modify, reorder, or eliminate patches as preferred before applying
2494 them again with gitlink:git-am[1].
2495
2496 [[patch-series-tools]]
2497 Other tools
2498 -----------
2499
2500 There are numerous other tools, such as stgit, which exist for the
2501 purpose of maintaining a patch series. These are outside of the scope of
2502 this manual.
2503
2504 [[problems-with-rewriting-history]]
2505 Problems with rewriting history
2506 -------------------------------
2507
2508 The primary problem with rewriting the history of a branch has to do
2509 with merging. Suppose somebody fetches your branch and merges it into
2510 their branch, with a result something like this:
2511
2512 ................................................
2513 o--o--O--o--o--o <-- origin
2514 \ \
2515 t--t--t--m <-- their branch:
2516 ................................................
2517
2518 Then suppose you modify the last three commits:
2519
2520 ................................................
2521 o--o--o <-- new head of origin
2522 /
2523 o--o--O--o--o--o <-- old head of origin
2524 ................................................
2525
2526 If we examined all this history together in one repository, it will
2527 look like:
2528
2529 ................................................
2530 o--o--o <-- new head of origin
2531 /
2532 o--o--O--o--o--o <-- old head of origin
2533 \ \
2534 t--t--t--m <-- their branch:
2535 ................................................
2536
2537 Git has no way of knowing that the new head is an updated version of
2538 the old head; it treats this situation exactly the same as it would if
2539 two developers had independently done the work on the old and new heads
2540 in parallel. At this point, if someone attempts to merge the new head
2541 in to their branch, git will attempt to merge together the two (old and
2542 new) lines of development, instead of trying to replace the old by the
2543 new. The results are likely to be unexpected.
2544
2545 You may still choose to publish branches whose history is rewritten,
2546 and it may be useful for others to be able to fetch those branches in
2547 order to examine or test them, but they should not attempt to pull such
2548 branches into their own work.
2549
2550 For true distributed development that supports proper merging,
2551 published branches should never be rewritten.
2552
2553 [[advanced-branch-management]]
2554 Advanced branch management
2555 ==========================
2556
2557 [[fetching-individual-branches]]
2558 Fetching individual branches
2559 ----------------------------
2560
2561 Instead of using gitlink:git-remote[1], you can also choose just
2562 to update one branch at a time, and to store it locally under an
2563 arbitrary name:
2564
2565 -------------------------------------------------
2566 $ git fetch origin todo:my-todo-work
2567 -------------------------------------------------
2568
2569 The first argument, "origin", just tells git to fetch from the
2570 repository you originally cloned from. The second argument tells git
2571 to fetch the branch named "todo" from the remote repository, and to
2572 store it locally under the name refs/heads/my-todo-work.
2573
2574 You can also fetch branches from other repositories; so
2575
2576 -------------------------------------------------
2577 $ git fetch git://example.com/proj.git master:example-master
2578 -------------------------------------------------
2579
2580 will create a new branch named "example-master" and store in it the
2581 branch named "master" from the repository at the given URL. If you
2582 already have a branch named example-master, it will attempt to
2583 <<fast-forwards,fast-forward>> to the commit given by example.com's
2584 master branch. In more detail:
2585
2586 [[fetch-fast-forwards]]
2587 git fetch and fast-forwards
2588 ---------------------------
2589
2590 In the previous example, when updating an existing branch, "git
2591 fetch" checks to make sure that the most recent commit on the remote
2592 branch is a descendant of the most recent commit on your copy of the
2593 branch before updating your copy of the branch to point at the new
2594 commit. Git calls this process a <<fast-forwards,fast forward>>.
2595
2596 A fast forward looks something like this:
2597
2598 ................................................
2599 o--o--o--o <-- old head of the branch
2600 \
2601 o--o--o <-- new head of the branch
2602 ................................................
2603
2604
2605 In some cases it is possible that the new head will *not* actually be
2606 a descendant of the old head. For example, the developer may have
2607 realized she made a serious mistake, and decided to backtrack,
2608 resulting in a situation like:
2609
2610 ................................................
2611 o--o--o--o--a--b <-- old head of the branch
2612 \
2613 o--o--o <-- new head of the branch
2614 ................................................
2615
2616 In this case, "git fetch" will fail, and print out a warning.
2617
2618 In that case, you can still force git to update to the new head, as
2619 described in the following section. However, note that in the
2620 situation above this may mean losing the commits labeled "a" and "b",
2621 unless you've already created a reference of your own pointing to
2622 them.
2623
2624 [[forcing-fetch]]
2625 Forcing git fetch to do non-fast-forward updates
2626 ------------------------------------------------
2627
2628 If git fetch fails because the new head of a branch is not a
2629 descendant of the old head, you may force the update with:
2630
2631 -------------------------------------------------
2632 $ git fetch git://example.com/proj.git +master:refs/remotes/example/master
2633 -------------------------------------------------
2634
2635 Note the addition of the "+" sign. Alternatively, you can use the "-f"
2636 flag to force updates of all the fetched branches, as in:
2637
2638 -------------------------------------------------
2639 $ git fetch -f origin
2640 -------------------------------------------------
2641
2642 Be aware that commits that the old version of example/master pointed at
2643 may be lost, as we saw in the previous section.
2644
2645 [[remote-branch-configuration]]
2646 Configuring remote branches
2647 ---------------------------
2648
2649 We saw above that "origin" is just a shortcut to refer to the
2650 repository that you originally cloned from. This information is
2651 stored in git configuration variables, which you can see using
2652 gitlink:git-config[1]:
2653
2654 -------------------------------------------------
2655 $ git config -l
2656 core.repositoryformatversion=0
2657 core.filemode=true
2658 core.logallrefupdates=true
2659 remote.origin.url=git://git.kernel.org/pub/scm/git/git.git
2660 remote.origin.fetch=+refs/heads/*:refs/remotes/origin/*
2661 branch.master.remote=origin
2662 branch.master.merge=refs/heads/master
2663 -------------------------------------------------
2664
2665 If there are other repositories that you also use frequently, you can
2666 create similar configuration options to save typing; for example,
2667 after
2668
2669 -------------------------------------------------
2670 $ git config remote.example.url git://example.com/proj.git
2671 -------------------------------------------------
2672
2673 then the following two commands will do the same thing:
2674
2675 -------------------------------------------------
2676 $ git fetch git://example.com/proj.git master:refs/remotes/example/master
2677 $ git fetch example master:refs/remotes/example/master
2678 -------------------------------------------------
2679
2680 Even better, if you add one more option:
2681
2682 -------------------------------------------------
2683 $ git config remote.example.fetch master:refs/remotes/example/master
2684 -------------------------------------------------
2685
2686 then the following commands will all do the same thing:
2687
2688 -------------------------------------------------
2689 $ git fetch git://example.com/proj.git master:refs/remotes/example/master
2690 $ git fetch example master:refs/remotes/example/master
2691 $ git fetch example
2692 -------------------------------------------------
2693
2694 You can also add a "+" to force the update each time:
2695
2696 -------------------------------------------------
2697 $ git config remote.example.fetch +master:ref/remotes/example/master
2698 -------------------------------------------------
2699
2700 Don't do this unless you're sure you won't mind "git fetch" possibly
2701 throwing away commits on mybranch.
2702
2703 Also note that all of the above configuration can be performed by
2704 directly editing the file .git/config instead of using
2705 gitlink:git-config[1].
2706
2707 See gitlink:git-config[1] for more details on the configuration
2708 options mentioned above.
2709
2710
2711 [[git-internals]]
2712 Git internals
2713 =============
2714
2715 Git depends on two fundamental abstractions: the "object database", and
2716 the "current directory cache" aka "index".
2717
2718 [[the-object-database]]
2719 The Object Database
2720 -------------------
2721
2722 The object database is literally just a content-addressable collection
2723 of objects. All objects are named by their content, which is
2724 approximated by the SHA1 hash of the object itself. Objects may refer
2725 to other objects (by referencing their SHA1 hash), and so you can
2726 build up a hierarchy of objects.
2727
2728 All objects have a statically determined "type" which is
2729 determined at object creation time, and which identifies the format of
2730 the object (i.e. how it is used, and how it can refer to other
2731 objects). There are currently four different object types: "blob",
2732 "tree", "commit", and "tag".
2733
2734 A <<def_blob_object,"blob" object>> cannot refer to any other object,
2735 and is, as the name implies, a pure storage object containing some
2736 user data. It is used to actually store the file data, i.e. a blob
2737 object is associated with some particular version of some file.
2738
2739 A <<def_tree_object,"tree" object>> is an object that ties one or more
2740 "blob" objects into a directory structure. In addition, a tree object
2741 can refer to other tree objects, thus creating a directory hierarchy.
2742
2743 A <<def_commit_object,"commit" object>> ties such directory hierarchies
2744 together into a <<def_DAG,directed acyclic graph>> of revisions - each
2745 "commit" is associated with exactly one tree (the directory hierarchy at
2746 the time of the commit). In addition, a "commit" refers to one or more
2747 "parent" commit objects that describe the history of how we arrived at
2748 that directory hierarchy.
2749
2750 As a special case, a commit object with no parents is called the "root"
2751 commit, and is the point of an initial project commit. Each project
2752 must have at least one root, and while you can tie several different
2753 root objects together into one project by creating a commit object which
2754 has two or more separate roots as its ultimate parents, that's probably
2755 just going to confuse people. So aim for the notion of "one root object
2756 per project", even if git itself does not enforce that.
2757
2758 A <<def_tag_object,"tag" object>> symbolically identifies and can be
2759 used to sign other objects. It contains the identifier and type of
2760 another object, a symbolic name (of course!) and, optionally, a
2761 signature.
2762
2763 Regardless of object type, all objects share the following
2764 characteristics: they are all deflated with zlib, and have a header
2765 that not only specifies their type, but also provides size information
2766 about the data in the object. It's worth noting that the SHA1 hash
2767 that is used to name the object is the hash of the original data
2768 plus this header, so `sha1sum` 'file' does not match the object name
2769 for 'file'.
2770 (Historical note: in the dawn of the age of git the hash
2771 was the sha1 of the 'compressed' object.)
2772
2773 As a result, the general consistency of an object can always be tested
2774 independently of the contents or the type of the object: all objects can
2775 be validated by verifying that (a) their hashes match the content of the
2776 file and (b) the object successfully inflates to a stream of bytes that
2777 forms a sequence of <ascii type without space> {plus} <space> {plus} <ascii decimal
2778 size> {plus} <byte\0> {plus} <binary object data>.
2779
2780 The structured objects can further have their structure and
2781 connectivity to other objects verified. This is generally done with
2782 the `git-fsck` program, which generates a full dependency graph
2783 of all objects, and verifies their internal consistency (in addition
2784 to just verifying their superficial consistency through the hash).
2785
2786 The object types in some more detail:
2787
2788 [[blob-object]]
2789 Blob Object
2790 -----------
2791
2792 A "blob" object is nothing but a binary blob of data, and doesn't
2793 refer to anything else. There is no signature or any other
2794 verification of the data, so while the object is consistent (it 'is'
2795 indexed by its sha1 hash, so the data itself is certainly correct), it
2796 has absolutely no other attributes. No name associations, no
2797 permissions. It is purely a blob of data (i.e. normally "file
2798 contents").
2799
2800 In particular, since the blob is entirely defined by its data, if two
2801 files in a directory tree (or in multiple different versions of the
2802 repository) have the same contents, they will share the same blob
2803 object. The object is totally independent of its location in the
2804 directory tree, and renaming a file does not change the object that
2805 file is associated with in any way.
2806
2807 A blob is typically created when gitlink:git-update-index[1]
2808 is run, and its data can be accessed by gitlink:git-cat-file[1].
2809
2810 [[tree-object]]
2811 Tree Object
2812 -----------
2813
2814 The next hierarchical object type is the "tree" object. A tree object
2815 is a list of mode/name/blob data, sorted by name. Alternatively, the
2816 mode data may specify a directory mode, in which case instead of
2817 naming a blob, that name is associated with another TREE object.
2818
2819 Like the "blob" object, a tree object is uniquely determined by the
2820 set contents, and so two separate but identical trees will always
2821 share the exact same object. This is true at all levels, i.e. it's
2822 true for a "leaf" tree (which does not refer to any other trees, only
2823 blobs) as well as for a whole subdirectory.
2824
2825 For that reason a "tree" object is just a pure data abstraction: it
2826 has no history, no signatures, no verification of validity, except
2827 that since the contents are again protected by the hash itself, we can
2828 trust that the tree is immutable and its contents never change.
2829
2830 So you can trust the contents of a tree to be valid, the same way you
2831 can trust the contents of a blob, but you don't know where those
2832 contents 'came' from.
2833
2834 Side note on trees: since a "tree" object is a sorted list of
2835 "filename+content", you can create a diff between two trees without
2836 actually having to unpack two trees. Just ignore all common parts,
2837 and your diff will look right. In other words, you can effectively
2838 (and efficiently) tell the difference between any two random trees by
2839 O(n) where "n" is the size of the difference, rather than the size of
2840 the tree.
2841
2842 Side note 2 on trees: since the name of a "blob" depends entirely and
2843 exclusively on its contents (i.e. there are no names or permissions
2844 involved), you can see trivial renames or permission changes by
2845 noticing that the blob stayed the same. However, renames with data
2846 changes need a smarter "diff" implementation.
2847
2848 A tree is created with gitlink:git-write-tree[1] and
2849 its data can be accessed by gitlink:git-ls-tree[1].
2850 Two trees can be compared with gitlink:git-diff-tree[1].
2851
2852 [[commit-object]]
2853 Commit Object
2854 -------------
2855
2856 The "commit" object is an object that introduces the notion of
2857 history into the picture. In contrast to the other objects, it
2858 doesn't just describe the physical state of a tree, it describes how
2859 we got there, and why.
2860
2861 A "commit" is defined by the tree-object that it results in, the
2862 parent commits (zero, one or more) that led up to that point, and a
2863 comment on what happened. Again, a commit is not trusted per se:
2864 the contents are well-defined and "safe" due to the cryptographically
2865 strong signatures at all levels, but there is no reason to believe
2866 that the tree is "good" or that the merge information makes sense.
2867 The parents do not have to actually have any relationship with the
2868 result, for example.
2869
2870 Note on commits: unlike some SCM's, commits do not contain
2871 rename information or file mode change information. All of that is
2872 implicit in the trees involved (the result tree, and the result trees
2873 of the parents), and describing that makes no sense in this idiotic
2874 file manager.
2875
2876 A commit is created with gitlink:git-commit-tree[1] and
2877 its data can be accessed by gitlink:git-cat-file[1].
2878
2879 [[trust]]
2880 Trust
2881 -----
2882
2883 An aside on the notion of "trust". Trust is really outside the scope
2884 of "git", but it's worth noting a few things. First off, since
2885 everything is hashed with SHA1, you 'can' trust that an object is
2886 intact and has not been messed with by external sources. So the name
2887 of an object uniquely identifies a known state - just not a state that
2888 you may want to trust.
2889
2890 Furthermore, since the SHA1 signature of a commit refers to the
2891 SHA1 signatures of the tree it is associated with and the signatures
2892 of the parent, a single named commit specifies uniquely a whole set
2893 of history, with full contents. You can't later fake any step of the
2894 way once you have the name of a commit.
2895
2896 So to introduce some real trust in the system, the only thing you need
2897 to do is to digitally sign just 'one' special note, which includes the
2898 name of a top-level commit. Your digital signature shows others
2899 that you trust that commit, and the immutability of the history of
2900 commits tells others that they can trust the whole history.
2901
2902 In other words, you can easily validate a whole archive by just
2903 sending out a single email that tells the people the name (SHA1 hash)
2904 of the top commit, and digitally sign that email using something
2905 like GPG/PGP.
2906
2907 To assist in this, git also provides the tag object...
2908
2909 [[tag-object]]
2910 Tag Object
2911 ----------
2912
2913 Git provides the "tag" object to simplify creating, managing and
2914 exchanging symbolic and signed tokens. The "tag" object at its
2915 simplest simply symbolically identifies another object by containing
2916 the sha1, type and symbolic name.
2917
2918 However it can optionally contain additional signature information
2919 (which git doesn't care about as long as there's less than 8k of
2920 it). This can then be verified externally to git.
2921
2922 Note that despite the tag features, "git" itself only handles content
2923 integrity; the trust framework (and signature provision and
2924 verification) has to come from outside.
2925
2926 A tag is created with gitlink:git-mktag[1],
2927 its data can be accessed by gitlink:git-cat-file[1],
2928 and the signature can be verified by
2929 gitlink:git-verify-tag[1].
2930
2931
2932 [[the-index]]
2933 The "index" aka "Current Directory Cache"
2934 -----------------------------------------
2935
2936 The index is a simple binary file, which contains an efficient
2937 representation of the contents of a virtual directory. It
2938 does so by a simple array that associates a set of names, dates,
2939 permissions and content (aka "blob") objects together. The cache is
2940 always kept ordered by name, and names are unique (with a few very
2941 specific rules) at any point in time, but the cache has no long-term
2942 meaning, and can be partially updated at any time.
2943
2944 In particular, the index certainly does not need to be consistent with
2945 the current directory contents (in fact, most operations will depend on
2946 different ways to make the index 'not' be consistent with the directory
2947 hierarchy), but it has three very important attributes:
2948
2949 '(a) it can re-generate the full state it caches (not just the
2950 directory structure: it contains pointers to the "blob" objects so
2951 that it can regenerate the data too)'
2952
2953 As a special case, there is a clear and unambiguous one-way mapping
2954 from a current directory cache to a "tree object", which can be
2955 efficiently created from just the current directory cache without
2956 actually looking at any other data. So a directory cache at any one
2957 time uniquely specifies one and only one "tree" object (but has
2958 additional data to make it easy to match up that tree object with what
2959 has happened in the directory)
2960
2961 '(b) it has efficient methods for finding inconsistencies between that
2962 cached state ("tree object waiting to be instantiated") and the
2963 current state.'
2964
2965 '(c) it can additionally efficiently represent information about merge
2966 conflicts between different tree objects, allowing each pathname to be
2967 associated with sufficient information about the trees involved that
2968 you can create a three-way merge between them.'
2969
2970 Those are the ONLY three things that the directory cache does. It's a
2971 cache, and the normal operation is to re-generate it completely from a
2972 known tree object, or update/compare it with a live tree that is being
2973 developed. If you blow the directory cache away entirely, you generally
2974 haven't lost any information as long as you have the name of the tree
2975 that it described.
2976
2977 At the same time, the index is also the staging area for creating
2978 new trees, and creating a new tree always involves a controlled
2979 modification of the index file. In particular, the index file can
2980 have the representation of an intermediate tree that has not yet been
2981 instantiated. So the index can be thought of as a write-back cache,
2982 which can contain dirty information that has not yet been written back
2983 to the backing store.
2984
2985
2986
2987 [[the-workflow]]
2988 The Workflow
2989 ------------
2990
2991 Generally, all "git" operations work on the index file. Some operations
2992 work *purely* on the index file (showing the current state of the
2993 index), but most operations move data to and from the index file. Either
2994 from the database or from the working directory. Thus there are four
2995 main combinations:
2996
2997 [[working-directory-to-index]]
2998 working directory -> index
2999 ~~~~~~~~~~~~~~~~~~~~~~~~~~
3000
3001 You update the index with information from the working directory with
3002 the gitlink:git-update-index[1] command. You
3003 generally update the index information by just specifying the filename
3004 you want to update, like so:
3005
3006 -------------------------------------------------
3007 $ git-update-index filename
3008 -------------------------------------------------
3009
3010 but to avoid common mistakes with filename globbing etc, the command
3011 will not normally add totally new entries or remove old entries,
3012 i.e. it will normally just update existing cache entries.
3013
3014 To tell git that yes, you really do realize that certain files no
3015 longer exist, or that new files should be added, you
3016 should use the `--remove` and `--add` flags respectively.
3017
3018 NOTE! A `--remove` flag does 'not' mean that subsequent filenames will
3019 necessarily be removed: if the files still exist in your directory
3020 structure, the index will be updated with their new status, not
3021 removed. The only thing `--remove` means is that update-cache will be
3022 considering a removed file to be a valid thing, and if the file really
3023 does not exist any more, it will update the index accordingly.
3024
3025 As a special case, you can also do `git-update-index --refresh`, which
3026 will refresh the "stat" information of each index to match the current
3027 stat information. It will 'not' update the object status itself, and
3028 it will only update the fields that are used to quickly test whether
3029 an object still matches its old backing store object.
3030
3031 [[index-to-object-database]]
3032 index -> object database
3033 ~~~~~~~~~~~~~~~~~~~~~~~~
3034
3035 You write your current index file to a "tree" object with the program
3036
3037 -------------------------------------------------
3038 $ git-write-tree
3039 -------------------------------------------------
3040
3041 that doesn't come with any options - it will just write out the
3042 current index into the set of tree objects that describe that state,
3043 and it will return the name of the resulting top-level tree. You can
3044 use that tree to re-generate the index at any time by going in the
3045 other direction:
3046
3047 [[object-database-to-index]]
3048 object database -> index
3049 ~~~~~~~~~~~~~~~~~~~~~~~~
3050
3051 You read a "tree" file from the object database, and use that to
3052 populate (and overwrite - don't do this if your index contains any
3053 unsaved state that you might want to restore later!) your current
3054 index. Normal operation is just
3055
3056 -------------------------------------------------
3057 $ git-read-tree <sha1 of tree>
3058 -------------------------------------------------
3059
3060 and your index file will now be equivalent to the tree that you saved
3061 earlier. However, that is only your 'index' file: your working
3062 directory contents have not been modified.
3063
3064 [[index-to-working-directory]]
3065 index -> working directory
3066 ~~~~~~~~~~~~~~~~~~~~~~~~~~
3067
3068 You update your working directory from the index by "checking out"
3069 files. This is not a very common operation, since normally you'd just
3070 keep your files updated, and rather than write to your working
3071 directory, you'd tell the index files about the changes in your
3072 working directory (i.e. `git-update-index`).
3073
3074 However, if you decide to jump to a new version, or check out somebody
3075 else's version, or just restore a previous tree, you'd populate your
3076 index file with read-tree, and then you need to check out the result
3077 with
3078
3079 -------------------------------------------------
3080 $ git-checkout-index filename
3081 -------------------------------------------------
3082
3083 or, if you want to check out all of the index, use `-a`.
3084
3085 NOTE! git-checkout-index normally refuses to overwrite old files, so
3086 if you have an old version of the tree already checked out, you will
3087 need to use the "-f" flag ('before' the "-a" flag or the filename) to
3088 'force' the checkout.
3089
3090
3091 Finally, there are a few odds and ends which are not purely moving
3092 from one representation to the other:
3093
3094 [[tying-it-all-together]]
3095 Tying it all together
3096 ~~~~~~~~~~~~~~~~~~~~~
3097
3098 To commit a tree you have instantiated with "git-write-tree", you'd
3099 create a "commit" object that refers to that tree and the history
3100 behind it - most notably the "parent" commits that preceded it in
3101 history.
3102
3103 Normally a "commit" has one parent: the previous state of the tree
3104 before a certain change was made. However, sometimes it can have two
3105 or more parent commits, in which case we call it a "merge", due to the
3106 fact that such a commit brings together ("merges") two or more
3107 previous states represented by other commits.
3108
3109 In other words, while a "tree" represents a particular directory state
3110 of a working directory, a "commit" represents that state in "time",
3111 and explains how we got there.
3112
3113 You create a commit object by giving it the tree that describes the
3114 state at the time of the commit, and a list of parents:
3115
3116 -------------------------------------------------
3117 $ git-commit-tree <tree> -p <parent> [-p <parent2> ..]
3118 -------------------------------------------------
3119
3120 and then giving the reason for the commit on stdin (either through
3121 redirection from a pipe or file, or by just typing it at the tty).
3122
3123 git-commit-tree will return the name of the object that represents
3124 that commit, and you should save it away for later use. Normally,
3125 you'd commit a new `HEAD` state, and while git doesn't care where you
3126 save the note about that state, in practice we tend to just write the
3127 result to the file pointed at by `.git/HEAD`, so that we can always see
3128 what the last committed state was.
3129
3130 Here is an ASCII art by Jon Loeliger that illustrates how
3131 various pieces fit together.
3132
3133 ------------
3134
3135 commit-tree
3136 commit obj
3137 +----+
3138 | |
3139 | |
3140 V V
3141 +-----------+
3142 | Object DB |
3143 | Backing |
3144 | Store |
3145 +-----------+
3146 ^
3147 write-tree | |
3148 tree obj | |
3149 | | read-tree
3150 | | tree obj
3151 V
3152 +-----------+
3153 | Index |
3154 | "cache" |
3155 +-----------+
3156 update-index ^
3157 blob obj | |
3158 | |
3159 checkout-index -u | | checkout-index
3160 stat | | blob obj
3161 V
3162 +-----------+
3163 | Working |
3164 | Directory |
3165 +-----------+
3166
3167 ------------
3168
3169
3170 [[examining-the-data]]
3171 Examining the data
3172 ------------------
3173
3174 You can examine the data represented in the object database and the
3175 index with various helper tools. For every object, you can use
3176 gitlink:git-cat-file[1] to examine details about the
3177 object:
3178
3179 -------------------------------------------------
3180 $ git-cat-file -t <objectname>
3181 -------------------------------------------------
3182
3183 shows the type of the object, and once you have the type (which is
3184 usually implicit in where you find the object), you can use
3185
3186 -------------------------------------------------
3187 $ git-cat-file blob|tree|commit|tag <objectname>
3188 -------------------------------------------------
3189
3190 to show its contents. NOTE! Trees have binary content, and as a result
3191 there is a special helper for showing that content, called
3192 `git-ls-tree`, which turns the binary content into a more easily
3193 readable form.
3194
3195 It's especially instructive to look at "commit" objects, since those
3196 tend to be small and fairly self-explanatory. In particular, if you
3197 follow the convention of having the top commit name in `.git/HEAD`,
3198 you can do
3199
3200 -------------------------------------------------
3201 $ git-cat-file commit HEAD
3202 -------------------------------------------------
3203
3204 to see what the top commit was.
3205
3206 [[merging-multiple-trees]]
3207 Merging multiple trees
3208 ----------------------
3209
3210 Git helps you do a three-way merge, which you can expand to n-way by
3211 repeating the merge procedure arbitrary times until you finally
3212 "commit" the state. The normal situation is that you'd only do one
3213 three-way merge (two parents), and commit it, but if you like to, you
3214 can do multiple parents in one go.
3215
3216 To do a three-way merge, you need the two sets of "commit" objects
3217 that you want to merge, use those to find the closest common parent (a
3218 third "commit" object), and then use those commit objects to find the
3219 state of the directory ("tree" object) at these points.
3220
3221 To get the "base" for the merge, you first look up the common parent
3222 of two commits with
3223
3224 -------------------------------------------------
3225 $ git-merge-base <commit1> <commit2>
3226 -------------------------------------------------
3227
3228 which will return you the commit they are both based on. You should
3229 now look up the "tree" objects of those commits, which you can easily
3230 do with (for example)
3231
3232 -------------------------------------------------
3233 $ git-cat-file commit <commitname> | head -1
3234 -------------------------------------------------
3235
3236 since the tree object information is always the first line in a commit
3237 object.
3238
3239 Once you know the three trees you are going to merge (the one "original"
3240 tree, aka the common tree, and the two "result" trees, aka the branches
3241 you want to merge), you do a "merge" read into the index. This will
3242 complain if it has to throw away your old index contents, so you should
3243 make sure that you've committed those - in fact you would normally
3244 always do a merge against your last commit (which should thus match what
3245 you have in your current index anyway).
3246
3247 To do the merge, do
3248
3249 -------------------------------------------------
3250 $ git-read-tree -m -u <origtree> <yourtree> <targettree>
3251 -------------------------------------------------
3252
3253 which will do all trivial merge operations for you directly in the
3254 index file, and you can just write the result out with
3255 `git-write-tree`.
3256
3257
3258 [[merging-multiple-trees-2]]
3259 Merging multiple trees, continued
3260 ---------------------------------
3261
3262 Sadly, many merges aren't trivial. If there are files that have
3263 been added.moved or removed, or if both branches have modified the
3264 same file, you will be left with an index tree that contains "merge
3265 entries" in it. Such an index tree can 'NOT' be written out to a tree
3266 object, and you will have to resolve any such merge clashes using
3267 other tools before you can write out the result.
3268
3269 You can examine such index state with `git-ls-files --unmerged`
3270 command. An example:
3271
3272 ------------------------------------------------
3273 $ git-read-tree -m $orig HEAD $target
3274 $ git-ls-files --unmerged
3275 100644 263414f423d0e4d70dae8fe53fa34614ff3e2860 1 hello.c
3276 100644 06fa6a24256dc7e560efa5687fa84b51f0263c3a 2 hello.c
3277 100644 cc44c73eb783565da5831b4d820c962954019b69 3 hello.c
3278 ------------------------------------------------
3279
3280 Each line of the `git-ls-files --unmerged` output begins with
3281 the blob mode bits, blob SHA1, 'stage number', and the
3282 filename. The 'stage number' is git's way to say which tree it
3283 came from: stage 1 corresponds to `$orig` tree, stage 2 `HEAD`
3284 tree, and stage3 `$target` tree.
3285
3286 Earlier we said that trivial merges are done inside
3287 `git-read-tree -m`. For example, if the file did not change
3288 from `$orig` to `HEAD` nor `$target`, or if the file changed
3289 from `$orig` to `HEAD` and `$orig` to `$target` the same way,
3290 obviously the final outcome is what is in `HEAD`. What the
3291 above example shows is that file `hello.c` was changed from
3292 `$orig` to `HEAD` and `$orig` to `$target` in a different way.
3293 You could resolve this by running your favorite 3-way merge
3294 program, e.g. `diff3`, `merge`, or git's own merge-file, on
3295 the blob objects from these three stages yourself, like this:
3296
3297 ------------------------------------------------
3298 $ git-cat-file blob 263414f... >hello.c~1
3299 $ git-cat-file blob 06fa6a2... >hello.c~2
3300 $ git-cat-file blob cc44c73... >hello.c~3
3301 $ git merge-file hello.c~2 hello.c~1 hello.c~3
3302 ------------------------------------------------
3303
3304 This would leave the merge result in `hello.c~2` file, along
3305 with conflict markers if there are conflicts. After verifying
3306 the merge result makes sense, you can tell git what the final
3307 merge result for this file is by:
3308
3309 -------------------------------------------------
3310 $ mv -f hello.c~2 hello.c
3311 $ git-update-index hello.c
3312 -------------------------------------------------
3313
3314 When a path is in unmerged state, running `git-update-index` for
3315 that path tells git to mark the path resolved.
3316
3317 The above is the description of a git merge at the lowest level,
3318 to help you understand what conceptually happens under the hood.
3319 In practice, nobody, not even git itself, uses three `git-cat-file`
3320 for this. There is `git-merge-index` program that extracts the
3321 stages to temporary files and calls a "merge" script on it:
3322
3323 -------------------------------------------------
3324 $ git-merge-index git-merge-one-file hello.c
3325 -------------------------------------------------
3326
3327 and that is what higher level `git merge -s resolve` is implemented with.
3328
3329 [[pack-files]]
3330 How git stores objects efficiently: pack files
3331 ----------------------------------------------
3332
3333 We've seen how git stores each object in a file named after the
3334 object's SHA1 hash.
3335
3336 Unfortunately this system becomes inefficient once a project has a
3337 lot of objects. Try this on an old project:
3338
3339 ------------------------------------------------
3340 $ git count-objects
3341 6930 objects, 47620 kilobytes
3342 ------------------------------------------------
3343
3344 The first number is the number of objects which are kept in
3345 individual files. The second is the amount of space taken up by
3346 those "loose" objects.
3347
3348 You can save space and make git faster by moving these loose objects in
3349 to a "pack file", which stores a group of objects in an efficient
3350 compressed format; the details of how pack files are formatted can be
3351 found in link:technical/pack-format.txt[technical/pack-format.txt].
3352
3353 To put the loose objects into a pack, just run git repack:
3354
3355 ------------------------------------------------
3356 $ git repack
3357 Generating pack...
3358 Done counting 6020 objects.
3359 Deltifying 6020 objects.
3360 100% (6020/6020) done
3361 Writing 6020 objects.
3362 100% (6020/6020) done
3363 Total 6020, written 6020 (delta 4070), reused 0 (delta 0)
3364 Pack pack-3e54ad29d5b2e05838c75df582c65257b8d08e1c created.
3365 ------------------------------------------------
3366
3367 You can then run
3368
3369 ------------------------------------------------
3370 $ git prune
3371 ------------------------------------------------
3372
3373 to remove any of the "loose" objects that are now contained in the
3374 pack. This will also remove any unreferenced objects (which may be
3375 created when, for example, you use "git reset" to remove a commit).
3376 You can verify that the loose objects are gone by looking at the
3377 .git/objects directory or by running
3378
3379 ------------------------------------------------
3380 $ git count-objects
3381 0 objects, 0 kilobytes
3382 ------------------------------------------------
3383
3384 Although the object files are gone, any commands that refer to those
3385 objects will work exactly as they did before.
3386
3387 The gitlink:git-gc[1] command performs packing, pruning, and more for
3388 you, so is normally the only high-level command you need.
3389
3390 [[dangling-objects]]
3391 Dangling objects
3392 ----------------
3393
3394 The gitlink:git-fsck[1] command will sometimes complain about dangling
3395 objects. They are not a problem.
3396
3397 The most common cause of dangling objects is that you've rebased a
3398 branch, or you have pulled from somebody else who rebased a branch--see
3399 <<cleaning-up-history>>. In that case, the old head of the original
3400 branch still exists, as does everything it pointed to. The branch
3401 pointer itself just doesn't, since you replaced it with another one.
3402
3403 There are also other situations that cause dangling objects. For
3404 example, a "dangling blob" may arise because you did a "git add" of a
3405 file, but then, before you actually committed it and made it part of the
3406 bigger picture, you changed something else in that file and committed
3407 that *updated* thing - the old state that you added originally ends up
3408 not being pointed to by any commit or tree, so it's now a dangling blob
3409 object.
3410
3411 Similarly, when the "recursive" merge strategy runs, and finds that
3412 there are criss-cross merges and thus more than one merge base (which is
3413 fairly unusual, but it does happen), it will generate one temporary
3414 midway tree (or possibly even more, if you had lots of criss-crossing
3415 merges and more than two merge bases) as a temporary internal merge
3416 base, and again, those are real objects, but the end result will not end
3417 up pointing to them, so they end up "dangling" in your repository.
3418
3419 Generally, dangling objects aren't anything to worry about. They can
3420 even be very useful: if you screw something up, the dangling objects can
3421 be how you recover your old tree (say, you did a rebase, and realized
3422 that you really didn't want to - you can look at what dangling objects
3423 you have, and decide to reset your head to some old dangling state).
3424
3425 For commits, you can just use:
3426
3427 ------------------------------------------------
3428 $ gitk <dangling-commit-sha-goes-here> --not --all
3429 ------------------------------------------------
3430
3431 This asks for all the history reachable from the given commit but not
3432 from any branch, tag, or other reference. If you decide it's something
3433 you want, you can always create a new reference to it, e.g.,
3434
3435 ------------------------------------------------
3436 $ git branch recovered-branch <dangling-commit-sha-goes-here>
3437 ------------------------------------------------
3438
3439 For blobs and trees, you can't do the same, but you can still examine
3440 them. You can just do
3441
3442 ------------------------------------------------
3443 $ git show <dangling-blob/tree-sha-goes-here>
3444 ------------------------------------------------
3445
3446 to show what the contents of the blob were (or, for a tree, basically
3447 what the "ls" for that directory was), and that may give you some idea
3448 of what the operation was that left that dangling object.
3449
3450 Usually, dangling blobs and trees aren't very interesting. They're
3451 almost always the result of either being a half-way mergebase (the blob
3452 will often even have the conflict markers from a merge in it, if you
3453 have had conflicting merges that you fixed up by hand), or simply
3454 because you interrupted a "git fetch" with ^C or something like that,
3455 leaving _some_ of the new objects in the object database, but just
3456 dangling and useless.
3457
3458 Anyway, once you are sure that you're not interested in any dangling
3459 state, you can just prune all unreachable objects:
3460
3461 ------------------------------------------------
3462 $ git prune
3463 ------------------------------------------------
3464
3465 and they'll be gone. But you should only run "git prune" on a quiescent
3466 repository - it's kind of like doing a filesystem fsck recovery: you
3467 don't want to do that while the filesystem is mounted.
3468
3469 (The same is true of "git-fsck" itself, btw - but since
3470 git-fsck never actually *changes* the repository, it just reports
3471 on what it found, git-fsck itself is never "dangerous" to run.
3472 Running it while somebody is actually changing the repository can cause
3473 confusing and scary messages, but it won't actually do anything bad. In
3474 contrast, running "git prune" while somebody is actively changing the
3475 repository is a *BAD* idea).
3476
3477 [[birdview-on-the-source-code]]
3478 A birds-eye view of Git's source code
3479 -------------------------------------
3480
3481 It is not always easy for new developers to find their way through Git's
3482 source code. This section gives you a little guidance to show where to
3483 start.
3484
3485 A good place to start is with the contents of the initial commit, with:
3486
3487 ----------------------------------------------------
3488 $ git checkout e83c5163
3489 ----------------------------------------------------
3490
3491 The initial revision lays the foundation for almost everything git has
3492 today, but is small enough to read in one sitting.
3493
3494 Note that terminology has changed since that revision. For example, the
3495 README in that revision uses the word "changeset" to describe what we
3496 now call a <<def_commit_object,commit>>.
3497
3498 Also, we do not call it "cache" any more, but "index", however, the
3499 file is still called `cache.h`. Remark: Not much reason to change it now,
3500 especially since there is no good single name for it anyway, because it is
3501 basically _the_ header file which is included by _all_ of Git's C sources.
3502
3503 If you grasp the ideas in that initial commit, you should check out a
3504 more recent version and skim `cache.h`, `object.h` and `commit.h`.
3505
3506 In the early days, Git (in the tradition of UNIX) was a bunch of programs
3507 which were extremely simple, and which you used in scripts, piping the
3508 output of one into another. This turned out to be good for initial
3509 development, since it was easier to test new things. However, recently
3510 many of these parts have become builtins, and some of the core has been
3511 "libified", i.e. put into libgit.a for performance, portability reasons,
3512 and to avoid code duplication.
3513
3514 By now, you know what the index is (and find the corresponding data
3515 structures in `cache.h`), and that there are just a couple of object types
3516 (blobs, trees, commits and tags) which inherit their common structure from
3517 `struct object`, which is their first member (and thus, you can cast e.g.
3518 `(struct object *)commit` to achieve the _same_ as `&commit->object`, i.e.
3519 get at the object name and flags).
3520
3521 Now is a good point to take a break to let this information sink in.
3522
3523 Next step: get familiar with the object naming. Read <<naming-commits>>.
3524 There are quite a few ways to name an object (and not only revisions!).
3525 All of these are handled in `sha1_name.c`. Just have a quick look at
3526 the function `get_sha1()`. A lot of the special handling is done by
3527 functions like `get_sha1_basic()` or the likes.
3528
3529 This is just to get you into the groove for the most libified part of Git:
3530 the revision walker.
3531
3532 Basically, the initial version of `git log` was a shell script:
3533
3534 ----------------------------------------------------------------
3535 $ git-rev-list --pretty $(git-rev-parse --default HEAD "$@") | \
3536 LESS=-S ${PAGER:-less}
3537 ----------------------------------------------------------------
3538
3539 What does this mean?
3540
3541 `git-rev-list` is the original version of the revision walker, which
3542 _always_ printed a list of revisions to stdout. It is still functional,
3543 and needs to, since most new Git programs start out as scripts using
3544 `git-rev-list`.
3545
3546 `git-rev-parse` is not as important any more; it was only used to filter out
3547 options that were relevant for the different plumbing commands that were
3548 called by the script.
3549
3550 Most of what `git-rev-list` did is contained in `revision.c` and
3551 `revision.h`. It wraps the options in a struct named `rev_info`, which
3552 controls how and what revisions are walked, and more.
3553
3554 The original job of `git-rev-parse` is now taken by the function
3555 `setup_revisions()`, which parses the revisions and the common command line
3556 options for the revision walker. This information is stored in the struct
3557 `rev_info` for later consumption. You can do your own command line option
3558 parsing after calling `setup_revisions()`. After that, you have to call
3559 `prepare_revision_walk()` for initialization, and then you can get the
3560 commits one by one with the function `get_revision()`.
3561
3562 If you are interested in more details of the revision walking process,
3563 just have a look at the first implementation of `cmd_log()`; call
3564 `git-show v1.3.0~155^2~4` and scroll down to that function (note that you
3565 no longer need to call `setup_pager()` directly).
3566
3567 Nowadays, `git log` is a builtin, which means that it is _contained_ in the
3568 command `git`. The source side of a builtin is
3569
3570 - a function called `cmd_<bla>`, typically defined in `builtin-<bla>.c`,
3571 and declared in `builtin.h`,
3572
3573 - an entry in the `commands[]` array in `git.c`, and
3574
3575 - an entry in `BUILTIN_OBJECTS` in the `Makefile`.
3576
3577 Sometimes, more than one builtin is contained in one source file. For
3578 example, `cmd_whatchanged()` and `cmd_log()` both reside in `builtin-log.c`,
3579 since they share quite a bit of code. In that case, the commands which are
3580 _not_ named like the `.c` file in which they live have to be listed in
3581 `BUILT_INS` in the `Makefile`.
3582
3583 `git log` looks more complicated in C than it does in the original script,
3584 but that allows for a much greater flexibility and performance.
3585
3586 Here again it is a good point to take a pause.
3587
3588 Lesson three is: study the code. Really, it is the best way to learn about
3589 the organization of Git (after you know the basic concepts).
3590
3591 So, think about something which you are interested in, say, "how can I
3592 access a blob just knowing the object name of it?". The first step is to
3593 find a Git command with which you can do it. In this example, it is either
3594 `git show` or `git cat-file`.
3595
3596 For the sake of clarity, let's stay with `git cat-file`, because it
3597
3598 - is plumbing, and
3599
3600 - was around even in the initial commit (it literally went only through
3601 some 20 revisions as `cat-file.c`, was renamed to `builtin-cat-file.c`
3602 when made a builtin, and then saw less than 10 versions).
3603
3604 So, look into `builtin-cat-file.c`, search for `cmd_cat_file()` and look what
3605 it does.
3606
3607 ------------------------------------------------------------------
3608 git_config(git_default_config);
3609 if (argc != 3)
3610 usage("git-cat-file [-t|-s|-e|-p|<type>] <sha1>");
3611 if (get_sha1(argv[2], sha1))
3612 die("Not a valid object name %s", argv[2]);
3613 ------------------------------------------------------------------
3614
3615 Let's skip over the obvious details; the only really interesting part
3616 here is the call to `get_sha1()`. It tries to interpret `argv[2]` as an
3617 object name, and if it refers to an object which is present in the current
3618 repository, it writes the resulting SHA-1 into the variable `sha1`.
3619
3620 Two things are interesting here:
3621
3622 - `get_sha1()` returns 0 on _success_. This might surprise some new
3623 Git hackers, but there is a long tradition in UNIX to return different
3624 negative numbers in case of different errors -- and 0 on success.
3625
3626 - the variable `sha1` in the function signature of `get_sha1()` is `unsigned
3627 char \*`, but is actually expected to be a pointer to `unsigned
3628 char[20]`. This variable will contain the 160-bit SHA-1 of the given
3629 commit. Note that whenever a SHA-1 is passed as `unsigned char \*`, it
3630 is the binary representation, as opposed to the ASCII representation in
3631 hex characters, which is passed as `char *`.
3632
3633 You will see both of these things throughout the code.
3634
3635 Now, for the meat:
3636
3637 -----------------------------------------------------------------------------
3638 case 0:
3639 buf = read_object_with_reference(sha1, argv[1], &size, NULL);
3640 -----------------------------------------------------------------------------
3641
3642 This is how you read a blob (actually, not only a blob, but any type of
3643 object). To know how the function `read_object_with_reference()` actually
3644 works, find the source code for it (something like `git grep
3645 read_object_with | grep ":[a-z]"` in the git repository), and read
3646 the source.
3647
3648 To find out how the result can be used, just read on in `cmd_cat_file()`:
3649
3650 -----------------------------------
3651 write_or_die(1, buf, size);
3652 -----------------------------------
3653
3654 Sometimes, you do not know where to look for a feature. In many such cases,
3655 it helps to search through the output of `git log`, and then `git show` the
3656 corresponding commit.
3657
3658 Example: If you know that there was some test case for `git bundle`, but
3659 do not remember where it was (yes, you _could_ `git grep bundle t/`, but that
3660 does not illustrate the point!):
3661
3662 ------------------------
3663 $ git log --no-merges t/
3664 ------------------------
3665
3666 In the pager (`less`), just search for "bundle", go a few lines back,
3667 and see that it is in commit 18449ab0... Now just copy this object name,
3668 and paste it into the command line
3669
3670 -------------------
3671 $ git show 18449ab0
3672 -------------------
3673
3674 Voila.
3675
3676 Another example: Find out what to do in order to make some script a
3677 builtin:
3678
3679 -------------------------------------------------
3680 $ git log --no-merges --diff-filter=A builtin-*.c
3681 -------------------------------------------------
3682
3683 You see, Git is actually the best tool to find out about the source of Git
3684 itself!
3685
3686 [[glossary]]
3687 include::glossary.txt[]
3688
3689 [[git-quick-start]]
3690 Appendix A: Git Quick Reference
3691 ===============================
3692
3693 This is a quick summary of the major commands; the previous chapters
3694 explain how these work in more detail.
3695
3696 [[quick-creating-a-new-repository]]
3697 Creating a new repository
3698 -------------------------
3699
3700 From a tarball:
3701
3702 -----------------------------------------------
3703 $ tar xzf project.tar.gz
3704 $ cd project
3705 $ git init
3706 Initialized empty Git repository in .git/
3707 $ git add .
3708 $ git commit
3709 -----------------------------------------------
3710
3711 From a remote repository:
3712
3713 -----------------------------------------------
3714 $ git clone git://example.com/pub/project.git
3715 $ cd project
3716 -----------------------------------------------
3717
3718 [[managing-branches]]
3719 Managing branches
3720 -----------------
3721
3722 -----------------------------------------------
3723 $ git branch # list all local branches in this repo
3724 $ git checkout test # switch working directory to branch "test"
3725 $ git branch new # create branch "new" starting at current HEAD
3726 $ git branch -d new # delete branch "new"
3727 -----------------------------------------------
3728
3729 Instead of basing new branch on current HEAD (the default), use:
3730
3731 -----------------------------------------------
3732 $ git branch new test # branch named "test"
3733 $ git branch new v2.6.15 # tag named v2.6.15
3734 $ git branch new HEAD^ # commit before the most recent
3735 $ git branch new HEAD^^ # commit before that
3736 $ git branch new test~10 # ten commits before tip of branch "test"
3737 -----------------------------------------------
3738
3739 Create and switch to a new branch at the same time:
3740
3741 -----------------------------------------------
3742 $ git checkout -b new v2.6.15
3743 -----------------------------------------------
3744
3745 Update and examine branches from the repository you cloned from:
3746
3747 -----------------------------------------------
3748 $ git fetch # update
3749 $ git branch -r # list
3750 origin/master
3751 origin/next
3752 ...
3753 $ git checkout -b masterwork origin/master
3754 -----------------------------------------------
3755
3756 Fetch a branch from a different repository, and give it a new
3757 name in your repository:
3758
3759 -----------------------------------------------
3760 $ git fetch git://example.com/project.git theirbranch:mybranch
3761 $ git fetch git://example.com/project.git v2.6.15:mybranch
3762 -----------------------------------------------
3763
3764 Keep a list of repositories you work with regularly:
3765
3766 -----------------------------------------------
3767 $ git remote add example git://example.com/project.git
3768 $ git remote # list remote repositories
3769 example
3770 origin
3771 $ git remote show example # get details
3772 * remote example
3773 URL: git://example.com/project.git
3774 Tracked remote branches
3775 master next ...
3776 $ git fetch example # update branches from example
3777 $ git branch -r # list all remote branches
3778 -----------------------------------------------
3779
3780
3781 [[exploring-history]]
3782 Exploring history
3783 -----------------
3784
3785 -----------------------------------------------
3786 $ gitk # visualize and browse history
3787 $ git log # list all commits
3788 $ git log src/ # ...modifying src/
3789 $ git log v2.6.15..v2.6.16 # ...in v2.6.16, not in v2.6.15
3790 $ git log master..test # ...in branch test, not in branch master
3791 $ git log test..master # ...in branch master, but not in test
3792 $ git log test...master # ...in one branch, not in both
3793 $ git log -S'foo()' # ...where difference contain "foo()"
3794 $ git log --since="2 weeks ago"
3795 $ git log -p # show patches as well
3796 $ git show # most recent commit
3797 $ git diff v2.6.15..v2.6.16 # diff between two tagged versions
3798 $ git diff v2.6.15..HEAD # diff with current head
3799 $ git grep "foo()" # search working directory for "foo()"
3800 $ git grep v2.6.15 "foo()" # search old tree for "foo()"
3801 $ git show v2.6.15:a.txt # look at old version of a.txt
3802 -----------------------------------------------
3803
3804 Search for regressions:
3805
3806 -----------------------------------------------
3807 $ git bisect start
3808 $ git bisect bad # current version is bad
3809 $ git bisect good v2.6.13-rc2 # last known good revision
3810 Bisecting: 675 revisions left to test after this
3811 # test here, then:
3812 $ git bisect good # if this revision is good, or
3813 $ git bisect bad # if this revision is bad.
3814 # repeat until done.
3815 -----------------------------------------------
3816
3817 [[making-changes]]
3818 Making changes
3819 --------------
3820
3821 Make sure git knows who to blame:
3822
3823 ------------------------------------------------
3824 $ cat >>~/.gitconfig <<\EOF
3825 [user]
3826 name = Your Name Comes Here
3827 email = you@yourdomain.example.com
3828 EOF
3829 ------------------------------------------------
3830
3831 Select file contents to include in the next commit, then make the
3832 commit:
3833
3834 -----------------------------------------------
3835 $ git add a.txt # updated file
3836 $ git add b.txt # new file
3837 $ git rm c.txt # old file
3838 $ git commit
3839 -----------------------------------------------
3840
3841 Or, prepare and create the commit in one step:
3842
3843 -----------------------------------------------
3844 $ git commit d.txt # use latest content only of d.txt
3845 $ git commit -a # use latest content of all tracked files
3846 -----------------------------------------------
3847
3848 [[merging]]
3849 Merging
3850 -------
3851
3852 -----------------------------------------------
3853 $ git merge test # merge branch "test" into the current branch
3854 $ git pull git://example.com/project.git master
3855 # fetch and merge in remote branch
3856 $ git pull . test # equivalent to git merge test
3857 -----------------------------------------------
3858
3859 [[sharing-your-changes]]
3860 Sharing your changes
3861 --------------------
3862
3863 Importing or exporting patches:
3864
3865 -----------------------------------------------
3866 $ git format-patch origin..HEAD # format a patch for each commit
3867 # in HEAD but not in origin
3868 $ git am mbox # import patches from the mailbox "mbox"
3869 -----------------------------------------------
3870
3871 Fetch a branch in a different git repository, then merge into the
3872 current branch:
3873
3874 -----------------------------------------------
3875 $ git pull git://example.com/project.git theirbranch
3876 -----------------------------------------------
3877
3878 Store the fetched branch into a local branch before merging into the
3879 current branch:
3880
3881 -----------------------------------------------
3882 $ git pull git://example.com/project.git theirbranch:mybranch
3883 -----------------------------------------------
3884
3885 After creating commits on a local branch, update the remote
3886 branch with your commits:
3887
3888 -----------------------------------------------
3889 $ git push ssh://example.com/project.git mybranch:theirbranch
3890 -----------------------------------------------
3891
3892 When remote and local branch are both named "test":
3893
3894 -----------------------------------------------
3895 $ git push ssh://example.com/project.git test
3896 -----------------------------------------------
3897
3898 Shortcut version for a frequently used remote repository:
3899
3900 -----------------------------------------------
3901 $ git remote add example ssh://example.com/project.git
3902 $ git push example test
3903 -----------------------------------------------
3904
3905 [[repository-maintenance]]
3906 Repository maintenance
3907 ----------------------
3908
3909 Check for corruption:
3910
3911 -----------------------------------------------
3912 $ git fsck
3913 -----------------------------------------------
3914
3915 Recompress, remove unused cruft:
3916
3917 -----------------------------------------------
3918 $ git gc
3919 -----------------------------------------------
3920
3921
3922 [[todo]]
3923 Appendix B: Notes and todo list for this manual
3924 ===============================================
3925
3926 This is a work in progress.
3927
3928 The basic requirements:
3929 - It must be readable in order, from beginning to end, by
3930 someone intelligent with a basic grasp of the unix
3931 commandline, but without any special knowledge of git. If
3932 necessary, any other prerequisites should be specifically
3933 mentioned as they arise.
3934 - Whenever possible, section headings should clearly describe
3935 the task they explain how to do, in language that requires
3936 no more knowledge than necessary: for example, "importing
3937 patches into a project" rather than "the git-am command"
3938
3939 Think about how to create a clear chapter dependency graph that will
3940 allow people to get to important topics without necessarily reading
3941 everything in between.
3942
3943 Scan Documentation/ for other stuff left out; in particular:
3944 howto's
3945 some of technical/?
3946 hooks
3947 list of commands in gitlink:git[1]
3948
3949 Scan email archives for other stuff left out
3950
3951 Scan man pages to see if any assume more background than this manual
3952 provides.
3953
3954 Simplify beginning by suggesting disconnected head instead of
3955 temporary branch creation?
3956
3957 Add more good examples. Entire sections of just cookbook examples
3958 might be a good idea; maybe make an "advanced examples" section a
3959 standard end-of-chapter section?
3960
3961 Include cross-references to the glossary, where appropriate.
3962
3963 Document shallow clones? See draft 1.5.0 release notes for some
3964 documentation.
3965
3966 Add a section on working with other version control systems, including
3967 CVS, Subversion, and just imports of series of release tarballs.
3968
3969 More details on gitweb?
3970
3971 Write a chapter on using plumbing and writing scripts.
3972
3973 Alternates, clone -reference, etc.
3974
3975 git unpack-objects -r for recovery