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