Code

55d4d37b210e7f43d67a6d4d4991e5ca3640e727
[git.git] / Documentation / user-manual.txt
1 Git User's Manual (for version 1.5.1 or newer)
2 ______________________________________________
4 This manual is designed to be readable by someone with basic unix
5 command-line skills, but no previous knowledge of git.
7 <<repositories-and-branches>> and <<exploring-git-history>> explain how
8 to fetch and study a project using git--read these chapters to learn how
9 to build and test a particular version of a software project, search for
10 regressions, and so on.
12 People needing to do actual development will also want to read
13 <<Developing-with-git>> and <<sharing-development>>.
15 Further chapters cover more specialized topics.
17 Comprehensive reference documentation is available through the man
18 pages.  For a command such as "git clone", just use
20 ------------------------------------------------
21 $ man git-clone
22 ------------------------------------------------
24 See also <<git-quick-start>> for a brief overview of git commands,
25 without any explanation.
27 Also, see <<todo>> for ways that you can help make this manual more
28 complete.
31 [[repositories-and-branches]]
32 Repositories and Branches
33 =========================
35 [[how-to-get-a-git-repository]]
36 How to get a git repository
37 ---------------------------
39 It will be useful to have a git repository to experiment with as you
40 read this manual.
42 The best way to get one is by using the gitlink:git-clone[1] command
43 to download a copy of an existing repository for a project that you
44 are interested in.  If you don't already have a project in mind, here
45 are some interesting examples:
47 ------------------------------------------------
48         # git itself (approx. 10MB download):
49 $ git clone git://git.kernel.org/pub/scm/git/git.git
50         # the linux kernel (approx. 150MB download):
51 $ git clone git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6.git
52 ------------------------------------------------
54 The initial clone may be time-consuming for a large project, but you
55 will only need to clone once.
57 The clone command creates a new directory named after the project
58 ("git" or "linux-2.6" in the examples above).  After you cd into this
59 directory, you will see that it contains a copy of the project files,
60 together with a special top-level directory named ".git", which
61 contains all the information about the history of the project.
63 In most of the following, examples will be taken from one of the two
64 repositories above.
66 [[how-to-check-out]]
67 How to check out a different version of a project
68 -------------------------------------------------
70 Git is best thought of as a tool for storing the history of a
71 collection of files.  It stores the history as a compressed
72 collection of interrelated snapshots (versions) of the project's
73 contents.
75 A single git repository may contain multiple branches.  It keeps track
76 of them by keeping a list of <<def_head,heads>> which reference the
77 latest version on each branch; the gitlink:git-branch[1] command shows
78 you the list of branch heads:
80 ------------------------------------------------
81 $ git branch
82 * master
83 ------------------------------------------------
85 A freshly cloned repository contains a single branch head, by default
86 named "master", with the working directory initialized to the state of
87 the project referred to by that branch head.
89 Most projects also use <<def_tag,tags>>.  Tags, like heads, are
90 references into the project's history, and can be listed using the
91 gitlink:git-tag[1] command:
93 ------------------------------------------------
94 $ git tag -l
95 v2.6.11
96 v2.6.11-tree
97 v2.6.12
98 v2.6.12-rc2
99 v2.6.12-rc3
100 v2.6.12-rc4
101 v2.6.12-rc5
102 v2.6.12-rc6
103 v2.6.13
104 ...
105 ------------------------------------------------
107 Tags are expected to always point at the same version of a project,
108 while heads are expected to advance as development progresses.
110 Create a new branch head pointing to one of these versions and check it
111 out using gitlink:git-checkout[1]:
113 ------------------------------------------------
114 $ git checkout -b new v2.6.13
115 ------------------------------------------------
117 The working directory then reflects the contents that the project had
118 when it was tagged v2.6.13, and gitlink:git-branch[1] shows two
119 branches, with an asterisk marking the currently checked-out branch:
121 ------------------------------------------------
122 $ git branch
123   master
124 * new
125 ------------------------------------------------
127 If you decide that you'd rather see version 2.6.17, you can modify
128 the current branch to point at v2.6.17 instead, with
130 ------------------------------------------------
131 $ git reset --hard v2.6.17
132 ------------------------------------------------
134 Note that if the current branch head was your only reference to a
135 particular point in history, then resetting that branch may leave you
136 with no way to find the history it used to point to; so use this command
137 carefully.
139 [[understanding-commits]]
140 Understanding History: Commits
141 ------------------------------
143 Every change in the history of a project is represented by a commit.
144 The gitlink:git-show[1] command shows the most recent commit on the
145 current branch:
147 ------------------------------------------------
148 $ git show
149 commit 2b5f6dcce5bf94b9b119e9ed8d537098ec61c3d2
150 Author: Jamal Hadi Salim <hadi@cyberus.ca>
151 Date:   Sat Dec 2 22:22:25 2006 -0800
153     [XFRM]: Fix aevent structuring to be more complete.
154     
155     aevents can not uniquely identify an SA. We break the ABI with this
156     patch, but consensus is that since it is not yet utilized by any
157     (known) application then it is fine (better do it now than later).
158     
159     Signed-off-by: Jamal Hadi Salim <hadi@cyberus.ca>
160     Signed-off-by: David S. Miller <davem@davemloft.net>
162 diff --git a/Documentation/networking/xfrm_sync.txt b/Documentation/networking/xfrm_sync.txt
163 index 8be626f..d7aac9d 100644
164 --- a/Documentation/networking/xfrm_sync.txt
165 +++ b/Documentation/networking/xfrm_sync.txt
166 @@ -47,10 +47,13 @@ aevent_id structure looks like:
167  
168     struct xfrm_aevent_id {
169               struct xfrm_usersa_id           sa_id;
170 +             xfrm_address_t                  saddr;
171               __u32                           flags;
172 +             __u32                           reqid;
173     };
174 ...
175 ------------------------------------------------
177 As you can see, a commit shows who made the latest change, what they
178 did, and why.
180 Every commit has a 40-hexdigit id, sometimes called the "object name" or the
181 "SHA1 id", shown on the first line of the "git show" output.  You can usually
182 refer to a commit by a shorter name, such as a tag or a branch name, but this
183 longer name can also be useful.  Most importantly, it is a globally unique
184 name for this commit: so if you tell somebody else the object name (for
185 example in email), then you are guaranteed that name will refer to the same
186 commit in their repository that it does in yours (assuming their repository
187 has that commit at all).  Since the object name is computed as a hash over the
188 contents of the commit, you are guaranteed that the commit can never change
189 without its name also changing.
191 In fact, in <<git-internals>> we shall see that everything stored in git
192 history, including file data and directory contents, is stored in an object
193 with a name that is a hash of its contents.
195 [[understanding-reachability]]
196 Understanding history: commits, parents, and reachability
197 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
199 Every commit (except the very first commit in a project) also has a
200 parent commit which shows what happened before this commit.
201 Following the chain of parents will eventually take you back to the
202 beginning of the project.
204 However, the commits do not form a simple list; git allows lines of
205 development to diverge and then reconverge, and the point where two
206 lines of development reconverge is called a "merge".  The commit
207 representing a merge can therefore have more than one parent, with
208 each parent representing the most recent commit on one of the lines
209 of development leading to that point.
211 The best way to see how this works is using the gitlink:gitk[1]
212 command; running gitk now on a git repository and looking for merge
213 commits will help understand how the git organizes history.
215 In the following, we say that commit X is "reachable" from commit Y
216 if commit X is an ancestor of commit Y.  Equivalently, you could say
217 that Y is a descendent of X, or that there is a chain of parents
218 leading from commit Y to commit X.
220 [[history-diagrams]]
221 Understanding history: History diagrams
222 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
224 We will sometimes represent git history using diagrams like the one
225 below.  Commits are shown as "o", and the links between them with
226 lines drawn with - / and \.  Time goes left to right:
229 ................................................
230          o--o--o <-- Branch A
231         /
232  o--o--o <-- master
233         \
234          o--o--o <-- Branch B
235 ................................................
237 If we need to talk about a particular commit, the character "o" may
238 be replaced with another letter or number.
240 [[what-is-a-branch]]
241 Understanding history: What is a branch?
242 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
244 When we need to be precise, we will use the word "branch" to mean a line
245 of development, and "branch head" (or just "head") to mean a reference
246 to the most recent commit on a branch.  In the example above, the branch
247 head named "A" is a pointer to one particular commit, but we refer to
248 the line of three commits leading up to that point as all being part of
249 "branch A".
251 However, when no confusion will result, we often just use the term
252 "branch" both for branches and for branch heads.
254 [[manipulating-branches]]
255 Manipulating branches
256 ---------------------
258 Creating, deleting, and modifying branches is quick and easy; here's
259 a summary of the commands:
261 git branch::
262         list all branches
263 git branch <branch>::
264         create a new branch named <branch>, referencing the same
265         point in history as the current branch
266 git branch <branch> <start-point>::
267         create a new branch named <branch>, referencing
268         <start-point>, which may be specified any way you like,
269         including using a branch name or a tag name
270 git branch -d <branch>::
271         delete the branch <branch>; if the branch you are deleting
272         points to a commit which is not reachable from the current
273         branch, this command will fail with a warning.
274 git branch -D <branch>::
275         even if the branch points to a commit not reachable
276         from the current branch, you may know that that commit
277         is still reachable from some other branch or tag.  In that
278         case it is safe to use this command to force git to delete
279         the branch.
280 git checkout <branch>::
281         make the current branch <branch>, updating the working
282         directory to reflect the version referenced by <branch>
283 git checkout -b <new> <start-point>::
284         create a new branch <new> referencing <start-point>, and
285         check it out.
287 The special symbol "HEAD" can always be used to refer to the current
288 branch.  In fact, git uses a file named "HEAD" in the .git directory to
289 remember which branch is current:
291 ------------------------------------------------
292 $ cat .git/HEAD
293 ref: refs/heads/master
294 ------------------------------------------------
296 [[detached-head]]
297 Examining an old version without creating a new branch
298 ------------------------------------------------------
300 The git-checkout command normally expects a branch head, but will also
301 accept an arbitrary commit; for example, you can check out the commit
302 referenced by a tag:
304 ------------------------------------------------
305 $ git checkout v2.6.17
306 Note: moving to "v2.6.17" which isn't a local branch
307 If you want to create a new branch from this checkout, you may do so
308 (now or later) by using -b with the checkout command again. Example:
309   git checkout -b <new_branch_name>
310 HEAD is now at 427abfa... Linux v2.6.17
311 ------------------------------------------------
313 The HEAD then refers to the SHA1 of the commit instead of to a branch,
314 and git branch shows that you are no longer on a branch:
316 ------------------------------------------------
317 $ cat .git/HEAD
318 427abfa28afedffadfca9dd8b067eb6d36bac53f
319 $ git branch
320 * (no branch)
321   master
322 ------------------------------------------------
324 In this case we say that the HEAD is "detached".
326 This is an easy way to check out a particular version without having to
327 make up a name for the new branch.   You can still create a new branch
328 (or tag) for this version later if you decide to.
330 [[examining-remote-branches]]
331 Examining branches from a remote repository
332 -------------------------------------------
334 The "master" branch that was created at the time you cloned is a copy
335 of the HEAD in the repository that you cloned from.  That repository
336 may also have had other branches, though, and your local repository
337 keeps branches which track each of those remote branches, which you
338 can view using the "-r" option to gitlink:git-branch[1]:
340 ------------------------------------------------
341 $ git branch -r
342   origin/HEAD
343   origin/html
344   origin/maint
345   origin/man
346   origin/master
347   origin/next
348   origin/pu
349   origin/todo
350 ------------------------------------------------
352 You cannot check out these remote-tracking branches, but you can
353 examine them on a branch of your own, just as you would a tag:
355 ------------------------------------------------
356 $ git checkout -b my-todo-copy origin/todo
357 ------------------------------------------------
359 Note that the name "origin" is just the name that git uses by default
360 to refer to the repository that you cloned from.
362 [[how-git-stores-references]]
363 Naming branches, tags, and other references
364 -------------------------------------------
366 Branches, remote-tracking branches, and tags are all references to
367 commits.  All references are named with a slash-separated path name
368 starting with "refs"; the names we've been using so far are actually
369 shorthand:
371         - The branch "test" is short for "refs/heads/test".
372         - The tag "v2.6.18" is short for "refs/tags/v2.6.18".
373         - "origin/master" is short for "refs/remotes/origin/master".
375 The full name is occasionally useful if, for example, there ever
376 exists a tag and a branch with the same name.
378 As another useful shortcut, the "HEAD" of a repository can be referred
379 to just using the name of that repository.  So, for example, "origin"
380 is usually a shortcut for the HEAD branch in the repository "origin".
382 For the complete list of paths which git checks for references, and
383 the order it uses to decide which to choose when there are multiple
384 references with the same shorthand name, see the "SPECIFYING
385 REVISIONS" section of gitlink:git-rev-parse[1].
387 [[Updating-a-repository-with-git-fetch]]
388 Updating a repository with git fetch
389 ------------------------------------
391 Eventually the developer cloned from will do additional work in her
392 repository, creating new commits and advancing the branches to point
393 at the new commits.
395 The command "git fetch", with no arguments, will update all of the
396 remote-tracking branches to the latest version found in her
397 repository.  It will not touch any of your own branches--not even the
398 "master" branch that was created for you on clone.
400 [[fetching-branches]]
401 Fetching branches from other repositories
402 -----------------------------------------
404 You can also track branches from repositories other than the one you
405 cloned from, using gitlink:git-remote[1]:
407 -------------------------------------------------
408 $ git remote add linux-nfs git://linux-nfs.org/pub/nfs-2.6.git
409 $ git fetch linux-nfs
410 * refs/remotes/linux-nfs/master: storing branch 'master' ...
411   commit: bf81b46
412 -------------------------------------------------
414 New remote-tracking branches will be stored under the shorthand name
415 that you gave "git remote add", in this case linux-nfs:
417 -------------------------------------------------
418 $ git branch -r
419 linux-nfs/master
420 origin/master
421 -------------------------------------------------
423 If you run "git fetch <remote>" later, the tracking branches for the
424 named <remote> will be updated.
426 If you examine the file .git/config, you will see that git has added
427 a new stanza:
429 -------------------------------------------------
430 $ cat .git/config
431 ...
432 [remote "linux-nfs"]
433         url = git://linux-nfs.org/pub/nfs-2.6.git
434         fetch = +refs/heads/*:refs/remotes/linux-nfs/*
435 ...
436 -------------------------------------------------
438 This is what causes git to track the remote's branches; you may modify
439 or delete these configuration options by editing .git/config with a
440 text editor.  (See the "CONFIGURATION FILE" section of
441 gitlink:git-config[1] for details.)
443 [[exploring-git-history]]
444 Exploring git history
445 =====================
447 Git is best thought of as a tool for storing the history of a
448 collection of files.  It does this by storing compressed snapshots of
449 the contents of a file heirarchy, together with "commits" which show
450 the relationships between these snapshots.
452 Git provides extremely flexible and fast tools for exploring the
453 history of a project.
455 We start with one specialized tool that is useful for finding the
456 commit that introduced a bug into a project.
458 [[using-bisect]]
459 How to use bisect to find a regression
460 --------------------------------------
462 Suppose version 2.6.18 of your project worked, but the version at
463 "master" crashes.  Sometimes the best way to find the cause of such a
464 regression is to perform a brute-force search through the project's
465 history to find the particular commit that caused the problem.  The
466 gitlink:git-bisect[1] command can help you do this:
468 -------------------------------------------------
469 $ git bisect start
470 $ git bisect good v2.6.18
471 $ git bisect bad master
472 Bisecting: 3537 revisions left to test after this
473 [65934a9a028b88e83e2b0f8b36618fe503349f8e] BLOCK: Make USB storage depend on SCSI rather than selecting it [try #6]
474 -------------------------------------------------
476 If you run "git branch" at this point, you'll see that git has
477 temporarily moved you to a new branch named "bisect".  This branch
478 points to a commit (with commit id 65934...) that is reachable from
479 v2.6.19 but not from v2.6.18.  Compile and test it, and see whether
480 it crashes.  Assume it does crash.  Then:
482 -------------------------------------------------
483 $ git bisect bad
484 Bisecting: 1769 revisions left to test after this
485 [7eff82c8b1511017ae605f0c99ac275a7e21b867] i2c-core: Drop useless bitmaskings
486 -------------------------------------------------
488 checks out an older version.  Continue like this, telling git at each
489 stage whether the version it gives you is good or bad, and notice
490 that the number of revisions left to test is cut approximately in
491 half each time.
493 After about 13 tests (in this case), it will output the commit id of
494 the guilty commit.  You can then examine the commit with
495 gitlink:git-show[1], find out who wrote it, and mail them your bug
496 report with the commit id.  Finally, run
498 -------------------------------------------------
499 $ git bisect reset
500 -------------------------------------------------
502 to return you to the branch you were on before and delete the
503 temporary "bisect" branch.
505 Note that the version which git-bisect checks out for you at each
506 point is just a suggestion, and you're free to try a different
507 version if you think it would be a good idea.  For example,
508 occasionally you may land on a commit that broke something unrelated;
509 run
511 -------------------------------------------------
512 $ git bisect visualize
513 -------------------------------------------------
515 which will run gitk and label the commit it chose with a marker that
516 says "bisect".  Chose a safe-looking commit nearby, note its commit
517 id, and check it out with:
519 -------------------------------------------------
520 $ git reset --hard fb47ddb2db...
521 -------------------------------------------------
523 then test, run "bisect good" or "bisect bad" as appropriate, and
524 continue.
526 [[naming-commits]]
527 Naming commits
528 --------------
530 We have seen several ways of naming commits already:
532         - 40-hexdigit object name
533         - branch name: refers to the commit at the head of the given
534           branch
535         - tag name: refers to the commit pointed to by the given tag
536           (we've seen branches and tags are special cases of
537           <<how-git-stores-references,references>>).
538         - HEAD: refers to the head of the current branch
540 There are many more; see the "SPECIFYING REVISIONS" section of the
541 gitlink:git-rev-parse[1] man page for the complete list of ways to
542 name revisions.  Some examples:
544 -------------------------------------------------
545 $ git show fb47ddb2 # the first few characters of the object name
546                     # are usually enough to specify it uniquely
547 $ git show HEAD^    # the parent of the HEAD commit
548 $ git show HEAD^^   # the grandparent
549 $ git show HEAD~4   # the great-great-grandparent
550 -------------------------------------------------
552 Recall that merge commits may have more than one parent; by default,
553 ^ and ~ follow the first parent listed in the commit, but you can
554 also choose:
556 -------------------------------------------------
557 $ git show HEAD^1   # show the first parent of HEAD
558 $ git show HEAD^2   # show the second parent of HEAD
559 -------------------------------------------------
561 In addition to HEAD, there are several other special names for
562 commits:
564 Merges (to be discussed later), as well as operations such as
565 git-reset, which change the currently checked-out commit, generally
566 set ORIG_HEAD to the value HEAD had before the current operation.
568 The git-fetch operation always stores the head of the last fetched
569 branch in FETCH_HEAD.  For example, if you run git fetch without
570 specifying a local branch as the target of the operation
572 -------------------------------------------------
573 $ git fetch git://example.com/proj.git theirbranch
574 -------------------------------------------------
576 the fetched commits will still be available from FETCH_HEAD.
578 When we discuss merges we'll also see the special name MERGE_HEAD,
579 which refers to the other branch that we're merging in to the current
580 branch.
582 The gitlink:git-rev-parse[1] command is a low-level command that is
583 occasionally useful for translating some name for a commit to the object
584 name for that commit:
586 -------------------------------------------------
587 $ git rev-parse origin
588 e05db0fd4f31dde7005f075a84f96b360d05984b
589 -------------------------------------------------
591 [[creating-tags]]
592 Creating tags
593 -------------
595 We can also create a tag to refer to a particular commit; after
596 running
598 -------------------------------------------------
599 $ git tag stable-1 1b2e1d63ff
600 -------------------------------------------------
602 You can use stable-1 to refer to the commit 1b2e1d63ff.
604 This creates a "lightweight" tag.  If you would also like to include a
605 comment with the tag, and possibly sign it cryptographically, then you
606 should create a tag object instead; see the gitlink:git-tag[1] man page
607 for details.
609 [[browsing-revisions]]
610 Browsing revisions
611 ------------------
613 The gitlink:git-log[1] command can show lists of commits.  On its
614 own, it shows all commits reachable from the parent commit; but you
615 can also make more specific requests:
617 -------------------------------------------------
618 $ git log v2.5..        # commits since (not reachable from) v2.5
619 $ git log test..master  # commits reachable from master but not test
620 $ git log master..test  # ...reachable from test but not master
621 $ git log master...test # ...reachable from either test or master,
622                         #    but not both
623 $ git log --since="2 weeks ago" # commits from the last 2 weeks
624 $ git log Makefile      # commits which modify Makefile
625 $ git log fs/           # ... which modify any file under fs/
626 $ git log -S'foo()'     # commits which add or remove any file data
627                         # matching the string 'foo()'
628 -------------------------------------------------
630 And of course you can combine all of these; the following finds
631 commits since v2.5 which touch the Makefile or any file under fs:
633 -------------------------------------------------
634 $ git log v2.5.. Makefile fs/
635 -------------------------------------------------
637 You can also ask git log to show patches:
639 -------------------------------------------------
640 $ git log -p
641 -------------------------------------------------
643 See the "--pretty" option in the gitlink:git-log[1] man page for more
644 display options.
646 Note that git log starts with the most recent commit and works
647 backwards through the parents; however, since git history can contain
648 multiple independent lines of development, the particular order that
649 commits are listed in may be somewhat arbitrary.
651 [[generating-diffs]]
652 Generating diffs
653 ----------------
655 You can generate diffs between any two versions using
656 gitlink:git-diff[1]:
658 -------------------------------------------------
659 $ git diff master..test
660 -------------------------------------------------
662 Sometimes what you want instead is a set of patches:
664 -------------------------------------------------
665 $ git format-patch master..test
666 -------------------------------------------------
668 will generate a file with a patch for each commit reachable from test
669 but not from master.  Note that if master also has commits which are
670 not reachable from test, then the combined result of these patches
671 will not be the same as the diff produced by the git-diff example.
673 [[viewing-old-file-versions]]
674 Viewing old file versions
675 -------------------------
677 You can always view an old version of a file by just checking out the
678 correct revision first.  But sometimes it is more convenient to be
679 able to view an old version of a single file without checking
680 anything out; this command does that:
682 -------------------------------------------------
683 $ git show v2.5:fs/locks.c
684 -------------------------------------------------
686 Before the colon may be anything that names a commit, and after it
687 may be any path to a file tracked by git.
689 [[history-examples]]
690 Examples
691 --------
693 [[checking-for-equal-branches]]
694 Check whether two branches point at the same history
695 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
697 Suppose you want to check whether two branches point at the same point
698 in history.
700 -------------------------------------------------
701 $ git diff origin..master
702 -------------------------------------------------
704 will tell you whether the contents of the project are the same at the
705 two branches; in theory, however, it's possible that the same project
706 contents could have been arrived at by two different historical
707 routes.  You could compare the object names:
709 -------------------------------------------------
710 $ git rev-list origin
711 e05db0fd4f31dde7005f075a84f96b360d05984b
712 $ git rev-list master
713 e05db0fd4f31dde7005f075a84f96b360d05984b
714 -------------------------------------------------
716 Or you could recall that the ... operator selects all commits
717 contained reachable from either one reference or the other but not
718 both: so
720 -------------------------------------------------
721 $ git log origin...master
722 -------------------------------------------------
724 will return no commits when the two branches are equal.
726 [[finding-tagged-descendants]]
727 Find first tagged version including a given fix
728 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
730 Suppose you know that the commit e05db0fd fixed a certain problem.
731 You'd like to find the earliest tagged release that contains that
732 fix.
734 Of course, there may be more than one answer--if the history branched
735 after commit e05db0fd, then there could be multiple "earliest" tagged
736 releases.
738 You could just visually inspect the commits since e05db0fd:
740 -------------------------------------------------
741 $ gitk e05db0fd..
742 -------------------------------------------------
744 Or you can use gitlink:git-name-rev[1], which will give the commit a
745 name based on any tag it finds pointing to one of the commit's
746 descendants:
748 -------------------------------------------------
749 $ git name-rev --tags e05db0fd
750 e05db0fd tags/v1.5.0-rc1^0~23
751 -------------------------------------------------
753 The gitlink:git-describe[1] command does the opposite, naming the
754 revision using a tag on which the given commit is based:
756 -------------------------------------------------
757 $ git describe e05db0fd
758 v1.5.0-rc0-260-ge05db0f
759 -------------------------------------------------
761 but that may sometimes help you guess which tags might come after the
762 given commit.
764 If you just want to verify whether a given tagged version contains a
765 given commit, you could use gitlink:git-merge-base[1]:
767 -------------------------------------------------
768 $ git merge-base e05db0fd v1.5.0-rc1
769 e05db0fd4f31dde7005f075a84f96b360d05984b
770 -------------------------------------------------
772 The merge-base command finds a common ancestor of the given commits,
773 and always returns one or the other in the case where one is a
774 descendant of the other; so the above output shows that e05db0fd
775 actually is an ancestor of v1.5.0-rc1.
777 Alternatively, note that
779 -------------------------------------------------
780 $ git log v1.5.0-rc1..e05db0fd
781 -------------------------------------------------
783 will produce empty output if and only if v1.5.0-rc1 includes e05db0fd,
784 because it outputs only commits that are not reachable from v1.5.0-rc1.
786 As yet another alternative, the gitlink:git-show-branch[1] command lists
787 the commits reachable from its arguments with a display on the left-hand
788 side that indicates which arguments that commit is reachable from.  So,
789 you can run something like
791 -------------------------------------------------
792 $ git show-branch e05db0fd v1.5.0-rc0 v1.5.0-rc1 v1.5.0-rc2
793 ! [e05db0fd] Fix warnings in sha1_file.c - use C99 printf format if
794 available
795  ! [v1.5.0-rc0] GIT v1.5.0 preview
796   ! [v1.5.0-rc1] GIT v1.5.0-rc1
797    ! [v1.5.0-rc2] GIT v1.5.0-rc2
798 ...
799 -------------------------------------------------
801 then search for a line that looks like
803 -------------------------------------------------
804 + ++ [e05db0fd] Fix warnings in sha1_file.c - use C99 printf format if
805 available
806 -------------------------------------------------
808 Which shows that e05db0fd is reachable from itself, from v1.5.0-rc1, and
809 from v1.5.0-rc2, but not from v1.5.0-rc0.
811 [[making-a-release]]
812 Creating a changelog and tarball for a software release
813 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
815 The gitlink:git-archive[1] command can create a tar or zip archive from
816 any version of a project; for example:
818 -------------------------------------------------
819 $ git archive --format=tar --prefix=project/ HEAD | gzip >latest.tar.gz
820 -------------------------------------------------
822 will use HEAD to produce a tar archive in which each filename is
823 preceded by "prefix/".
825 If you're releasing a new version of a software project, you may want
826 to simultaneously make a changelog to include in the release
827 announcement.
829 Linus Torvalds, for example, makes new kernel releases by tagging them,
830 then running:
832 -------------------------------------------------
833 $ release-script 2.6.12 2.6.13-rc6 2.6.13-rc7
834 -------------------------------------------------
836 where release-script is a shell script that looks like:
838 -------------------------------------------------
839 #!/bin/sh
840 stable="$1"
841 last="$2"
842 new="$3"
843 echo "# git tag v$new"
844 echo "git archive --prefix=linux-$new/ v$new | gzip -9 > ../linux-$new.tar.gz"
845 echo "git diff v$stable v$new | gzip -9 > ../patch-$new.gz"
846 echo "git log --no-merges v$new ^v$last > ../ChangeLog-$new"
847 echo "git shortlog --no-merges v$new ^v$last > ../ShortLog"
848 echo "git diff --stat --summary -M v$last v$new > ../diffstat-$new"
849 -------------------------------------------------
851 and then he just cut-and-pastes the output commands after verifying that
852 they look OK.
854 [[Developing-with-git]]
855 Developing with git
856 ===================
858 [[telling-git-your-name]]
859 Telling git your name
860 ---------------------
862 Before creating any commits, you should introduce yourself to git.  The
863 easiest way to do so is to make sure the following lines appear in a
864 file named .gitconfig in your home directory:
866 ------------------------------------------------
867 [user]
868         name = Your Name Comes Here
869         email = you@yourdomain.example.com
870 ------------------------------------------------
872 (See the "CONFIGURATION FILE" section of gitlink:git-config[1] for
873 details on the configuration file.)
876 [[creating-a-new-repository]]
877 Creating a new repository
878 -------------------------
880 Creating a new repository from scratch is very easy:
882 -------------------------------------------------
883 $ mkdir project
884 $ cd project
885 $ git init
886 -------------------------------------------------
888 If you have some initial content (say, a tarball):
890 -------------------------------------------------
891 $ tar -xzvf project.tar.gz
892 $ cd project
893 $ git init
894 $ git add . # include everything below ./ in the first commit:
895 $ git commit
896 -------------------------------------------------
898 [[how-to-make-a-commit]]
899 How to make a commit
900 --------------------
902 Creating a new commit takes three steps:
904         1. Making some changes to the working directory using your
905            favorite editor.
906         2. Telling git about your changes.
907         3. Creating the commit using the content you told git about
908            in step 2.
910 In practice, you can interleave and repeat steps 1 and 2 as many
911 times as you want: in order to keep track of what you want committed
912 at step 3, git maintains a snapshot of the tree's contents in a
913 special staging area called "the index."
915 At the beginning, the content of the index will be identical to
916 that of the HEAD.  The command "git diff --cached", which shows
917 the difference between the HEAD and the index, should therefore
918 produce no output at that point.
920 Modifying the index is easy:
922 To update the index with the new contents of a modified file, use
924 -------------------------------------------------
925 $ git add path/to/file
926 -------------------------------------------------
928 To add the contents of a new file to the index, use
930 -------------------------------------------------
931 $ git add path/to/file
932 -------------------------------------------------
934 To remove a file from the index and from the working tree,
936 -------------------------------------------------
937 $ git rm path/to/file
938 -------------------------------------------------
940 After each step you can verify that
942 -------------------------------------------------
943 $ git diff --cached
944 -------------------------------------------------
946 always shows the difference between the HEAD and the index file--this
947 is what you'd commit if you created the commit now--and that
949 -------------------------------------------------
950 $ git diff
951 -------------------------------------------------
953 shows the difference between the working tree and the index file.
955 Note that "git add" always adds just the current contents of a file
956 to the index; further changes to the same file will be ignored unless
957 you run git-add on the file again.
959 When you're ready, just run
961 -------------------------------------------------
962 $ git commit
963 -------------------------------------------------
965 and git will prompt you for a commit message and then create the new
966 commit.  Check to make sure it looks like what you expected with
968 -------------------------------------------------
969 $ git show
970 -------------------------------------------------
972 As a special shortcut,
973                 
974 -------------------------------------------------
975 $ git commit -a
976 -------------------------------------------------
978 will update the index with any files that you've modified or removed
979 and create a commit, all in one step.
981 A number of commands are useful for keeping track of what you're
982 about to commit:
984 -------------------------------------------------
985 $ git diff --cached # difference between HEAD and the index; what
986                     # would be commited if you ran "commit" now.
987 $ git diff          # difference between the index file and your
988                     # working directory; changes that would not
989                     # be included if you ran "commit" now.
990 $ git diff HEAD     # difference between HEAD and working tree; what
991                     # would be committed if you ran "commit -a" now.
992 $ git status        # a brief per-file summary of the above.
993 -------------------------------------------------
995 [[creating-good-commit-messages]]
996 Creating good commit messages
997 -----------------------------
999 Though not required, it's a good idea to begin the commit message
1000 with a single short (less than 50 character) line summarizing the
1001 change, followed by a blank line and then a more thorough
1002 description.  Tools that turn commits into email, for example, use
1003 the first line on the Subject line and the rest of the commit in the
1004 body.
1006 [[how-to-merge]]
1007 How to merge
1008 ------------
1010 You can rejoin two diverging branches of development using
1011 gitlink:git-merge[1]:
1013 -------------------------------------------------
1014 $ git merge branchname
1015 -------------------------------------------------
1017 merges the development in the branch "branchname" into the current
1018 branch.  If there are conflicts--for example, if the same file is
1019 modified in two different ways in the remote branch and the local
1020 branch--then you are warned; the output may look something like this:
1022 -------------------------------------------------
1023 $ git merge next
1024  100% (4/4) done
1025 Auto-merged file.txt
1026 CONFLICT (content): Merge conflict in file.txt
1027 Automatic merge failed; fix conflicts and then commit the result.
1028 -------------------------------------------------
1030 Conflict markers are left in the problematic files, and after
1031 you resolve the conflicts manually, you can update the index
1032 with the contents and run git commit, as you normally would when
1033 creating a new file.
1035 If you examine the resulting commit using gitk, you will see that it
1036 has two parents, one pointing to the top of the current branch, and
1037 one to the top of the other branch.
1039 [[resolving-a-merge]]
1040 Resolving a merge
1041 -----------------
1043 When a merge isn't resolved automatically, git leaves the index and
1044 the working tree in a special state that gives you all the
1045 information you need to help resolve the merge.
1047 Files with conflicts are marked specially in the index, so until you
1048 resolve the problem and update the index, gitlink:git-commit[1] will
1049 fail:
1051 -------------------------------------------------
1052 $ git commit
1053 file.txt: needs merge
1054 -------------------------------------------------
1056 Also, gitlink:git-status[1] will list those files as "unmerged", and the
1057 files with conflicts will have conflict markers added, like this:
1059 -------------------------------------------------
1060 <<<<<<< HEAD:file.txt
1061 Hello world
1062 =======
1063 Goodbye
1064 >>>>>>> 77976da35a11db4580b80ae27e8d65caf5208086:file.txt
1065 -------------------------------------------------
1067 All you need to do is edit the files to resolve the conflicts, and then
1069 -------------------------------------------------
1070 $ git add file.txt
1071 $ git commit
1072 -------------------------------------------------
1074 Note that the commit message will already be filled in for you with
1075 some information about the merge.  Normally you can just use this
1076 default message unchanged, but you may add additional commentary of
1077 your own if desired.
1079 The above is all you need to know to resolve a simple merge.  But git
1080 also provides more information to help resolve conflicts:
1082 [[conflict-resolution]]
1083 Getting conflict-resolution help during a merge
1084 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1086 All of the changes that git was able to merge automatically are
1087 already added to the index file, so gitlink:git-diff[1] shows only
1088 the conflicts.  It uses an unusual syntax:
1090 -------------------------------------------------
1091 $ git diff
1092 diff --cc file.txt
1093 index 802992c,2b60207..0000000
1094 --- a/file.txt
1095 +++ b/file.txt
1096 @@@ -1,1 -1,1 +1,5 @@@
1097 ++<<<<<<< HEAD:file.txt
1098  +Hello world
1099 ++=======
1100 + Goodbye
1101 ++>>>>>>> 77976da35a11db4580b80ae27e8d65caf5208086:file.txt
1102 -------------------------------------------------
1104 Recall that the commit which will be commited after we resolve this
1105 conflict will have two parents instead of the usual one: one parent
1106 will be HEAD, the tip of the current branch; the other will be the
1107 tip of the other branch, which is stored temporarily in MERGE_HEAD.
1109 During the merge, the index holds three versions of each file.  Each of
1110 these three "file stages" represents a different version of the file:
1112 -------------------------------------------------
1113 $ git show :1:file.txt  # the file in a common ancestor of both branches
1114 $ git show :2:file.txt  # the version from HEAD, but including any
1115                         # nonconflicting changes from MERGE_HEAD
1116 $ git show :3:file.txt  # the version from MERGE_HEAD, but including any
1117                         # nonconflicting changes from HEAD.
1118 -------------------------------------------------
1120 Since the stage 2 and stage 3 versions have already been updated with
1121 nonconflicting changes, the only remaining differences between them are
1122 the important ones; thus gitlink:git-diff[1] can use the information in
1123 the index to show only those conflicts.
1125 The diff above shows the differences between the working-tree version of
1126 file.txt and the stage 2 and stage 3 versions.  So instead of preceding
1127 each line by a single "+" or "-", it now uses two columns: the first
1128 column is used for differences between the first parent and the working
1129 directory copy, and the second for differences between the second parent
1130 and the working directory copy.  (See the "COMBINED DIFF FORMAT" section
1131 of gitlink:git-diff-files[1] for a details of the format.)
1133 After resolving the conflict in the obvious way (but before updating the
1134 index), the diff will look like:
1136 -------------------------------------------------
1137 $ git diff
1138 diff --cc file.txt
1139 index 802992c,2b60207..0000000
1140 --- a/file.txt
1141 +++ b/file.txt
1142 @@@ -1,1 -1,1 +1,1 @@@
1143 - Hello world
1144  -Goodbye
1145 ++Goodbye world
1146 -------------------------------------------------
1148 This shows that our resolved version deleted "Hello world" from the
1149 first parent, deleted "Goodbye" from the second parent, and added
1150 "Goodbye world", which was previously absent from both.
1152 Some special diff options allow diffing the working directory against
1153 any of these stages:
1155 -------------------------------------------------
1156 $ git diff -1 file.txt          # diff against stage 1
1157 $ git diff --base file.txt      # same as the above
1158 $ git diff -2 file.txt          # diff against stage 2
1159 $ git diff --ours file.txt      # same as the above
1160 $ git diff -3 file.txt          # diff against stage 3
1161 $ git diff --theirs file.txt    # same as the above.
1162 -------------------------------------------------
1164 The gitlink:git-log[1] and gitk[1] commands also provide special help
1165 for merges:
1167 -------------------------------------------------
1168 $ git log --merge
1169 $ gitk --merge
1170 -------------------------------------------------
1172 These will display all commits which exist only on HEAD or on
1173 MERGE_HEAD, and which touch an unmerged file.
1175 You may also use gitlink:git-mergetool[1], which lets you merge the
1176 unmerged files using external tools such as emacs or kdiff3.
1178 Each time you resolve the conflicts in a file and update the index:
1180 -------------------------------------------------
1181 $ git add file.txt
1182 -------------------------------------------------
1184 the different stages of that file will be "collapsed", after which
1185 git-diff will (by default) no longer show diffs for that file.
1187 [[undoing-a-merge]]
1188 Undoing a merge
1189 ---------------
1191 If you get stuck and decide to just give up and throw the whole mess
1192 away, you can always return to the pre-merge state with
1194 -------------------------------------------------
1195 $ git reset --hard HEAD
1196 -------------------------------------------------
1198 Or, if you've already commited the merge that you want to throw away,
1200 -------------------------------------------------
1201 $ git reset --hard ORIG_HEAD
1202 -------------------------------------------------
1204 However, this last command can be dangerous in some cases--never
1205 throw away a commit you have already committed if that commit may
1206 itself have been merged into another branch, as doing so may confuse
1207 further merges.
1209 [[fast-forwards]]
1210 Fast-forward merges
1211 -------------------
1213 There is one special case not mentioned above, which is treated
1214 differently.  Normally, a merge results in a merge commit, with two
1215 parents, one pointing at each of the two lines of development that
1216 were merged.
1218 However, if the current branch is a descendant of the other--so every
1219 commit present in the one is already contained in the other--then git
1220 just performs a "fast forward"; the head of the current branch is moved
1221 forward to point at the head of the merged-in branch, without any new
1222 commits being created.
1224 [[fixing-mistakes]]
1225 Fixing mistakes
1226 ---------------
1228 If you've messed up the working tree, but haven't yet committed your
1229 mistake, you can return the entire working tree to the last committed
1230 state with
1232 -------------------------------------------------
1233 $ git reset --hard HEAD
1234 -------------------------------------------------
1236 If you make a commit that you later wish you hadn't, there are two
1237 fundamentally different ways to fix the problem:
1239         1. You can create a new commit that undoes whatever was done
1240         by the previous commit.  This is the correct thing if your
1241         mistake has already been made public.
1243         2. You can go back and modify the old commit.  You should
1244         never do this if you have already made the history public;
1245         git does not normally expect the "history" of a project to
1246         change, and cannot correctly perform repeated merges from
1247         a branch that has had its history changed.
1249 [[reverting-a-commit]]
1250 Fixing a mistake with a new commit
1251 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1253 Creating a new commit that reverts an earlier change is very easy;
1254 just pass the gitlink:git-revert[1] command a reference to the bad
1255 commit; for example, to revert the most recent commit:
1257 -------------------------------------------------
1258 $ git revert HEAD
1259 -------------------------------------------------
1261 This will create a new commit which undoes the change in HEAD.  You
1262 will be given a chance to edit the commit message for the new commit.
1264 You can also revert an earlier change, for example, the next-to-last:
1266 -------------------------------------------------
1267 $ git revert HEAD^
1268 -------------------------------------------------
1270 In this case git will attempt to undo the old change while leaving
1271 intact any changes made since then.  If more recent changes overlap
1272 with the changes to be reverted, then you will be asked to fix
1273 conflicts manually, just as in the case of <<resolving-a-merge,
1274 resolving a merge>>.
1276 [[fixing-a-mistake-by-editing-history]]
1277 Fixing a mistake by editing history
1278 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1280 If the problematic commit is the most recent commit, and you have not
1281 yet made that commit public, then you may just
1282 <<undoing-a-merge,destroy it using git-reset>>.
1284 Alternatively, you
1285 can edit the working directory and update the index to fix your
1286 mistake, just as if you were going to <<how-to-make-a-commit,create a
1287 new commit>>, then run
1289 -------------------------------------------------
1290 $ git commit --amend
1291 -------------------------------------------------
1293 which will replace the old commit by a new commit incorporating your
1294 changes, giving you a chance to edit the old commit message first.
1296 Again, you should never do this to a commit that may already have
1297 been merged into another branch; use gitlink:git-revert[1] instead in
1298 that case.
1300 It is also possible to edit commits further back in the history, but
1301 this is an advanced topic to be left for
1302 <<cleaning-up-history,another chapter>>.
1304 [[checkout-of-path]]
1305 Checking out an old version of a file
1306 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1308 In the process of undoing a previous bad change, you may find it
1309 useful to check out an older version of a particular file using
1310 gitlink:git-checkout[1].  We've used git checkout before to switch
1311 branches, but it has quite different behavior if it is given a path
1312 name: the command
1314 -------------------------------------------------
1315 $ git checkout HEAD^ path/to/file
1316 -------------------------------------------------
1318 replaces path/to/file by the contents it had in the commit HEAD^, and
1319 also updates the index to match.  It does not change branches.
1321 If you just want to look at an old version of the file, without
1322 modifying the working directory, you can do that with
1323 gitlink:git-show[1]:
1325 -------------------------------------------------
1326 $ git show HEAD^:path/to/file
1327 -------------------------------------------------
1329 which will display the given version of the file.
1331 [[ensuring-good-performance]]
1332 Ensuring good performance
1333 -------------------------
1335 On large repositories, git depends on compression to keep the history
1336 information from taking up to much space on disk or in memory.
1338 This compression is not performed automatically.  Therefore you
1339 should occasionally run gitlink:git-gc[1]:
1341 -------------------------------------------------
1342 $ git gc
1343 -------------------------------------------------
1345 to recompress the archive.  This can be very time-consuming, so
1346 you may prefer to run git-gc when you are not doing other work.
1349 [[ensuring-reliability]]
1350 Ensuring reliability
1351 --------------------
1353 [[checking-for-corruption]]
1354 Checking the repository for corruption
1355 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1357 The gitlink:git-fsck[1] command runs a number of self-consistency checks
1358 on the repository, and reports on any problems.  This may take some
1359 time.  The most common warning by far is about "dangling" objects:
1361 -------------------------------------------------
1362 $ git fsck
1363 dangling commit 7281251ddd2a61e38657c827739c57015671a6b3
1364 dangling commit 2706a059f258c6b245f298dc4ff2ccd30ec21a63
1365 dangling commit 13472b7c4b80851a1bc551779171dcb03655e9b5
1366 dangling blob 218761f9d90712d37a9c5e36f406f92202db07eb
1367 dangling commit bf093535a34a4d35731aa2bd90fe6b176302f14f
1368 dangling commit 8e4bec7f2ddaa268bef999853c25755452100f8e
1369 dangling tree d50bb86186bf27b681d25af89d3b5b68382e4085
1370 dangling tree b24c2473f1fd3d91352a624795be026d64c8841f
1371 ...
1372 -------------------------------------------------
1374 Dangling objects are not a problem.  At worst they may take up a little
1375 extra disk space.  They can sometimes provide a last-resort method of
1376 recovery lost work--see <<dangling-objects>> for details.  However, if
1377 you want, you may remove them with gitlink:git-prune[1] or the --prune
1378 option to gitlink:git-gc[1]:
1380 -------------------------------------------------
1381 $ git gc --prune
1382 -------------------------------------------------
1384 This may be time-consuming.  Unlike most other git operations (including
1385 git-gc when run without any options), it is not safe to prune while
1386 other git operations are in progress in the same repository.
1388 [[recovering-lost-changes]]
1389 Recovering lost changes
1390 ~~~~~~~~~~~~~~~~~~~~~~~
1392 [[reflogs]]
1393 Reflogs
1394 ^^^^^^^
1396 Say you modify a branch with gitlink:git-reset[1] --hard, and then
1397 realize that the branch was the only reference you had to that point in
1398 history.
1400 Fortunately, git also keeps a log, called a "reflog", of all the
1401 previous values of each branch.  So in this case you can still find the
1402 old history using, for example, 
1404 -------------------------------------------------
1405 $ git log master@{1}
1406 -------------------------------------------------
1408 This lists the commits reachable from the previous version of the head.
1409 This syntax can be used to with any git command that accepts a commit,
1410 not just with git log.  Some other examples:
1412 -------------------------------------------------
1413 $ git show master@{2}           # See where the branch pointed 2,
1414 $ git show master@{3}           # 3, ... changes ago.
1415 $ gitk master@{yesterday}       # See where it pointed yesterday,
1416 $ gitk master@{"1 week ago"}    # ... or last week
1417 $ git log --walk-reflogs master # show reflog entries for master
1418 -------------------------------------------------
1420 A separate reflog is kept for the HEAD, so
1422 -------------------------------------------------
1423 $ git show HEAD@{"1 week ago"}
1424 -------------------------------------------------
1426 will show what HEAD pointed to one week ago, not what the current branch
1427 pointed to one week ago.  This allows you to see the history of what
1428 you've checked out.
1430 The reflogs are kept by default for 30 days, after which they may be
1431 pruned.  See gitlink:git-reflog[1] and gitlink:git-gc[1] to learn
1432 how to control this pruning, and see the "SPECIFYING REVISIONS"
1433 section of gitlink:git-rev-parse[1] for details.
1435 Note that the reflog history is very different from normal git history.
1436 While normal history is shared by every repository that works on the
1437 same project, the reflog history is not shared: it tells you only about
1438 how the branches in your local repository have changed over time.
1440 [[dangling-object-recovery]]
1441 Examining dangling objects
1442 ^^^^^^^^^^^^^^^^^^^^^^^^^^
1444 In some situations the reflog may not be able to save you.  For example,
1445 suppose you delete a branch, then realize you need the history it
1446 contained.  The reflog is also deleted; however, if you have not yet
1447 pruned the repository, then you may still be able to find the lost
1448 commits in the dangling objects that git-fsck reports.  See
1449 <<dangling-objects>> for the details.
1451 -------------------------------------------------
1452 $ git fsck
1453 dangling commit 7281251ddd2a61e38657c827739c57015671a6b3
1454 dangling commit 2706a059f258c6b245f298dc4ff2ccd30ec21a63
1455 dangling commit 13472b7c4b80851a1bc551779171dcb03655e9b5
1456 ...
1457 -------------------------------------------------
1459 You can examine
1460 one of those dangling commits with, for example,
1462 ------------------------------------------------
1463 $ gitk 7281251ddd --not --all
1464 ------------------------------------------------
1466 which does what it sounds like: it says that you want to see the commit
1467 history that is described by the dangling commit(s), but not the
1468 history that is described by all your existing branches and tags.  Thus
1469 you get exactly the history reachable from that commit that is lost.
1470 (And notice that it might not be just one commit: we only report the
1471 "tip of the line" as being dangling, but there might be a whole deep
1472 and complex commit history that was dropped.)
1474 If you decide you want the history back, you can always create a new
1475 reference pointing to it, for example, a new branch:
1477 ------------------------------------------------
1478 $ git branch recovered-branch 7281251ddd 
1479 ------------------------------------------------
1481 Other types of dangling objects (blobs and trees) are also possible, and
1482 dangling objects can arise in other situations.
1485 [[sharing-development]]
1486 Sharing development with others
1487 ===============================
1489 [[getting-updates-with-git-pull]]
1490 Getting updates with git pull
1491 -----------------------------
1493 After you clone a repository and make a few changes of your own, you
1494 may wish to check the original repository for updates and merge them
1495 into your own work.
1497 We have already seen <<Updating-a-repository-with-git-fetch,how to
1498 keep remote tracking branches up to date>> with gitlink:git-fetch[1],
1499 and how to merge two branches.  So you can merge in changes from the
1500 original repository's master branch with:
1502 -------------------------------------------------
1503 $ git fetch
1504 $ git merge origin/master
1505 -------------------------------------------------
1507 However, the gitlink:git-pull[1] command provides a way to do this in
1508 one step:
1510 -------------------------------------------------
1511 $ git pull origin master
1512 -------------------------------------------------
1514 In fact, "origin" is normally the default repository to pull from,
1515 and the default branch is normally the HEAD of the remote repository,
1516 so often you can accomplish the above with just
1518 -------------------------------------------------
1519 $ git pull
1520 -------------------------------------------------
1522 See the descriptions of the branch.<name>.remote and branch.<name>.merge
1523 options in gitlink:git-config[1] to learn how to control these defaults
1524 depending on the current branch.  Also note that the --track option to
1525 gitlink:git-branch[1] and gitlink:git-checkout[1] can be used to
1526 automatically set the default remote branch to pull from at the time
1527 that a branch is created:
1529 -------------------------------------------------
1530 $ git checkout --track -b origin/maint maint
1531 -------------------------------------------------
1533 In addition to saving you keystrokes, "git pull" also helps you by
1534 producing a default commit message documenting the branch and
1535 repository that you pulled from.
1537 (But note that no such commit will be created in the case of a
1538 <<fast-forwards,fast forward>>; instead, your branch will just be
1539 updated to point to the latest commit from the upstream branch.)
1541 The git-pull command can also be given "." as the "remote" repository,
1542 in which case it just merges in a branch from the current repository; so
1543 the commands
1545 -------------------------------------------------
1546 $ git pull . branch
1547 $ git merge branch
1548 -------------------------------------------------
1550 are roughly equivalent.  The former is actually very commonly used.
1552 [[submitting-patches]]
1553 Submitting patches to a project
1554 -------------------------------
1556 If you just have a few changes, the simplest way to submit them may
1557 just be to send them as patches in email:
1559 First, use gitlink:git-format-patch[1]; for example:
1561 -------------------------------------------------
1562 $ git format-patch origin
1563 -------------------------------------------------
1565 will produce a numbered series of files in the current directory, one
1566 for each patch in the current branch but not in origin/HEAD.
1568 You can then import these into your mail client and send them by
1569 hand.  However, if you have a lot to send at once, you may prefer to
1570 use the gitlink:git-send-email[1] script to automate the process.
1571 Consult the mailing list for your project first to determine how they
1572 prefer such patches be handled.
1574 [[importing-patches]]
1575 Importing patches to a project
1576 ------------------------------
1578 Git also provides a tool called gitlink:git-am[1] (am stands for
1579 "apply mailbox"), for importing such an emailed series of patches.
1580 Just save all of the patch-containing messages, in order, into a
1581 single mailbox file, say "patches.mbox", then run
1583 -------------------------------------------------
1584 $ git am -3 patches.mbox
1585 -------------------------------------------------
1587 Git will apply each patch in order; if any conflicts are found, it
1588 will stop, and you can fix the conflicts as described in
1589 "<<resolving-a-merge,Resolving a merge>>".  (The "-3" option tells
1590 git to perform a merge; if you would prefer it just to abort and
1591 leave your tree and index untouched, you may omit that option.)
1593 Once the index is updated with the results of the conflict
1594 resolution, instead of creating a new commit, just run
1596 -------------------------------------------------
1597 $ git am --resolved
1598 -------------------------------------------------
1600 and git will create the commit for you and continue applying the
1601 remaining patches from the mailbox.
1603 The final result will be a series of commits, one for each patch in
1604 the original mailbox, with authorship and commit log message each
1605 taken from the message containing each patch.
1607 [[setting-up-a-public-repository]]
1608 Setting up a public repository
1609 ------------------------------
1611 Another way to submit changes to a project is to simply tell the
1612 maintainer of that project to pull from your repository, exactly as
1613 you did in the section "<<getting-updates-with-git-pull, Getting
1614 updates with git pull>>".
1616 If you and maintainer both have accounts on the same machine, then
1617 then you can just pull changes from each other's repositories
1618 directly; note that all of the commands (gitlink:git-clone[1],
1619 git-fetch[1], git-pull[1], etc.) that accept a URL as an argument
1620 will also accept a local directory name; so, for example, you can
1621 use
1623 -------------------------------------------------
1624 $ git clone /path/to/repository
1625 $ git pull /path/to/other/repository
1626 -------------------------------------------------
1628 If this sort of setup is inconvenient or impossible, another (more
1629 common) option is to set up a public repository on a public server.
1630 This also allows you to cleanly separate private work in progress
1631 from publicly visible work.
1633 You will continue to do your day-to-day work in your personal
1634 repository, but periodically "push" changes from your personal
1635 repository into your public repository, allowing other developers to
1636 pull from that repository.  So the flow of changes, in a situation
1637 where there is one other developer with a public repository, looks
1638 like this:
1640                         you push
1641   your personal repo ------------------> your public repo
1642         ^                                     |
1643         |                                     |
1644         | you pull                            | they pull
1645         |                                     |
1646         |                                     |
1647         |               they push             V
1648   their public repo <------------------- their repo
1650 Now, assume your personal repository is in the directory ~/proj.  We
1651 first create a new clone of the repository:
1653 -------------------------------------------------
1654 $ git clone --bare ~/proj proj.git
1655 -------------------------------------------------
1657 The resulting directory proj.git contains a "bare" git repository--it is
1658 just the contents of the ".git" directory, without a checked-out copy of
1659 a working directory.
1661 Next, copy proj.git to the server where you plan to host the
1662 public repository.  You can use scp, rsync, or whatever is most
1663 convenient.
1665 If somebody else maintains the public server, they may already have
1666 set up a git service for you, and you may skip to the section
1667 "<<pushing-changes-to-a-public-repository,Pushing changes to a public
1668 repository>>", below.
1670 Otherwise, the following sections explain how to export your newly
1671 created public repository:
1673 [[exporting-via-http]]
1674 Exporting a git repository via http
1675 -----------------------------------
1677 The git protocol gives better performance and reliability, but on a
1678 host with a web server set up, http exports may be simpler to set up.
1680 All you need to do is place the newly created bare git repository in
1681 a directory that is exported by the web server, and make some
1682 adjustments to give web clients some extra information they need:
1684 -------------------------------------------------
1685 $ mv proj.git /home/you/public_html/proj.git
1686 $ cd proj.git
1687 $ git --bare update-server-info
1688 $ chmod a+x hooks/post-update
1689 -------------------------------------------------
1691 (For an explanation of the last two lines, see
1692 gitlink:git-update-server-info[1], and the documentation
1693 link:hooks.txt[Hooks used by git].)
1695 Advertise the url of proj.git.  Anybody else should then be able to
1696 clone or pull from that url, for example with a commandline like:
1698 -------------------------------------------------
1699 $ git clone http://yourserver.com/~you/proj.git
1700 -------------------------------------------------
1702 (See also
1703 link:howto/setup-git-server-over-http.txt[setup-git-server-over-http]
1704 for a slightly more sophisticated setup using WebDAV which also
1705 allows pushing over http.)
1707 [[exporting-via-git]]
1708 Exporting a git repository via the git protocol
1709 -----------------------------------------------
1711 This is the preferred method.
1713 For now, we refer you to the gitlink:git-daemon[1] man page for
1714 instructions.  (See especially the examples section.)
1716 [[pushing-changes-to-a-public-repository]]
1717 Pushing changes to a public repository
1718 --------------------------------------
1720 Note that the two techniques outline above (exporting via
1721 <<exporting-via-http,http>> or <<exporting-via-git,git>>) allow other
1722 maintainers to fetch your latest changes, but they do not allow write
1723 access, which you will need to update the public repository with the
1724 latest changes created in your private repository.
1726 The simplest way to do this is using gitlink:git-push[1] and ssh; to
1727 update the remote branch named "master" with the latest state of your
1728 branch named "master", run
1730 -------------------------------------------------
1731 $ git push ssh://yourserver.com/~you/proj.git master:master
1732 -------------------------------------------------
1734 or just
1736 -------------------------------------------------
1737 $ git push ssh://yourserver.com/~you/proj.git master
1738 -------------------------------------------------
1740 As with git-fetch, git-push will complain if this does not result in
1741 a <<fast-forwards,fast forward>>.  Normally this is a sign of
1742 something wrong.  However, if you are sure you know what you're
1743 doing, you may force git-push to perform the update anyway by
1744 proceeding the branch name by a plus sign:
1746 -------------------------------------------------
1747 $ git push ssh://yourserver.com/~you/proj.git +master
1748 -------------------------------------------------
1750 As with git-fetch, you may also set up configuration options to
1751 save typing; so, for example, after
1753 -------------------------------------------------
1754 $ cat >>.git/config <<EOF
1755 [remote "public-repo"]
1756         url = ssh://yourserver.com/~you/proj.git
1757 EOF
1758 -------------------------------------------------
1760 you should be able to perform the above push with just
1762 -------------------------------------------------
1763 $ git push public-repo master
1764 -------------------------------------------------
1766 See the explanations of the remote.<name>.url, branch.<name>.remote,
1767 and remote.<name>.push options in gitlink:git-config[1] for
1768 details.
1770 [[setting-up-a-shared-repository]]
1771 Setting up a shared repository
1772 ------------------------------
1774 Another way to collaborate is by using a model similar to that
1775 commonly used in CVS, where several developers with special rights
1776 all push to and pull from a single shared repository.  See
1777 link:cvs-migration.txt[git for CVS users] for instructions on how to
1778 set this up.
1780 [[setting-up-gitweb]]
1781 Allow web browsing of a repository
1782 ----------------------------------
1784 The gitweb cgi script provides users an easy way to browse your
1785 project's files and history without having to install git; see the file
1786 gitweb/INSTALL in the git source tree for instructions on setting it up.
1788 [[sharing-development-examples]]
1789 Examples
1790 --------
1792 TODO: topic branches, typical roles as in everyday.txt, ?
1795 [[cleaning-up-history]]
1796 Rewriting history and maintaining patch series
1797 ==============================================
1799 Normally commits are only added to a project, never taken away or
1800 replaced.  Git is designed with this assumption, and violating it will
1801 cause git's merge machinery (for example) to do the wrong thing.
1803 However, there is a situation in which it can be useful to violate this
1804 assumption.
1806 [[patch-series]]
1807 Creating the perfect patch series
1808 ---------------------------------
1810 Suppose you are a contributor to a large project, and you want to add a
1811 complicated feature, and to present it to the other developers in a way
1812 that makes it easy for them to read your changes, verify that they are
1813 correct, and understand why you made each change.
1815 If you present all of your changes as a single patch (or commit), they
1816 may find that it is too much to digest all at once.
1818 If you present them with the entire history of your work, complete with
1819 mistakes, corrections, and dead ends, they may be overwhelmed.
1821 So the ideal is usually to produce a series of patches such that:
1823         1. Each patch can be applied in order.
1825         2. Each patch includes a single logical change, together with a
1826            message explaining the change.
1828         3. No patch introduces a regression: after applying any initial
1829            part of the series, the resulting project still compiles and
1830            works, and has no bugs that it didn't have before.
1832         4. The complete series produces the same end result as your own
1833            (probably much messier!) development process did.
1835 We will introduce some tools that can help you do this, explain how to
1836 use them, and then explain some of the problems that can arise because
1837 you are rewriting history.
1839 [[using-git-rebase]]
1840 Keeping a patch series up to date using git-rebase
1841 --------------------------------------------------
1843 Suppose that you create a branch "mywork" on a remote-tracking branch
1844 "origin", and create some commits on top of it:
1846 -------------------------------------------------
1847 $ git checkout -b mywork origin
1848 $ vi file.txt
1849 $ git commit
1850 $ vi otherfile.txt
1851 $ git commit
1852 ...
1853 -------------------------------------------------
1855 You have performed no merges into mywork, so it is just a simple linear
1856 sequence of patches on top of "origin":
1858 ................................................
1859  o--o--o <-- origin
1860         \
1861          o--o--o <-- mywork
1862 ................................................
1864 Some more interesting work has been done in the upstream project, and
1865 "origin" has advanced:
1867 ................................................
1868  o--o--O--o--o--o <-- origin
1869         \
1870          a--b--c <-- mywork
1871 ................................................
1873 At this point, you could use "pull" to merge your changes back in;
1874 the result would create a new merge commit, like this:
1876 ................................................
1877  o--o--O--o--o--o <-- origin
1878         \        \
1879          a--b--c--m <-- mywork
1880 ................................................
1881  
1882 However, if you prefer to keep the history in mywork a simple series of
1883 commits without any merges, you may instead choose to use
1884 gitlink:git-rebase[1]:
1886 -------------------------------------------------
1887 $ git checkout mywork
1888 $ git rebase origin
1889 -------------------------------------------------
1891 This will remove each of your commits from mywork, temporarily saving
1892 them as patches (in a directory named ".dotest"), update mywork to
1893 point at the latest version of origin, then apply each of the saved
1894 patches to the new mywork.  The result will look like:
1897 ................................................
1898  o--o--O--o--o--o <-- origin
1899                  \
1900                   a'--b'--c' <-- mywork
1901 ................................................
1903 In the process, it may discover conflicts.  In that case it will stop
1904 and allow you to fix the conflicts; after fixing conflicts, use "git
1905 add" to update the index with those contents, and then, instead of
1906 running git-commit, just run
1908 -------------------------------------------------
1909 $ git rebase --continue
1910 -------------------------------------------------
1912 and git will continue applying the rest of the patches.
1914 At any point you may use the --abort option to abort this process and
1915 return mywork to the state it had before you started the rebase:
1917 -------------------------------------------------
1918 $ git rebase --abort
1919 -------------------------------------------------
1921 [[modifying-one-commit]]
1922 Modifying a single commit
1923 -------------------------
1925 We saw in <<fixing-a-mistake-by-editing-history>> that you can replace the
1926 most recent commit using
1928 -------------------------------------------------
1929 $ git commit --amend
1930 -------------------------------------------------
1932 which will replace the old commit by a new commit incorporating your
1933 changes, giving you a chance to edit the old commit message first.
1935 You can also use a combination of this and gitlink:git-rebase[1] to edit
1936 commits further back in your history.  First, tag the problematic commit with
1938 -------------------------------------------------
1939 $ git tag bad mywork~5
1940 -------------------------------------------------
1942 (Either gitk or git-log may be useful for finding the commit.)
1944 Then check out that commit, edit it, and rebase the rest of the series
1945 on top of it (note that we could check out the commit on a temporary
1946 branch, but instead we're using a <<detached-head,detached head>>):
1948 -------------------------------------------------
1949 $ git checkout bad
1950 $ # make changes here and update the index
1951 $ git commit --amend
1952 $ git rebase --onto HEAD bad mywork
1953 -------------------------------------------------
1955 When you're done, you'll be left with mywork checked out, with the top
1956 patches on mywork reapplied on top of your modified commit.  You can
1957 then clean up with
1959 -------------------------------------------------
1960 $ git tag -d bad
1961 -------------------------------------------------
1963 Note that the immutable nature of git history means that you haven't really
1964 "modified" existing commits; instead, you have replaced the old commits with
1965 new commits having new object names.
1967 [[reordering-patch-series]]
1968 Reordering or selecting from a patch series
1969 -------------------------------------------
1971 Given one existing commit, the gitlink:git-cherry-pick[1] command
1972 allows you to apply the change introduced by that commit and create a
1973 new commit that records it.  So, for example, if "mywork" points to a
1974 series of patches on top of "origin", you might do something like:
1976 -------------------------------------------------
1977 $ git checkout -b mywork-new origin
1978 $ gitk origin..mywork &
1979 -------------------------------------------------
1981 And browse through the list of patches in the mywork branch using gitk,
1982 applying them (possibly in a different order) to mywork-new using
1983 cherry-pick, and possibly modifying them as you go using commit
1984 --amend.
1986 Another technique is to use git-format-patch to create a series of
1987 patches, then reset the state to before the patches:
1989 -------------------------------------------------
1990 $ git format-patch origin
1991 $ git reset --hard origin
1992 -------------------------------------------------
1994 Then modify, reorder, or eliminate patches as preferred before applying
1995 them again with gitlink:git-am[1].
1997 [[patch-series-tools]]
1998 Other tools
1999 -----------
2001 There are numerous other tools, such as stgit, which exist for the
2002 purpose of maintaining a patch series.  These are outside of the scope of
2003 this manual.
2005 [[problems-with-rewriting-history]]
2006 Problems with rewriting history
2007 -------------------------------
2009 The primary problem with rewriting the history of a branch has to do
2010 with merging.  Suppose somebody fetches your branch and merges it into
2011 their branch, with a result something like this:
2013 ................................................
2014  o--o--O--o--o--o <-- origin
2015         \        \
2016          t--t--t--m <-- their branch:
2017 ................................................
2019 Then suppose you modify the last three commits:
2021 ................................................
2022          o--o--o <-- new head of origin
2023         /
2024  o--o--O--o--o--o <-- old head of origin
2025 ................................................
2027 If we examined all this history together in one repository, it will
2028 look like:
2030 ................................................
2031          o--o--o <-- new head of origin
2032         /
2033  o--o--O--o--o--o <-- old head of origin
2034         \        \
2035          t--t--t--m <-- their branch:
2036 ................................................
2038 Git has no way of knowing that the new head is an updated version of
2039 the old head; it treats this situation exactly the same as it would if
2040 two developers had independently done the work on the old and new heads
2041 in parallel.  At this point, if someone attempts to merge the new head
2042 in to their branch, git will attempt to merge together the two (old and
2043 new) lines of development, instead of trying to replace the old by the
2044 new.  The results are likely to be unexpected.
2046 You may still choose to publish branches whose history is rewritten,
2047 and it may be useful for others to be able to fetch those branches in
2048 order to examine or test them, but they should not attempt to pull such
2049 branches into their own work.
2051 For true distributed development that supports proper merging,
2052 published branches should never be rewritten.
2054 [[advanced-branch-management]]
2055 Advanced branch management
2056 ==========================
2058 [[fetching-individual-branches]]
2059 Fetching individual branches
2060 ----------------------------
2062 Instead of using gitlink:git-remote[1], you can also choose just
2063 to update one branch at a time, and to store it locally under an
2064 arbitrary name:
2066 -------------------------------------------------
2067 $ git fetch origin todo:my-todo-work
2068 -------------------------------------------------
2070 The first argument, "origin", just tells git to fetch from the
2071 repository you originally cloned from.  The second argument tells git
2072 to fetch the branch named "todo" from the remote repository, and to
2073 store it locally under the name refs/heads/my-todo-work.
2075 You can also fetch branches from other repositories; so
2077 -------------------------------------------------
2078 $ git fetch git://example.com/proj.git master:example-master
2079 -------------------------------------------------
2081 will create a new branch named "example-master" and store in it the
2082 branch named "master" from the repository at the given URL.  If you
2083 already have a branch named example-master, it will attempt to
2084 <<fast-forwards,fast-forward>> to the commit given by example.com's
2085 master branch.  In more detail:
2087 [[fetch-fast-forwards]]
2088 git fetch and fast-forwards
2089 ---------------------------
2091 In the previous example, when updating an existing branch, "git
2092 fetch" checks to make sure that the most recent commit on the remote
2093 branch is a descendant of the most recent commit on your copy of the
2094 branch before updating your copy of the branch to point at the new
2095 commit.  Git calls this process a <<fast-forwards,fast forward>>.
2097 A fast forward looks something like this:
2099 ................................................
2100  o--o--o--o <-- old head of the branch
2101            \
2102             o--o--o <-- new head of the branch
2103 ................................................
2106 In some cases it is possible that the new head will *not* actually be
2107 a descendant of the old head.  For example, the developer may have
2108 realized she made a serious mistake, and decided to backtrack,
2109 resulting in a situation like:
2111 ................................................
2112  o--o--o--o--a--b <-- old head of the branch
2113            \
2114             o--o--o <-- new head of the branch
2115 ................................................
2117 In this case, "git fetch" will fail, and print out a warning.
2119 In that case, you can still force git to update to the new head, as
2120 described in the following section.  However, note that in the
2121 situation above this may mean losing the commits labeled "a" and "b",
2122 unless you've already created a reference of your own pointing to
2123 them.
2125 [[forcing-fetch]]
2126 Forcing git fetch to do non-fast-forward updates
2127 ------------------------------------------------
2129 If git fetch fails because the new head of a branch is not a
2130 descendant of the old head, you may force the update with:
2132 -------------------------------------------------
2133 $ git fetch git://example.com/proj.git +master:refs/remotes/example/master
2134 -------------------------------------------------
2136 Note the addition of the "+" sign.  Alternatively, you can use the "-f"
2137 flag to force updates of all the fetched branches, as in:
2139 -------------------------------------------------
2140 $ git fetch -f origin
2141 -------------------------------------------------
2143 Be aware that commits that the old version of example/master pointed at
2144 may be lost, as we saw in the previous section.
2146 [[remote-branch-configuration]]
2147 Configuring remote branches
2148 ---------------------------
2150 We saw above that "origin" is just a shortcut to refer to the
2151 repository that you originally cloned from.  This information is
2152 stored in git configuration variables, which you can see using
2153 gitlink:git-config[1]:
2155 -------------------------------------------------
2156 $ git config -l
2157 core.repositoryformatversion=0
2158 core.filemode=true
2159 core.logallrefupdates=true
2160 remote.origin.url=git://git.kernel.org/pub/scm/git/git.git
2161 remote.origin.fetch=+refs/heads/*:refs/remotes/origin/*
2162 branch.master.remote=origin
2163 branch.master.merge=refs/heads/master
2164 -------------------------------------------------
2166 If there are other repositories that you also use frequently, you can
2167 create similar configuration options to save typing; for example,
2168 after
2170 -------------------------------------------------
2171 $ git config remote.example.url git://example.com/proj.git
2172 -------------------------------------------------
2174 then the following two commands will do the same thing:
2176 -------------------------------------------------
2177 $ git fetch git://example.com/proj.git master:refs/remotes/example/master
2178 $ git fetch example master:refs/remotes/example/master
2179 -------------------------------------------------
2181 Even better, if you add one more option:
2183 -------------------------------------------------
2184 $ git config remote.example.fetch master:refs/remotes/example/master
2185 -------------------------------------------------
2187 then the following commands will all do the same thing:
2189 -------------------------------------------------
2190 $ git fetch git://example.com/proj.git master:refs/remotes/example/master
2191 $ git fetch example master:refs/remotes/example/master
2192 $ git fetch example
2193 -------------------------------------------------
2195 You can also add a "+" to force the update each time:
2197 -------------------------------------------------
2198 $ git config remote.example.fetch +master:ref/remotes/example/master
2199 -------------------------------------------------
2201 Don't do this unless you're sure you won't mind "git fetch" possibly
2202 throwing away commits on mybranch.
2204 Also note that all of the above configuration can be performed by
2205 directly editing the file .git/config instead of using
2206 gitlink:git-config[1].
2208 See gitlink:git-config[1] for more details on the configuration
2209 options mentioned above.
2212 [[git-internals]]
2213 Git internals
2214 =============
2216 Git depends on two fundamental abstractions: the "object database", and
2217 the "current directory cache" aka "index".
2219 [[the-object-database]]
2220 The Object Database
2221 -------------------
2223 The object database is literally just a content-addressable collection
2224 of objects.  All objects are named by their content, which is
2225 approximated by the SHA1 hash of the object itself.  Objects may refer
2226 to other objects (by referencing their SHA1 hash), and so you can
2227 build up a hierarchy of objects.
2229 All objects have a statically determined "type" which is
2230 determined at object creation time, and which identifies the format of
2231 the object (i.e. how it is used, and how it can refer to other
2232 objects).  There are currently four different object types: "blob",
2233 "tree", "commit", and "tag".
2235 A <<def_blob_object,"blob" object>> cannot refer to any other object,
2236 and is, as the name implies, a pure storage object containing some
2237 user data.  It is used to actually store the file data, i.e. a blob
2238 object is associated with some particular version of some file.
2240 A <<def_tree_object,"tree" object>> is an object that ties one or more
2241 "blob" objects into a directory structure. In addition, a tree object
2242 can refer to other tree objects, thus creating a directory hierarchy.
2244 A <<def_commit_object,"commit" object>> ties such directory hierarchies
2245 together into a <<def_DAG,directed acyclic graph>> of revisions - each
2246 "commit" is associated with exactly one tree (the directory hierarchy at
2247 the time of the commit). In addition, a "commit" refers to one or more
2248 "parent" commit objects that describe the history of how we arrived at
2249 that directory hierarchy.
2251 As a special case, a commit object with no parents is called the "root"
2252 commit, and is the point of an initial project commit.  Each project
2253 must have at least one root, and while you can tie several different
2254 root objects together into one project by creating a commit object which
2255 has two or more separate roots as its ultimate parents, that's probably
2256 just going to confuse people.  So aim for the notion of "one root object
2257 per project", even if git itself does not enforce that. 
2259 A <<def_tag_object,"tag" object>> symbolically identifies and can be
2260 used to sign other objects. It contains the identifier and type of
2261 another object, a symbolic name (of course!) and, optionally, a
2262 signature.
2264 Regardless of object type, all objects share the following
2265 characteristics: they are all deflated with zlib, and have a header
2266 that not only specifies their type, but also provides size information
2267 about the data in the object.  It's worth noting that the SHA1 hash
2268 that is used to name the object is the hash of the original data
2269 plus this header, so `sha1sum` 'file' does not match the object name
2270 for 'file'.
2271 (Historical note: in the dawn of the age of git the hash
2272 was the sha1 of the 'compressed' object.)
2274 As a result, the general consistency of an object can always be tested
2275 independently of the contents or the type of the object: all objects can
2276 be validated by verifying that (a) their hashes match the content of the
2277 file and (b) the object successfully inflates to a stream of bytes that
2278 forms a sequence of <ascii type without space> + <space> + <ascii decimal
2279 size> + <byte\0> + <binary object data>. 
2281 The structured objects can further have their structure and
2282 connectivity to other objects verified. This is generally done with
2283 the `git-fsck` program, which generates a full dependency graph
2284 of all objects, and verifies their internal consistency (in addition
2285 to just verifying their superficial consistency through the hash).
2287 The object types in some more detail:
2289 [[blob-object]]
2290 Blob Object
2291 -----------
2293 A "blob" object is nothing but a binary blob of data, and doesn't
2294 refer to anything else.  There is no signature or any other
2295 verification of the data, so while the object is consistent (it 'is'
2296 indexed by its sha1 hash, so the data itself is certainly correct), it
2297 has absolutely no other attributes.  No name associations, no
2298 permissions.  It is purely a blob of data (i.e. normally "file
2299 contents").
2301 In particular, since the blob is entirely defined by its data, if two
2302 files in a directory tree (or in multiple different versions of the
2303 repository) have the same contents, they will share the same blob
2304 object. The object is totally independent of its location in the
2305 directory tree, and renaming a file does not change the object that
2306 file is associated with in any way.
2308 A blob is typically created when gitlink:git-update-index[1]
2309 is run, and its data can be accessed by gitlink:git-cat-file[1].
2311 [[tree-object]]
2312 Tree Object
2313 -----------
2315 The next hierarchical object type is the "tree" object.  A tree object
2316 is a list of mode/name/blob data, sorted by name.  Alternatively, the
2317 mode data may specify a directory mode, in which case instead of
2318 naming a blob, that name is associated with another TREE object.
2320 Like the "blob" object, a tree object is uniquely determined by the
2321 set contents, and so two separate but identical trees will always
2322 share the exact same object. This is true at all levels, i.e. it's
2323 true for a "leaf" tree (which does not refer to any other trees, only
2324 blobs) as well as for a whole subdirectory.
2326 For that reason a "tree" object is just a pure data abstraction: it
2327 has no history, no signatures, no verification of validity, except
2328 that since the contents are again protected by the hash itself, we can
2329 trust that the tree is immutable and its contents never change.
2331 So you can trust the contents of a tree to be valid, the same way you
2332 can trust the contents of a blob, but you don't know where those
2333 contents 'came' from.
2335 Side note on trees: since a "tree" object is a sorted list of
2336 "filename+content", you can create a diff between two trees without
2337 actually having to unpack two trees.  Just ignore all common parts,
2338 and your diff will look right.  In other words, you can effectively
2339 (and efficiently) tell the difference between any two random trees by
2340 O(n) where "n" is the size of the difference, rather than the size of
2341 the tree.
2343 Side note 2 on trees: since the name of a "blob" depends entirely and
2344 exclusively on its contents (i.e. there are no names or permissions
2345 involved), you can see trivial renames or permission changes by
2346 noticing that the blob stayed the same.  However, renames with data
2347 changes need a smarter "diff" implementation.
2349 A tree is created with gitlink:git-write-tree[1] and
2350 its data can be accessed by gitlink:git-ls-tree[1].
2351 Two trees can be compared with gitlink:git-diff-tree[1].
2353 [[commit-object]]
2354 Commit Object
2355 -------------
2357 The "commit" object is an object that introduces the notion of
2358 history into the picture.  In contrast to the other objects, it
2359 doesn't just describe the physical state of a tree, it describes how
2360 we got there, and why.
2362 A "commit" is defined by the tree-object that it results in, the
2363 parent commits (zero, one or more) that led up to that point, and a
2364 comment on what happened.  Again, a commit is not trusted per se:
2365 the contents are well-defined and "safe" due to the cryptographically
2366 strong signatures at all levels, but there is no reason to believe
2367 that the tree is "good" or that the merge information makes sense.
2368 The parents do not have to actually have any relationship with the
2369 result, for example.
2371 Note on commits: unlike some SCM's, commits do not contain
2372 rename information or file mode change information.  All of that is
2373 implicit in the trees involved (the result tree, and the result trees
2374 of the parents), and describing that makes no sense in this idiotic
2375 file manager.
2377 A commit is created with gitlink:git-commit-tree[1] and
2378 its data can be accessed by gitlink:git-cat-file[1].
2380 [[trust]]
2381 Trust
2382 -----
2384 An aside on the notion of "trust". Trust is really outside the scope
2385 of "git", but it's worth noting a few things.  First off, since
2386 everything is hashed with SHA1, you 'can' trust that an object is
2387 intact and has not been messed with by external sources.  So the name
2388 of an object uniquely identifies a known state - just not a state that
2389 you may want to trust.
2391 Furthermore, since the SHA1 signature of a commit refers to the
2392 SHA1 signatures of the tree it is associated with and the signatures
2393 of the parent, a single named commit specifies uniquely a whole set
2394 of history, with full contents.  You can't later fake any step of the
2395 way once you have the name of a commit.
2397 So to introduce some real trust in the system, the only thing you need
2398 to do is to digitally sign just 'one' special note, which includes the
2399 name of a top-level commit.  Your digital signature shows others
2400 that you trust that commit, and the immutability of the history of
2401 commits tells others that they can trust the whole history.
2403 In other words, you can easily validate a whole archive by just
2404 sending out a single email that tells the people the name (SHA1 hash)
2405 of the top commit, and digitally sign that email using something
2406 like GPG/PGP.
2408 To assist in this, git also provides the tag object...
2410 [[tag-object]]
2411 Tag Object
2412 ----------
2414 Git provides the "tag" object to simplify creating, managing and
2415 exchanging symbolic and signed tokens.  The "tag" object at its
2416 simplest simply symbolically identifies another object by containing
2417 the sha1, type and symbolic name.
2419 However it can optionally contain additional signature information
2420 (which git doesn't care about as long as there's less than 8k of
2421 it). This can then be verified externally to git.
2423 Note that despite the tag features, "git" itself only handles content
2424 integrity; the trust framework (and signature provision and
2425 verification) has to come from outside.
2427 A tag is created with gitlink:git-mktag[1],
2428 its data can be accessed by gitlink:git-cat-file[1],
2429 and the signature can be verified by
2430 gitlink:git-verify-tag[1].
2433 [[the-index]]
2434 The "index" aka "Current Directory Cache"
2435 -----------------------------------------
2437 The index is a simple binary file, which contains an efficient
2438 representation of the contents of a virtual directory.  It
2439 does so by a simple array that associates a set of names, dates,
2440 permissions and content (aka "blob") objects together.  The cache is
2441 always kept ordered by name, and names are unique (with a few very
2442 specific rules) at any point in time, but the cache has no long-term
2443 meaning, and can be partially updated at any time.
2445 In particular, the index certainly does not need to be consistent with
2446 the current directory contents (in fact, most operations will depend on
2447 different ways to make the index 'not' be consistent with the directory
2448 hierarchy), but it has three very important attributes:
2450 '(a) it can re-generate the full state it caches (not just the
2451 directory structure: it contains pointers to the "blob" objects so
2452 that it can regenerate the data too)'
2454 As a special case, there is a clear and unambiguous one-way mapping
2455 from a current directory cache to a "tree object", which can be
2456 efficiently created from just the current directory cache without
2457 actually looking at any other data.  So a directory cache at any one
2458 time uniquely specifies one and only one "tree" object (but has
2459 additional data to make it easy to match up that tree object with what
2460 has happened in the directory)
2462 '(b) it has efficient methods for finding inconsistencies between that
2463 cached state ("tree object waiting to be instantiated") and the
2464 current state.'
2466 '(c) it can additionally efficiently represent information about merge
2467 conflicts between different tree objects, allowing each pathname to be
2468 associated with sufficient information about the trees involved that
2469 you can create a three-way merge between them.'
2471 Those are the ONLY three things that the directory cache does.  It's a
2472 cache, and the normal operation is to re-generate it completely from a
2473 known tree object, or update/compare it with a live tree that is being
2474 developed.  If you blow the directory cache away entirely, you generally
2475 haven't lost any information as long as you have the name of the tree
2476 that it described. 
2478 At the same time, the index is at the same time also the
2479 staging area for creating new trees, and creating a new tree always
2480 involves a controlled modification of the index file.  In particular,
2481 the index file can have the representation of an intermediate tree that
2482 has not yet been instantiated.  So the index can be thought of as a
2483 write-back cache, which can contain dirty information that has not yet
2484 been written back to the backing store.
2488 [[the-workflow]]
2489 The Workflow
2490 ------------
2492 Generally, all "git" operations work on the index file. Some operations
2493 work *purely* on the index file (showing the current state of the
2494 index), but most operations move data to and from the index file. Either
2495 from the database or from the working directory. Thus there are four
2496 main combinations: 
2498 [[working-directory-to-index]]
2499 working directory -> index
2500 ~~~~~~~~~~~~~~~~~~~~~~~~~~
2502 You update the index with information from the working directory with
2503 the gitlink:git-update-index[1] command.  You
2504 generally update the index information by just specifying the filename
2505 you want to update, like so:
2507 -------------------------------------------------
2508 $ git-update-index filename
2509 -------------------------------------------------
2511 but to avoid common mistakes with filename globbing etc, the command
2512 will not normally add totally new entries or remove old entries,
2513 i.e. it will normally just update existing cache entries.
2515 To tell git that yes, you really do realize that certain files no
2516 longer exist, or that new files should be added, you
2517 should use the `--remove` and `--add` flags respectively.
2519 NOTE! A `--remove` flag does 'not' mean that subsequent filenames will
2520 necessarily be removed: if the files still exist in your directory
2521 structure, the index will be updated with their new status, not
2522 removed. The only thing `--remove` means is that update-cache will be
2523 considering a removed file to be a valid thing, and if the file really
2524 does not exist any more, it will update the index accordingly.
2526 As a special case, you can also do `git-update-index --refresh`, which
2527 will refresh the "stat" information of each index to match the current
2528 stat information. It will 'not' update the object status itself, and
2529 it will only update the fields that are used to quickly test whether
2530 an object still matches its old backing store object.
2532 [[index-to-object-database]]
2533 index -> object database
2534 ~~~~~~~~~~~~~~~~~~~~~~~~
2536 You write your current index file to a "tree" object with the program
2538 -------------------------------------------------
2539 $ git-write-tree
2540 -------------------------------------------------
2542 that doesn't come with any options - it will just write out the
2543 current index into the set of tree objects that describe that state,
2544 and it will return the name of the resulting top-level tree. You can
2545 use that tree to re-generate the index at any time by going in the
2546 other direction:
2548 [[object-database-to-index]]
2549 object database -> index
2550 ~~~~~~~~~~~~~~~~~~~~~~~~
2552 You read a "tree" file from the object database, and use that to
2553 populate (and overwrite - don't do this if your index contains any
2554 unsaved state that you might want to restore later!) your current
2555 index.  Normal operation is just
2557 -------------------------------------------------
2558 $ git-read-tree <sha1 of tree>
2559 -------------------------------------------------
2561 and your index file will now be equivalent to the tree that you saved
2562 earlier. However, that is only your 'index' file: your working
2563 directory contents have not been modified.
2565 [[index-to-working-directory]]
2566 index -> working directory
2567 ~~~~~~~~~~~~~~~~~~~~~~~~~~
2569 You update your working directory from the index by "checking out"
2570 files. This is not a very common operation, since normally you'd just
2571 keep your files updated, and rather than write to your working
2572 directory, you'd tell the index files about the changes in your
2573 working directory (i.e. `git-update-index`).
2575 However, if you decide to jump to a new version, or check out somebody
2576 else's version, or just restore a previous tree, you'd populate your
2577 index file with read-tree, and then you need to check out the result
2578 with
2580 -------------------------------------------------
2581 $ git-checkout-index filename
2582 -------------------------------------------------
2584 or, if you want to check out all of the index, use `-a`.
2586 NOTE! git-checkout-index normally refuses to overwrite old files, so
2587 if you have an old version of the tree already checked out, you will
2588 need to use the "-f" flag ('before' the "-a" flag or the filename) to
2589 'force' the checkout.
2592 Finally, there are a few odds and ends which are not purely moving
2593 from one representation to the other:
2595 [[tying-it-all-together]]
2596 Tying it all together
2597 ~~~~~~~~~~~~~~~~~~~~~
2599 To commit a tree you have instantiated with "git-write-tree", you'd
2600 create a "commit" object that refers to that tree and the history
2601 behind it - most notably the "parent" commits that preceded it in
2602 history.
2604 Normally a "commit" has one parent: the previous state of the tree
2605 before a certain change was made. However, sometimes it can have two
2606 or more parent commits, in which case we call it a "merge", due to the
2607 fact that such a commit brings together ("merges") two or more
2608 previous states represented by other commits.
2610 In other words, while a "tree" represents a particular directory state
2611 of a working directory, a "commit" represents that state in "time",
2612 and explains how we got there.
2614 You create a commit object by giving it the tree that describes the
2615 state at the time of the commit, and a list of parents:
2617 -------------------------------------------------
2618 $ git-commit-tree <tree> -p <parent> [-p <parent2> ..]
2619 -------------------------------------------------
2621 and then giving the reason for the commit on stdin (either through
2622 redirection from a pipe or file, or by just typing it at the tty).
2624 git-commit-tree will return the name of the object that represents
2625 that commit, and you should save it away for later use. Normally,
2626 you'd commit a new `HEAD` state, and while git doesn't care where you
2627 save the note about that state, in practice we tend to just write the
2628 result to the file pointed at by `.git/HEAD`, so that we can always see
2629 what the last committed state was.
2631 Here is an ASCII art by Jon Loeliger that illustrates how
2632 various pieces fit together.
2634 ------------
2636                      commit-tree
2637                       commit obj
2638                        +----+
2639                        |    |
2640                        |    |
2641                        V    V
2642                     +-----------+
2643                     | Object DB |
2644                     |  Backing  |
2645                     |   Store   |
2646                     +-----------+
2647                        ^
2648            write-tree  |     |
2649              tree obj  |     |
2650                        |     |  read-tree
2651                        |     |  tree obj
2652                              V
2653                     +-----------+
2654                     |   Index   |
2655                     |  "cache"  |
2656                     +-----------+
2657          update-index  ^
2658              blob obj  |     |
2659                        |     |
2660     checkout-index -u  |     |  checkout-index
2661              stat      |     |  blob obj
2662                              V
2663                     +-----------+
2664                     |  Working  |
2665                     | Directory |
2666                     +-----------+
2668 ------------
2671 [[examining-the-data]]
2672 Examining the data
2673 ------------------
2675 You can examine the data represented in the object database and the
2676 index with various helper tools. For every object, you can use
2677 gitlink:git-cat-file[1] to examine details about the
2678 object:
2680 -------------------------------------------------
2681 $ git-cat-file -t <objectname>
2682 -------------------------------------------------
2684 shows the type of the object, and once you have the type (which is
2685 usually implicit in where you find the object), you can use
2687 -------------------------------------------------
2688 $ git-cat-file blob|tree|commit|tag <objectname>
2689 -------------------------------------------------
2691 to show its contents. NOTE! Trees have binary content, and as a result
2692 there is a special helper for showing that content, called
2693 `git-ls-tree`, which turns the binary content into a more easily
2694 readable form.
2696 It's especially instructive to look at "commit" objects, since those
2697 tend to be small and fairly self-explanatory. In particular, if you
2698 follow the convention of having the top commit name in `.git/HEAD`,
2699 you can do
2701 -------------------------------------------------
2702 $ git-cat-file commit HEAD
2703 -------------------------------------------------
2705 to see what the top commit was.
2707 [[merging-multiple-trees]]
2708 Merging multiple trees
2709 ----------------------
2711 Git helps you do a three-way merge, which you can expand to n-way by
2712 repeating the merge procedure arbitrary times until you finally
2713 "commit" the state.  The normal situation is that you'd only do one
2714 three-way merge (two parents), and commit it, but if you like to, you
2715 can do multiple parents in one go.
2717 To do a three-way merge, you need the two sets of "commit" objects
2718 that you want to merge, use those to find the closest common parent (a
2719 third "commit" object), and then use those commit objects to find the
2720 state of the directory ("tree" object) at these points.
2722 To get the "base" for the merge, you first look up the common parent
2723 of two commits with
2725 -------------------------------------------------
2726 $ git-merge-base <commit1> <commit2>
2727 -------------------------------------------------
2729 which will return you the commit they are both based on.  You should
2730 now look up the "tree" objects of those commits, which you can easily
2731 do with (for example)
2733 -------------------------------------------------
2734 $ git-cat-file commit <commitname> | head -1
2735 -------------------------------------------------
2737 since the tree object information is always the first line in a commit
2738 object.
2740 Once you know the three trees you are going to merge (the one "original"
2741 tree, aka the common tree, and the two "result" trees, aka the branches
2742 you want to merge), you do a "merge" read into the index. This will
2743 complain if it has to throw away your old index contents, so you should
2744 make sure that you've committed those - in fact you would normally
2745 always do a merge against your last commit (which should thus match what
2746 you have in your current index anyway).
2748 To do the merge, do
2750 -------------------------------------------------
2751 $ git-read-tree -m -u <origtree> <yourtree> <targettree>
2752 -------------------------------------------------
2754 which will do all trivial merge operations for you directly in the
2755 index file, and you can just write the result out with
2756 `git-write-tree`.
2759 [[merging-multiple-trees-2]]
2760 Merging multiple trees, continued
2761 ---------------------------------
2763 Sadly, many merges aren't trivial. If there are files that have
2764 been added.moved or removed, or if both branches have modified the
2765 same file, you will be left with an index tree that contains "merge
2766 entries" in it. Such an index tree can 'NOT' be written out to a tree
2767 object, and you will have to resolve any such merge clashes using
2768 other tools before you can write out the result.
2770 You can examine such index state with `git-ls-files --unmerged`
2771 command.  An example:
2773 ------------------------------------------------
2774 $ git-read-tree -m $orig HEAD $target
2775 $ git-ls-files --unmerged
2776 100644 263414f423d0e4d70dae8fe53fa34614ff3e2860 1       hello.c
2777 100644 06fa6a24256dc7e560efa5687fa84b51f0263c3a 2       hello.c
2778 100644 cc44c73eb783565da5831b4d820c962954019b69 3       hello.c
2779 ------------------------------------------------
2781 Each line of the `git-ls-files --unmerged` output begins with
2782 the blob mode bits, blob SHA1, 'stage number', and the
2783 filename.  The 'stage number' is git's way to say which tree it
2784 came from: stage 1 corresponds to `$orig` tree, stage 2 `HEAD`
2785 tree, and stage3 `$target` tree.
2787 Earlier we said that trivial merges are done inside
2788 `git-read-tree -m`.  For example, if the file did not change
2789 from `$orig` to `HEAD` nor `$target`, or if the file changed
2790 from `$orig` to `HEAD` and `$orig` to `$target` the same way,
2791 obviously the final outcome is what is in `HEAD`.  What the
2792 above example shows is that file `hello.c` was changed from
2793 `$orig` to `HEAD` and `$orig` to `$target` in a different way.
2794 You could resolve this by running your favorite 3-way merge
2795 program, e.g.  `diff3`, `merge`, or git's own merge-file, on
2796 the blob objects from these three stages yourself, like this:
2798 ------------------------------------------------
2799 $ git-cat-file blob 263414f... >hello.c~1
2800 $ git-cat-file blob 06fa6a2... >hello.c~2
2801 $ git-cat-file blob cc44c73... >hello.c~3
2802 $ git merge-file hello.c~2 hello.c~1 hello.c~3
2803 ------------------------------------------------
2805 This would leave the merge result in `hello.c~2` file, along
2806 with conflict markers if there are conflicts.  After verifying
2807 the merge result makes sense, you can tell git what the final
2808 merge result for this file is by:
2810 -------------------------------------------------
2811 $ mv -f hello.c~2 hello.c
2812 $ git-update-index hello.c
2813 -------------------------------------------------
2815 When a path is in unmerged state, running `git-update-index` for
2816 that path tells git to mark the path resolved.
2818 The above is the description of a git merge at the lowest level,
2819 to help you understand what conceptually happens under the hood.
2820 In practice, nobody, not even git itself, uses three `git-cat-file`
2821 for this.  There is `git-merge-index` program that extracts the
2822 stages to temporary files and calls a "merge" script on it:
2824 -------------------------------------------------
2825 $ git-merge-index git-merge-one-file hello.c
2826 -------------------------------------------------
2828 and that is what higher level `git merge -s resolve` is implemented with.
2830 [[pack-files]]
2831 How git stores objects efficiently: pack files
2832 ----------------------------------------------
2834 We've seen how git stores each object in a file named after the
2835 object's SHA1 hash.
2837 Unfortunately this system becomes inefficient once a project has a
2838 lot of objects.  Try this on an old project:
2840 ------------------------------------------------
2841 $ git count-objects
2842 6930 objects, 47620 kilobytes
2843 ------------------------------------------------
2845 The first number is the number of objects which are kept in
2846 individual files.  The second is the amount of space taken up by
2847 those "loose" objects.
2849 You can save space and make git faster by moving these loose objects in
2850 to a "pack file", which stores a group of objects in an efficient
2851 compressed format; the details of how pack files are formatted can be
2852 found in link:technical/pack-format.txt[technical/pack-format.txt].
2854 To put the loose objects into a pack, just run git repack:
2856 ------------------------------------------------
2857 $ git repack
2858 Generating pack...
2859 Done counting 6020 objects.
2860 Deltifying 6020 objects.
2861  100% (6020/6020) done
2862 Writing 6020 objects.
2863  100% (6020/6020) done
2864 Total 6020, written 6020 (delta 4070), reused 0 (delta 0)
2865 Pack pack-3e54ad29d5b2e05838c75df582c65257b8d08e1c created.
2866 ------------------------------------------------
2868 You can then run
2870 ------------------------------------------------
2871 $ git prune
2872 ------------------------------------------------
2874 to remove any of the "loose" objects that are now contained in the
2875 pack.  This will also remove any unreferenced objects (which may be
2876 created when, for example, you use "git reset" to remove a commit).
2877 You can verify that the loose objects are gone by looking at the
2878 .git/objects directory or by running
2880 ------------------------------------------------
2881 $ git count-objects
2882 0 objects, 0 kilobytes
2883 ------------------------------------------------
2885 Although the object files are gone, any commands that refer to those
2886 objects will work exactly as they did before.
2888 The gitlink:git-gc[1] command performs packing, pruning, and more for
2889 you, so is normally the only high-level command you need.
2891 [[dangling-objects]]
2892 Dangling objects
2893 ----------------
2895 The gitlink:git-fsck[1] command will sometimes complain about dangling
2896 objects.  They are not a problem.
2898 The most common cause of dangling objects is that you've rebased a
2899 branch, or you have pulled from somebody else who rebased a branch--see
2900 <<cleaning-up-history>>.  In that case, the old head of the original
2901 branch still exists, as does everything it pointed to. The branch
2902 pointer itself just doesn't, since you replaced it with another one.
2904 There are also other situations that cause dangling objects. For
2905 example, a "dangling blob" may arise because you did a "git add" of a
2906 file, but then, before you actually committed it and made it part of the
2907 bigger picture, you changed something else in that file and committed
2908 that *updated* thing - the old state that you added originally ends up
2909 not being pointed to by any commit or tree, so it's now a dangling blob
2910 object.
2912 Similarly, when the "recursive" merge strategy runs, and finds that
2913 there are criss-cross merges and thus more than one merge base (which is
2914 fairly unusual, but it does happen), it will generate one temporary
2915 midway tree (or possibly even more, if you had lots of criss-crossing
2916 merges and more than two merge bases) as a temporary internal merge
2917 base, and again, those are real objects, but the end result will not end
2918 up pointing to them, so they end up "dangling" in your repository.
2920 Generally, dangling objects aren't anything to worry about. They can
2921 even be very useful: if you screw something up, the dangling objects can
2922 be how you recover your old tree (say, you did a rebase, and realized
2923 that you really didn't want to - you can look at what dangling objects
2924 you have, and decide to reset your head to some old dangling state).
2926 For commits, you can just use:
2928 ------------------------------------------------
2929 $ gitk <dangling-commit-sha-goes-here> --not --all
2930 ------------------------------------------------
2932 This asks for all the history reachable from the given commit but not
2933 from any branch, tag, or other reference.  If you decide it's something
2934 you want, you can always create a new reference to it, e.g.,
2936 ------------------------------------------------
2937 $ git branch recovered-branch <dangling-commit-sha-goes-here>
2938 ------------------------------------------------
2940 For blobs and trees, you can't do the same, but you can still examine
2941 them.  You can just do
2943 ------------------------------------------------
2944 $ git show <dangling-blob/tree-sha-goes-here>
2945 ------------------------------------------------
2947 to show what the contents of the blob were (or, for a tree, basically
2948 what the "ls" for that directory was), and that may give you some idea
2949 of what the operation was that left that dangling object.
2951 Usually, dangling blobs and trees aren't very interesting. They're
2952 almost always the result of either being a half-way mergebase (the blob
2953 will often even have the conflict markers from a merge in it, if you
2954 have had conflicting merges that you fixed up by hand), or simply
2955 because you interrupted a "git fetch" with ^C or something like that,
2956 leaving _some_ of the new objects in the object database, but just
2957 dangling and useless.
2959 Anyway, once you are sure that you're not interested in any dangling 
2960 state, you can just prune all unreachable objects:
2962 ------------------------------------------------
2963 $ git prune
2964 ------------------------------------------------
2966 and they'll be gone. But you should only run "git prune" on a quiescent
2967 repository - it's kind of like doing a filesystem fsck recovery: you
2968 don't want to do that while the filesystem is mounted.
2970 (The same is true of "git-fsck" itself, btw - but since 
2971 git-fsck never actually *changes* the repository, it just reports 
2972 on what it found, git-fsck itself is never "dangerous" to run. 
2973 Running it while somebody is actually changing the repository can cause 
2974 confusing and scary messages, but it won't actually do anything bad. In 
2975 contrast, running "git prune" while somebody is actively changing the 
2976 repository is a *BAD* idea).
2978 [[birdview-on-the-source-code]]
2979 A birds-eye view of Git's source code
2980 -------------------------------------
2982 It is not always easy for new developers to find their way through Git's
2983 source code.  This section gives you a little guidance to show where to
2984 start.
2986 A good place to start is with the contents of the initial commit, with:
2988 ----------------------------------------------------
2989 $ git checkout e83c5163
2990 ----------------------------------------------------
2992 The initial revision lays the foundation for almost everything git has
2993 today, but is small enough to read in one sitting.
2995 Note that terminology has changed since that revision.  For example, the
2996 README in that revision uses the word "changeset" to describe what we
2997 now call a <<def_commit_object,commit>>.
2999 Also, we do not call it "cache" any more, but "index", however, the
3000 file is still called `cache.h`.  Remark: Not much reason to change it now,
3001 especially since there is no good single name for it anyway, because it is
3002 basically _the_ header file which is included by _all_ of Git's C sources.
3004 If you grasp the ideas in that initial commit, you should check out a
3005 more recent version and skim `cache.h`, `object.h` and `commit.h`.
3007 In the early days, Git (in the tradition of UNIX) was a bunch of programs
3008 which were extremely simple, and which you used in scripts, piping the
3009 output of one into another. This turned out to be good for initial
3010 development, since it was easier to test new things.  However, recently
3011 many of these parts have become builtins, and some of the core has been
3012 "libified", i.e. put into libgit.a for performance, portability reasons,
3013 and to avoid code duplication.
3015 By now, you know what the index is (and find the corresponding data
3016 structures in `cache.h`), and that there are just a couple of object types
3017 (blobs, trees, commits and tags) which inherit their common structure from
3018 `struct object`, which is their first member (and thus, you can cast e.g.
3019 `(struct object *)commit` to achieve the _same_ as `&commit->object`, i.e.
3020 get at the object name and flags).
3022 Now is a good point to take a break to let this information sink in.
3024 Next step: get familiar with the object naming.  Read <<naming-commits>>.
3025 There are quite a few ways to name an object (and not only revisions!).
3026 All of these are handled in `sha1_name.c`. Just have a quick look at
3027 the function `get_sha1()`. A lot of the special handling is done by
3028 functions like `get_sha1_basic()` or the likes.
3030 This is just to get you into the groove for the most libified part of Git:
3031 the revision walker.
3033 Basically, the initial version of `git log` was a shell script:
3035 ----------------------------------------------------------------
3036 $ git-rev-list --pretty $(git-rev-parse --default HEAD "$@") | \
3037         LESS=-S ${PAGER:-less}
3038 ----------------------------------------------------------------
3040 What does this mean?
3042 `git-rev-list` is the original version of the revision walker, which
3043 _always_ printed a list of revisions to stdout.  It is still functional,
3044 and needs to, since most new Git programs start out as scripts using
3045 `git-rev-list`.
3047 `git-rev-parse` is not as important any more; it was only used to filter out
3048 options that were relevant for the different plumbing commands that were
3049 called by the script.
3051 Most of what `git-rev-list` did is contained in `revision.c` and
3052 `revision.h`.  It wraps the options in a struct named `rev_info`, which
3053 controls how and what revisions are walked, and more.
3055 The original job of `git-rev-parse` is now taken by the function
3056 `setup_revisions()`, which parses the revisions and the common command line
3057 options for the revision walker. This information is stored in the struct
3058 `rev_info` for later consumption. You can do your own command line option
3059 parsing after calling `setup_revisions()`. After that, you have to call
3060 `prepare_revision_walk()` for initialization, and then you can get the
3061 commits one by one with the function `get_revision()`.
3063 If you are interested in more details of the revision walking process,
3064 just have a look at the first implementation of `cmd_log()`; call
3065 `git-show v1.3.0~155^2~4` and scroll down to that function (note that you
3066 no longer need to call `setup_pager()` directly).
3068 Nowadays, `git log` is a builtin, which means that it is _contained_ in the
3069 command `git`.  The source side of a builtin is
3071 - a function called `cmd_<bla>`, typically defined in `builtin-<bla>.c`,
3072   and declared in `builtin.h`,
3074 - an entry in the `commands[]` array in `git.c`, and
3076 - an entry in `BUILTIN_OBJECTS` in the `Makefile`.
3078 Sometimes, more than one builtin is contained in one source file.  For
3079 example, `cmd_whatchanged()` and `cmd_log()` both reside in `builtin-log.c`,
3080 since they share quite a bit of code.  In that case, the commands which are
3081 _not_ named like the `.c` file in which they live have to be listed in
3082 `BUILT_INS` in the `Makefile`.
3084 `git log` looks more complicated in C than it does in the original script,
3085 but that allows for a much greater flexibility and performance.
3087 Here again it is a good point to take a pause.
3089 Lesson three is: study the code.  Really, it is the best way to learn about
3090 the organization of Git (after you know the basic concepts).
3092 So, think about something which you are interested in, say, "how can I
3093 access a blob just knowing the object name of it?".  The first step is to
3094 find a Git command with which you can do it.  In this example, it is either
3095 `git show` or `git cat-file`.
3097 For the sake of clarity, let's stay with `git cat-file`, because it
3099 - is plumbing, and
3101 - was around even in the initial commit (it literally went only through
3102   some 20 revisions as `cat-file.c`, was renamed to `builtin-cat-file.c`
3103   when made a builtin, and then saw less than 10 versions).
3105 So, look into `builtin-cat-file.c`, search for `cmd_cat_file()` and look what
3106 it does.
3108 ------------------------------------------------------------------
3109         git_config(git_default_config);
3110         if (argc != 3)
3111                 usage("git-cat-file [-t|-s|-e|-p|<type>] <sha1>");
3112         if (get_sha1(argv[2], sha1))
3113                 die("Not a valid object name %s", argv[2]);
3114 ------------------------------------------------------------------
3116 Let's skip over the obvious details; the only really interesting part
3117 here is the call to `get_sha1()`.  It tries to interpret `argv[2]` as an
3118 object name, and if it refers to an object which is present in the current
3119 repository, it writes the resulting SHA-1 into the variable `sha1`.
3121 Two things are interesting here:
3123 - `get_sha1()` returns 0 on _success_.  This might surprise some new
3124   Git hackers, but there is a long tradition in UNIX to return different
3125   negative numbers in case of different errors -- and 0 on success.
3127 - the variable `sha1` in the function signature of `get_sha1()` is `unsigned
3128   char \*`, but is actually expected to be a pointer to `unsigned
3129   char[20]`.  This variable will contain the 160-bit SHA-1 of the given
3130   commit.  Note that whenever a SHA-1 is passed as `unsigned char \*`, it
3131   is the binary representation, as opposed to the ASCII representation in
3132   hex characters, which is passed as `char *`.
3134 You will see both of these things throughout the code.
3136 Now, for the meat:
3138 -----------------------------------------------------------------------------
3139         case 0:
3140                 buf = read_object_with_reference(sha1, argv[1], &size, NULL);
3141 -----------------------------------------------------------------------------
3143 This is how you read a blob (actually, not only a blob, but any type of
3144 object).  To know how the function `read_object_with_reference()` actually
3145 works, find the source code for it (something like `git grep
3146 read_object_with | grep ":[a-z]"` in the git repository), and read
3147 the source.
3149 To find out how the result can be used, just read on in `cmd_cat_file()`:
3151 -----------------------------------
3152         write_or_die(1, buf, size);
3153 -----------------------------------
3155 Sometimes, you do not know where to look for a feature.  In many such cases,
3156 it helps to search through the output of `git log`, and then `git show` the
3157 corresponding commit.
3159 Example: If you know that there was some test case for `git bundle`, but
3160 do not remember where it was (yes, you _could_ `git grep bundle t/`, but that
3161 does not illustrate the point!):
3163 ------------------------
3164 $ git log --no-merges t/
3165 ------------------------
3167 In the pager (`less`), just search for "bundle", go a few lines back,
3168 and see that it is in commit 18449ab0...  Now just copy this object name,
3169 and paste it into the command line
3171 -------------------
3172 $ git show 18449ab0
3173 -------------------
3175 Voila.
3177 Another example: Find out what to do in order to make some script a
3178 builtin:
3180 -------------------------------------------------
3181 $ git log --no-merges --diff-filter=A builtin-*.c
3182 -------------------------------------------------
3184 You see, Git is actually the best tool to find out about the source of Git
3185 itself!
3187 [[glossary]]
3188 include::glossary.txt[]
3190 [[git-quick-start]]
3191 Appendix A: Git Quick Start
3192 ===========================
3194 This is a quick summary of the major commands; the following chapters
3195 will explain how these work in more detail.
3197 [[quick-creating-a-new-repository]]
3198 Creating a new repository
3199 -------------------------
3201 From a tarball:
3203 -----------------------------------------------
3204 $ tar xzf project.tar.gz
3205 $ cd project
3206 $ git init
3207 Initialized empty Git repository in .git/
3208 $ git add .
3209 $ git commit
3210 -----------------------------------------------
3212 From a remote repository:
3214 -----------------------------------------------
3215 $ git clone git://example.com/pub/project.git
3216 $ cd project
3217 -----------------------------------------------
3219 [[managing-branches]]
3220 Managing branches
3221 -----------------
3223 -----------------------------------------------
3224 $ git branch         # list all local branches in this repo
3225 $ git checkout test  # switch working directory to branch "test"
3226 $ git branch new     # create branch "new" starting at current HEAD
3227 $ git branch -d new  # delete branch "new"
3228 -----------------------------------------------
3230 Instead of basing new branch on current HEAD (the default), use:
3232 -----------------------------------------------
3233 $ git branch new test    # branch named "test"
3234 $ git branch new v2.6.15 # tag named v2.6.15
3235 $ git branch new HEAD^   # commit before the most recent
3236 $ git branch new HEAD^^  # commit before that
3237 $ git branch new test~10 # ten commits before tip of branch "test"
3238 -----------------------------------------------
3240 Create and switch to a new branch at the same time:
3242 -----------------------------------------------
3243 $ git checkout -b new v2.6.15
3244 -----------------------------------------------
3246 Update and examine branches from the repository you cloned from:
3248 -----------------------------------------------
3249 $ git fetch             # update
3250 $ git branch -r         # list
3251   origin/master
3252   origin/next
3253   ...
3254 $ git checkout -b masterwork origin/master
3255 -----------------------------------------------
3257 Fetch a branch from a different repository, and give it a new
3258 name in your repository:
3260 -----------------------------------------------
3261 $ git fetch git://example.com/project.git theirbranch:mybranch
3262 $ git fetch git://example.com/project.git v2.6.15:mybranch
3263 -----------------------------------------------
3265 Keep a list of repositories you work with regularly:
3267 -----------------------------------------------
3268 $ git remote add example git://example.com/project.git
3269 $ git remote                    # list remote repositories
3270 example
3271 origin
3272 $ git remote show example       # get details
3273 * remote example
3274   URL: git://example.com/project.git
3275   Tracked remote branches
3276     master next ...
3277 $ git fetch example             # update branches from example
3278 $ git branch -r                 # list all remote branches
3279 -----------------------------------------------
3282 [[exploring-history]]
3283 Exploring history
3284 -----------------
3286 -----------------------------------------------
3287 $ gitk                      # visualize and browse history
3288 $ git log                   # list all commits
3289 $ git log src/              # ...modifying src/
3290 $ git log v2.6.15..v2.6.16  # ...in v2.6.16, not in v2.6.15
3291 $ git log master..test      # ...in branch test, not in branch master
3292 $ git log test..master      # ...in branch master, but not in test
3293 $ git log test...master     # ...in one branch, not in both
3294 $ git log -S'foo()'         # ...where difference contain "foo()"
3295 $ git log --since="2 weeks ago"
3296 $ git log -p                # show patches as well
3297 $ git show                  # most recent commit
3298 $ git diff v2.6.15..v2.6.16 # diff between two tagged versions
3299 $ git diff v2.6.15..HEAD    # diff with current head
3300 $ git grep "foo()"          # search working directory for "foo()"
3301 $ git grep v2.6.15 "foo()"  # search old tree for "foo()"
3302 $ git show v2.6.15:a.txt    # look at old version of a.txt
3303 -----------------------------------------------
3305 Search for regressions:
3307 -----------------------------------------------
3308 $ git bisect start
3309 $ git bisect bad                # current version is bad
3310 $ git bisect good v2.6.13-rc2   # last known good revision
3311 Bisecting: 675 revisions left to test after this
3312                                 # test here, then:
3313 $ git bisect good               # if this revision is good, or
3314 $ git bisect bad                # if this revision is bad.
3315                                 # repeat until done.
3316 -----------------------------------------------
3318 [[making-changes]]
3319 Making changes
3320 --------------
3322 Make sure git knows who to blame:
3324 ------------------------------------------------
3325 $ cat >>~/.gitconfig <<\EOF
3326 [user]
3327         name = Your Name Comes Here
3328         email = you@yourdomain.example.com
3329 EOF
3330 ------------------------------------------------
3332 Select file contents to include in the next commit, then make the
3333 commit:
3335 -----------------------------------------------
3336 $ git add a.txt    # updated file
3337 $ git add b.txt    # new file
3338 $ git rm c.txt     # old file
3339 $ git commit
3340 -----------------------------------------------
3342 Or, prepare and create the commit in one step:
3344 -----------------------------------------------
3345 $ git commit d.txt # use latest content only of d.txt
3346 $ git commit -a    # use latest content of all tracked files
3347 -----------------------------------------------
3349 [[merging]]
3350 Merging
3351 -------
3353 -----------------------------------------------
3354 $ git merge test   # merge branch "test" into the current branch
3355 $ git pull git://example.com/project.git master
3356                    # fetch and merge in remote branch
3357 $ git pull . test  # equivalent to git merge test
3358 -----------------------------------------------
3360 [[sharing-your-changes]]
3361 Sharing your changes
3362 --------------------
3364 Importing or exporting patches:
3366 -----------------------------------------------
3367 $ git format-patch origin..HEAD # format a patch for each commit
3368                                 # in HEAD but not in origin
3369 $ git am mbox # import patches from the mailbox "mbox"
3370 -----------------------------------------------
3372 Fetch a branch in a different git repository, then merge into the
3373 current branch:
3375 -----------------------------------------------
3376 $ git pull git://example.com/project.git theirbranch
3377 -----------------------------------------------
3379 Store the fetched branch into a local branch before merging into the
3380 current branch:
3382 -----------------------------------------------
3383 $ git pull git://example.com/project.git theirbranch:mybranch
3384 -----------------------------------------------
3386 After creating commits on a local branch, update the remote
3387 branch with your commits:
3389 -----------------------------------------------
3390 $ git push ssh://example.com/project.git mybranch:theirbranch
3391 -----------------------------------------------
3393 When remote and local branch are both named "test":
3395 -----------------------------------------------
3396 $ git push ssh://example.com/project.git test
3397 -----------------------------------------------
3399 Shortcut version for a frequently used remote repository:
3401 -----------------------------------------------
3402 $ git remote add example ssh://example.com/project.git
3403 $ git push example test
3404 -----------------------------------------------
3406 [[repository-maintenance]]
3407 Repository maintenance
3408 ----------------------
3410 Check for corruption:
3412 -----------------------------------------------
3413 $ git fsck
3414 -----------------------------------------------
3416 Recompress, remove unused cruft:
3418 -----------------------------------------------
3419 $ git gc
3420 -----------------------------------------------
3423 [[todo]]
3424 Appendix B: Notes and todo list for this manual
3425 ===============================================
3427 This is a work in progress.
3429 The basic requirements:
3430         - It must be readable in order, from beginning to end, by
3431           someone intelligent with a basic grasp of the unix
3432           commandline, but without any special knowledge of git.  If
3433           necessary, any other prerequisites should be specifically
3434           mentioned as they arise.
3435         - Whenever possible, section headings should clearly describe
3436           the task they explain how to do, in language that requires
3437           no more knowledge than necessary: for example, "importing
3438           patches into a project" rather than "the git-am command"
3440 Think about how to create a clear chapter dependency graph that will
3441 allow people to get to important topics without necessarily reading
3442 everything in between.
3444 Say something about .gitignore.
3446 Scan Documentation/ for other stuff left out; in particular:
3447         howto's
3448         some of technical/?
3449         hooks
3450         list of commands in gitlink:git[1]
3452 Scan email archives for other stuff left out
3454 Scan man pages to see if any assume more background than this manual
3455 provides.
3457 Simplify beginning by suggesting disconnected head instead of
3458 temporary branch creation?
3460 Add more good examples.  Entire sections of just cookbook examples
3461 might be a good idea; maybe make an "advanced examples" section a
3462 standard end-of-chapter section?
3464 Include cross-references to the glossary, where appropriate.
3466 Document shallow clones?  See draft 1.5.0 release notes for some
3467 documentation.
3469 Add a section on working with other version control systems, including
3470 CVS, Subversion, and just imports of series of release tarballs.
3472 More details on gitweb?
3474 Write a chapter on using plumbing and writing scripts.