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