Code

18831476cd0507d2dc64ae2f08674345ae48795f
[git.git] / Documentation / user-manual.txt
1 Git User's Manual
2 _________________
4 This manual is designed to be readable by someone with basic unix
5 commandline skills, but no previous knowledge of git.
7 Chapters 1 and 2 explain how to fetch and study a project using git--the
8 tools you'd need to build and test a particular version of a software
9 project, to search for regressions, and so on.
11 Chapter 3 explains how to do development with git and share your progress
12 with others.
14 Further chapters cover more specialized topics.
16 Comprehensive reference documentation is available through the man
17 pages.  For a command such as "git clone", just use
19 ------------------------------------------------
20 $ man git-clone
21 ------------------------------------------------
23 Repositories and Branches
24 =========================
26 How to get a git repository
27 ---------------------------
29 It will be useful to have a git repository to experiment with as you
30 read this manual.
32 The best way to get one is by using the gitlink:git-clone[1] command
33 to download a copy of an existing repository for a project that you
34 are interested in.  If you don't already have a project in mind, here
35 are some interesting examples:
37 ------------------------------------------------
38         # git itself (approx. 10MB download):
39 $ git clone git://git.kernel.org/pub/scm/git/git.git
40         # the linux kernel (approx. 150MB download):
41 $ git clone git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6.git
42 ------------------------------------------------
44 The initial clone may be time-consuming for a large project, but you
45 will only need to clone once.
47 The clone command creates a new directory named after the project
48 ("git" or "linux-2.6" in the examples above).  After you cd into this
49 directory, you will see that it contains a copy of the project files,
50 together with a special top-level directory named ".git", which
51 contains all the information about the history of the project.
53 In the following, examples will be taken from one of the two
54 repositories above.
56 How to check out a different version of a project
57 -------------------------------------------------
59 Git is best thought of as a tool for storing the history of a
60 collection of files.  It stores the history as a compressed
61 collection of interrelated snapshots (versions) of the project's
62 contents.
64 A single git repository may contain multiple branches.  Each branch
65 is a bookmark referencing a particular point in the project history.
66 The gitlink:git-branch[1] command shows you the list of branches:
68 ------------------------------------------------
69 $ git branch
70 * master
71 ------------------------------------------------
73 A freshly cloned repository contains a single branch, named "master",
74 and the working directory contains the version of the project
75 referred to by the master branch.
77 Most projects also use tags.  Tags, like branches, are references
78 into the project's history, and can be listed using the
79 gitlink:git-tag[1] command:
81 ------------------------------------------------
82 $ git tag -l
83 v2.6.11
84 v2.6.11-tree
85 v2.6.12
86 v2.6.12-rc2
87 v2.6.12-rc3
88 v2.6.12-rc4
89 v2.6.12-rc5
90 v2.6.12-rc6
91 v2.6.13
92 ...
93 ------------------------------------------------
95 Create a new branch pointing to one of these versions and check it
96 out using gitlink:git-checkout[1]:
98 ------------------------------------------------
99 $ git checkout -b new v2.6.13
100 ------------------------------------------------
102 The working directory then reflects the contents that the project had
103 when it was tagged v2.6.13, and gitlink:git-branch[1] shows two
104 branches, with an asterisk marking the currently checked-out branch:
106 ------------------------------------------------
107 $ git branch
108   master
109 * new
110 ------------------------------------------------
112 If you decide that you'd rather see version 2.6.17, you can modify
113 the current branch to point at v2.6.17 instead, with
115 ------------------------------------------------
116 $ git reset --hard v2.6.17
117 ------------------------------------------------
119 Note that if the current branch was your only reference to a
120 particular point in history, then resetting that branch may leave you
121 with no way to find the history it used to point to; so use this
122 command carefully.
124 Understanding History: Commits
125 ------------------------------
127 Every change in the history of a project is represented by a commit.
128 The gitlink:git-show[1] command shows the most recent commit on the
129 current branch:
131 ------------------------------------------------
132 $ git show
133 commit 2b5f6dcce5bf94b9b119e9ed8d537098ec61c3d2
134 Author: Jamal Hadi Salim <hadi@cyberus.ca>
135 Date:   Sat Dec 2 22:22:25 2006 -0800
137     [XFRM]: Fix aevent structuring to be more complete.
138     
139     aevents can not uniquely identify an SA. We break the ABI with this
140     patch, but consensus is that since it is not yet utilized by any
141     (known) application then it is fine (better do it now than later).
142     
143     Signed-off-by: Jamal Hadi Salim <hadi@cyberus.ca>
144     Signed-off-by: David S. Miller <davem@davemloft.net>
146 diff --git a/Documentation/networking/xfrm_sync.txt b/Documentation/networking/xfrm_sync.txt
147 index 8be626f..d7aac9d 100644
148 --- a/Documentation/networking/xfrm_sync.txt
149 +++ b/Documentation/networking/xfrm_sync.txt
150 @@ -47,10 +47,13 @@ aevent_id structure looks like:
151  
152     struct xfrm_aevent_id {
153               struct xfrm_usersa_id           sa_id;
154 +             xfrm_address_t                  saddr;
155               __u32                           flags;
156 +             __u32                           reqid;
157     };
158 ...
159 ------------------------------------------------
161 As you can see, a commit shows who made the latest change, what they
162 did, and why.
164 Every commit has a 20-digit id, sometimes called the "SHA1 id", shown
165 on the first line of the "git show" output.  You can usually refer to
166 a commit by a shorter name, such as a tag or a branch name, but this
167 longer id can also be useful.  In particular, it is a globally unique
168 name for this commit: so if you tell somebody else the SHA1 id (for
169 example in email), then you are guaranteed they will see the same
170 commit in their repository that you do in yours.
172 Understanding history: commits, parents, and reachability
173 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
175 Every commit (except the very first commit in a project) also has a
176 parent commit which shows what happened before this commit.
177 Following the chain of parents will eventually take you back to the
178 beginning of the project.
180 However, the commits do not form a simple list; git allows lines of
181 development to diverge and then reconverge, and the point where two
182 lines of development reconverge is called a "merge".  The commit
183 representing a merge can therefore have more than one parent, with
184 each parent representing the most recent commit on one of the lines
185 of development leading to that point.
187 The best way to see how this works is using the gitlink:gitk[1]
188 command; running gitk now on a git repository and looking for merge
189 commits will help understand how the git organizes history.
191 In the following, we say that commit X is "reachable" from commit Y
192 if commit X is an ancestor of commit Y.  Equivalently, you could say
193 that Y is a descendent of X, or that there is a chain of parents
194 leading from commit Y to commit X.
196 Undestanding history: History diagrams
197 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
199 We will sometimes represent git history using diagrams like the one
200 below.  Commits are shown as "o", and the links between them with
201 lines drawn with - / and \.  Time goes left to right:
203          o--o--o <-- Branch A
204         /
205  o--o--o <-- master
206         \
207          o--o--o <-- Branch B
209 If we need to talk about a particular commit, the character "o" may
210 be replaced with another letter or number.
212 Understanding history: What is a branch?
213 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
215 Though we've been using the word "branch" to mean a kind of reference
216 to a particular commit, the word branch is also commonly used to
217 refer to the line of commits leading up to that point.  In the
218 example above, git may think of the branch named "A" as just a
219 pointer to one particular commit, but we may refer informally to the
220 line of three commits leading up to that point as all being part of
221 "branch A".
223 If we need to make it clear that we're just talking about the most
224 recent commit on the branch, we may refer to that commit as the
225 "head" of the branch.
227 Manipulating branches
228 ---------------------
230 Creating, deleting, and modifying branches is quick and easy; here's
231 a summary of the commands:
233 git branch::
234         list all branches
235 git branch <branch>::
236         create a new branch named <branch>, referencing the same
237         point in history as the current branch
238 git branch <branch> <start-point>::
239         create a new branch named <branch>, referencing
240         <start-point>, which may be specified any way you like,
241         including using a branch name or a tag name
242 git branch -d <branch>::
243         delete the branch <branch>; if the branch you are deleting
244         points to a commit which is not reachable from this branch,
245         this command will fail with a warning.
246 git branch -D <branch>::
247         even if the branch points to a commit not reachable
248         from the current branch, you may know that that commit
249         is still reachable from some other branch or tag.  In that
250         case it is safe to use this command to force git to delete
251         the branch.
252 git checkout <branch>::
253         make the current branch <branch>, updating the working
254         directory to reflect the version referenced by <branch>
255 git checkout -b <new> <start-point>::
256         create a new branch <new> referencing <start-point>, and
257         check it out.
259 It is also useful to know that the special symbol "HEAD" can always
260 be used to refer to the current branch.
262 Examining branches from a remote repository
263 -------------------------------------------
265 The "master" branch that was created at the time you cloned is a copy
266 of the HEAD in the repository that you cloned from.  That repository
267 may also have had other branches, though, and your local repository
268 keeps branches which track each of those remote branches, which you
269 can view using the "-r" option to gitlink:git-branch[1]:
271 ------------------------------------------------
272 $ git branch -r
273   origin/HEAD
274   origin/html
275   origin/maint
276   origin/man
277   origin/master
278   origin/next
279   origin/pu
280   origin/todo
281 ------------------------------------------------
283 You cannot check out these remote-tracking branches, but you can
284 examine them on a branch of your own, just as you would a tag:
286 ------------------------------------------------
287 $ git checkout -b my-todo-copy origin/todo
288 ------------------------------------------------
290 Note that the name "origin" is just the name that git uses by default
291 to refer to the repository that you cloned from.
293 [[how-git-stores-references]]
294 How git stores references
295 -------------------------
297 Branches, remote-tracking branches, and tags are all references to
298 commits.  Git stores these references in the ".git" directory.  Most
299 of them are stored in .git/refs/:
301         - branches are stored in .git/refs/heads
302         - tags are stored in .git/refs/tags
303         - remote-tracking branches for "origin" are stored in
304           .git/refs/remotes/origin/
306 If you look at one of these files you will see that they usually
307 contain just the SHA1 id of a commit:
309 ------------------------------------------------
310 $ ls .git/refs/heads/
311 master
312 $ cat .git/refs/heads/master
313 c0f982dcf188d55db9d932a39d4ea7becaa55fed
314 ------------------------------------------------
316 You can refer to a reference by its path relative to the .git
317 directory.  However, we've seen above that git will also accept
318 shorter names; for example, "master" is an acceptable shortcut for
319 "refs/heads/master", and "origin/master" is a shortcut for
320 "refs/remotes/origin/master".
322 As another useful shortcut, you can also refer to the "HEAD" of
323 "origin" (or any other remote), using just the name of the remote.
325 For the complete list of paths which git checks for references, and
326 how it decides which to choose when there are multiple references
327 with the same name, see the "SPECIFYING REVISIONS" section of
328 gitlink:git-rev-parse[1].
330 [[Updating-a-repository-with-git-fetch]]
331 Updating a repository with git fetch
332 ------------------------------------
334 Eventually the developer cloned from will do additional work in her
335 repository, creating new commits and advancing the branches to point
336 at the new commits.
338 The command "git fetch", with no arguments, will update all of the
339 remote-tracking branches to the latest version found in her
340 repository.  It will not touch any of your own branches--not even the
341 "master" branch that was created for you on clone.
343 Fetching individual branches
344 ----------------------------
346 You can also choose to update just one branch at a time:
348 -------------------------------------------------
349 $ git fetch origin todo:refs/remotes/origin/todo
350 -------------------------------------------------
352 The first argument, "origin", just tells git to fetch from the
353 repository you originally cloned from.  The second argument tells git
354 to fetch the branch named "todo" from the remote repository, and to
355 store it locally under the name refs/remotes/origin/todo; as we saw
356 above, remote-tracking branches are stored under
357 refs/remotes/<name-of-repository>/<name-of-branch>.
359 You can also fetch branches from other repositories; so
361 -------------------------------------------------
362 $ git fetch git://example.com/proj.git master:refs/remotes/example/master
363 -------------------------------------------------
365 will create a new reference named "refs/remotes/example/master" and
366 store in it the branch named "master" from the repository at the
367 given URL.  If you already have a branch named
368 "refs/remotes/example/master", it will attempt to "fast-forward" to
369 the commit given by example.com's master branch.  So next we explain
370 what a fast-forward is:
372 [[fast-forwards]]
373 Understanding git history: fast-forwards
374 ----------------------------------------
376 In the previous example, when updating an existing branch, "git
377 fetch" checks to make sure that the most recent commit on the remote
378 branch is a descendant of the most recent commit on your copy of the
379 branch before updating your copy of the branch to point at the new
380 commit.  Git calls this process a "fast forward".
382 A fast forward looks something like this:
384  o--o--o--o <-- old head of the branch
385            \
386             o--o--o <-- new head of the branch
389 In some cases it is possible that the new head will *not* actually be
390 a descendant of the old head.  For example, the developer may have
391 realized she made a serious mistake, and decided to backtrack,
392 resulting in a situation like:
394  o--o--o--o--a--b <-- old head of the branch
395            \
396             o--o--o <-- new head of the branch
400 In this case, "git fetch" will fail, and print out a warning.
402 In that case, you can still force git to update to the new head, as
403 described in the following section.  However, note that in the
404 situation above this may mean losing the commits labeled "a" and "b",
405 unless you've already created a reference of your own pointing to
406 them.
408 Forcing git fetch to do non-fast-forward updates
409 ------------------------------------------------
411 If git fetch fails because the new head of a branch is not a
412 descendant of the old head, you may force the update with:
414 -------------------------------------------------
415 $ git fetch git://example.com/proj.git +master:refs/remotes/example/master
416 -------------------------------------------------
418 Note the addition of the "+" sign.  Be aware that commits which the
419 old version of example/master pointed at may be lost, as we saw in
420 the previous section.
422 Configuring remote branches
423 ---------------------------
425 We saw above that "origin" is just a shortcut to refer to the
426 repository which you originally cloned from.  This information is
427 stored in git configuration variables, which you can see using
428 gitlink:git-repo-config[1]:
430 -------------------------------------------------
431 $ git-repo-config -l
432 core.repositoryformatversion=0
433 core.filemode=true
434 core.logallrefupdates=true
435 remote.origin.url=git://git.kernel.org/pub/scm/git/git.git
436 remote.origin.fetch=+refs/heads/*:refs/remotes/origin/*
437 branch.master.remote=origin
438 branch.master.merge=refs/heads/master
439 -------------------------------------------------
441 If there are other repositories that you also use frequently, you can
442 create similar configuration options to save typing; for example,
443 after
445 -------------------------------------------------
446 $ git repo-config remote.example.url=git://example.com/proj.git
447 -------------------------------------------------
449 then the following two commands will do the same thing:
451 -------------------------------------------------
452 $ git fetch git://example.com/proj.git master:refs/remotes/example/master
453 $ git fetch example master:refs/remotes/example/master
454 -------------------------------------------------
456 Even better, if you add one more option:
458 -------------------------------------------------
459 $ git repo-config remote.example.fetch=master:refs/remotes/example/master
460 -------------------------------------------------
462 then the following commands will all do the same thing:
464 -------------------------------------------------
465 $ git fetch git://example.com/proj.git master:ref/remotes/example/master
466 $ git fetch example master:ref/remotes/example/master
467 $ git fetch example example/master
468 $ git fetch example
469 -------------------------------------------------
471 You can also add a "+" to force the update each time:
473 -------------------------------------------------
474 $ git repo-config +master:ref/remotes/example/master
475 -------------------------------------------------
477 Don't do this unless you're sure you won't mind "git fetch" possibly
478 throwing away commits on mybranch.
480 Also note that all of the above configuration can be performed by
481 directly editing the file .git/config instead of using
482 gitlink:git-repo-config[1].
484 See gitlink:git-repo-config[1] for more details on the configuration
485 options mentioned above.
487 Exploring git history
488 =====================
490 Git is best thought of as a tool for storing the history of a
491 collection of files.  It does this by storing compressed snapshots of
492 the contents of a file heirarchy, together with "commits" which show
493 the relationships between these snapshots.
495 Git provides extremely flexible and fast tools for exploring the
496 history of a project.
498 We start with one specialized tool which is useful for finding the
499 commit that introduced a bug into a project.
501 How to use bisect to find a regression
502 --------------------------------------
504 Suppose version 2.6.18 of your project worked, but the version at
505 "master" crashes.  Sometimes the best way to find the cause of such a
506 regression is to perform a brute-force search through the project's
507 history to find the particular commit that caused the problem.  The
508 gitlink:git-bisect[1] command can help you do this:
510 -------------------------------------------------
511 $ git bisect start
512 $ git bisect good v2.6.18
513 $ git bisect bad master
514 Bisecting: 3537 revisions left to test after this
515 [65934a9a028b88e83e2b0f8b36618fe503349f8e] BLOCK: Make USB storage depend on SCSI rather than selecting it [try #6]
516 -------------------------------------------------
518 If you run "git branch" at this point, you'll see that git has
519 temporarily moved you to a new branch named "bisect".  This branch
520 points to a commit (with commit id 65934...) that is reachable from
521 v2.6.19 but not from v2.6.18.  Compile and test it, and see whether
522 it crashes.  Assume it does crash.  Then:
524 -------------------------------------------------
525 $ git bisect bad
526 Bisecting: 1769 revisions left to test after this
527 [7eff82c8b1511017ae605f0c99ac275a7e21b867] i2c-core: Drop useless bitmaskings
528 -------------------------------------------------
530 checks out an older version.  Continue like this, telling git at each
531 stage whether the version it gives you is good or bad, and notice
532 that the number of revisions left to test is cut approximately in
533 half each time.
535 After about 13 tests (in this case), it will output the commit id of
536 the guilty commit.  You can then examine the commit with
537 gitlink:git-show[1], find out who wrote it, and mail them your bug
538 report with the commit id.  Finally, run
540 -------------------------------------------------
541 $ git bisect reset
542 -------------------------------------------------
544 to return you to the branch you were on before and delete the
545 temporary "bisect" branch.
547 Note that the version which git-bisect checks out for you at each
548 point is just a suggestion, and you're free to try a different
549 version if you think it would be a good idea.  For example,
550 occasionally you may land on a commit that broke something unrelated;
551 run
553 -------------------------------------------------
554 $ git bisect-visualize
555 -------------------------------------------------
557 which will run gitk and label the commit it chose with a marker that
558 says "bisect".  Chose a safe-looking commit nearby, note its commit
559 id, and check it out with:
561 -------------------------------------------------
562 $ git reset --hard fb47ddb2db...
563 -------------------------------------------------
565 then test, run "bisect good" or "bisect bad" as appropriate, and
566 continue.
568 Naming commits
569 --------------
571 We have seen several ways of naming commits already:
573         - 20-digit SHA1 id
574         - branch name: refers to the commit at the head of the given
575           branch
576         - tag name: refers to the commit pointed to by the given tag
577           (we've seen branches and tags are special cases of
578           <<how-git-stores-references,references>>).
579         - HEAD: refers to the head of the current branch
581 There are many more; see the "SPECIFYING REVISION" section of the
582 gitlink:git-rev-list[1] man page for the complete list of ways to
583 name revisions.  Some examples:
585 -------------------------------------------------
586 $ git show fb47ddb2 # the first few characters of the SHA1 id
587                     # are usually enough to specify it uniquely
588 $ git show HEAD^    # the parent of the HEAD commit
589 $ git show HEAD^^   # the grandparent
590 $ git show HEAD~4   # the great-great-grandparent
591 -------------------------------------------------
593 Recall that merge commits may have more than one parent; by default,
594 ^ and ~ follow the first parent listed in the commit, but you can
595 also choose:
597 -------------------------------------------------
598 $ git show HEAD^1   # show the first parent of HEAD
599 $ git show HEAD^2   # show the second parent of HEAD
600 -------------------------------------------------
602 In addition to HEAD, there are several other special names for
603 commits:
605 Merges (to be discussed later), as well as operations such as
606 git-reset, which change the currently checked-out commit, generally
607 set ORIG_HEAD to the value HEAD had before the current operation.
609 The git-fetch operation always stores the head of the last fetched
610 branch in FETCH_HEAD.  For example, if you run git fetch without
611 specifying a local branch as the target of the operation
613 -------------------------------------------------
614 $ git fetch git://example.com/proj.git theirbranch
615 -------------------------------------------------
617 the fetched commits will still be available from FETCH_HEAD.
619 When we discuss merges we'll also see the special name MERGE_HEAD,
620 which refers to the other branch that we're merging in to the current
621 branch.
623 Creating tags
624 -------------
626 We can also create a tag to refer to a particular commit; after
627 running
629 -------------------------------------------------
630 $ git-tag stable-1 1b2e1d63ff
631 -------------------------------------------------
633 You can use stable-1 to refer to the commit 1b2e1d63ff.
635 This creates a "lightweight" tag.  If the tag is a tag you wish to
636 share with others, and possibly sign cryptographically, then you
637 should create a tag object instead; see the gitlink:git-tag[1] man
638 page for details.
640 Browsing revisions
641 ------------------
643 The gitlink:git-log[1] command can show lists of commits.  On its
644 own, it shows all commits reachable from the parent commit; but you
645 can also make more specific requests:
647 -------------------------------------------------
648 $ git log v2.5..        # commits since (not reachable from) v2.5
649 $ git log test..master  # commits reachable from master but not test
650 $ git log master..test  # ...reachable from test but not master
651 $ git log master...test # ...reachable from either test or master,
652                         #    but not both
653 $ git log --since="2 weeks ago" # commits from the last 2 weeks
654 $ git log Makefile      # commits which modify Makefile
655 $ git log fs/           # ... which modify any file under fs/
656 $ git log -S'foo()'     # commits which add or remove any file data
657                         # matching the string 'foo()'
658 -------------------------------------------------
660 And of course you can combine all of these; the following finds
661 commits since v2.5 which touch the Makefile or any file under fs:
663 -------------------------------------------------
664 $ git log v2.5.. Makefile fs/
665 -------------------------------------------------
667 You can also ask git log to show patches:
669 -------------------------------------------------
670 $ git log -p
671 -------------------------------------------------
673 See the "--pretty" option in the gitlink:git-log[1] man page for more
674 display options.
676 Note that git log starts with the most recent commit and works
677 backwards through the parents; however, since git history can contain
678 multiple independant lines of development, the particular order that
679 commits are listed in may be somewhat arbitrary.
681 Generating diffs
682 ----------------
684 You can generate diffs between any two versions using
685 gitlink:git-diff[1]:
687 -------------------------------------------------
688 $ git diff master..test
689 -------------------------------------------------
691 Sometimes what you want instead is a set of patches:
693 -------------------------------------------------
694 $ git format-patch master..test
695 -------------------------------------------------
697 will generate a file with a patch for each commit reachable from test
698 but not from master.  Note that if master also has commits which are
699 not reachable from test, then the combined result of these patches
700 will not be the same as the diff produced by the git-diff example.
702 Viewing old file versions
703 -------------------------
705 You can always view an old version of a file by just checking out the
706 correct revision first.  But sometimes it is more convenient to be
707 able to view an old version of a single file without checking
708 anything out; this command does that:
710 -------------------------------------------------
711 $ git show v2.5:fs/locks.c
712 -------------------------------------------------
714 Before the colon may be anything that names a commit, and after it
715 may be any path to a file tracked by git.
717 Developing with git
718 ===================
720 Telling git your name
721 ---------------------
723 Before creating any commits, you should introduce yourself to git.  The
724 easiest way to do so is:
726 ------------------------------------------------
727 $ cat >~/.gitconfig <<\EOF
728 [user]
729         name = Your Name Comes Here
730         email = you@yourdomain.example.com
731 EOF
732 ------------------------------------------------
735 Creating a new repository
736 -------------------------
738 Creating a new repository from scratch is very easy:
740 -------------------------------------------------
741 $ mkdir project
742 $ cd project
743 $ git init-db
744 -------------------------------------------------
746 If you have some initial content (say, a tarball):
748 -------------------------------------------------
749 $ tar -xzvf project.tar.gz
750 $ cd project
751 $ git init-db
752 $ git add . # include everything below ./ in the first commit:
753 $ git commit
754 -------------------------------------------------
756 [[how-to-make-a-commit]]
757 how to make a commit
758 --------------------
760 Creating a new commit takes three steps:
762         1. Making some changes to the working directory using your
763            favorite editor.
764         2. Telling git about your changes.
765         3. Creating the commit using the content you told git about
766            in step 2.
768 In practice, you can interleave and repeat steps 1 and 2 as many
769 times as you want: in order to keep track of what you want committed
770 at step 3, git maintains a snapshot of the tree's contents in a
771 special staging area called "the index."
773 By default, the content of the index is identical to that of the
774 HEAD.  The command "git diff --cached" shows the difference between
775 HEAD and the index, so you should no output from that command.
777 Modifying the index is easy:
779 To update the index with the new contents of a modified file, use
781 -------------------------------------------------
782 $ git add path/to/file
783 -------------------------------------------------
785 To add the contents of a new file to the index, use
787 -------------------------------------------------
788 $ git add path/to/file
789 -------------------------------------------------
791 To remove a file from the index that you've removed from the working
792 tree,
794 -------------------------------------------------
795 $ git rm path/to/file
796 -------------------------------------------------
798 After each step you can verify that
800 -------------------------------------------------
801 $ git diff --cached
802 -------------------------------------------------
804 always shows the difference between the HEAD and the index file--this
805 is what you'd commit if you created the commit now--and that
807 -------------------------------------------------
808 $ git diff
809 -------------------------------------------------
811 shows the difference between the working tree and the index file.
813 Note that "git add" always adds just the current contents of a file
814 to the index; further changes to the same file will be ignored unless
815 you run git-add on the file again.
817 When you're ready, just run
819 -------------------------------------------------
820 $ git commit
821 -------------------------------------------------
823 and git will prompt you for a commit message and then create the new
824 commmit.  Check to make sure it looks like what you expected with
826 -------------------------------------------------
827 $ git show
828 -------------------------------------------------
830 As a special shortcut,
831                 
832 -------------------------------------------------
833 $ git commit -a
834 -------------------------------------------------
836 will update the index with any files that you've modified or removed
837 and create a commit, all in one step.
839 A number of commands are useful for keeping track of what you're
840 about to commit:
842 -------------------------------------------------
843 $ git diff --cached # difference between HEAD and the index; what
844                     # would be commited if you ran "commit" now.
845 $ git diff          # difference between the index file and your
846                     # working directory; changes that would not
847                     # be included if you ran "commit" now.
848 $ git status        # a brief per-file summary of the above.
849 -------------------------------------------------
851 creating good commit messages
852 -----------------------------
854 Though not required, it's a good idea to begin the commit message
855 with a single short (less than 50 character) line summarizing the
856 change, followed by a blank line and then a more thorough
857 description.  Tools that turn commits into email, for example, use
858 the first line on the Subject line and the rest of the commit in the
859 body.
861 how to merge
862 ------------
864 You can rejoin two diverging branches of development using
865 gitlink:git-merge[1]:
867 -------------------------------------------------
868 $ git merge branchname
869 -------------------------------------------------
871 merges the development in the branch "branchname" into the current
872 branch.  If there are conflicts--for example, if the same file is
873 modified in two different ways in the remote branch and the local
874 branch--then you are warned; the output may look something like this:
876 -------------------------------------------------
877 $ git pull . next
878 Trying really trivial in-index merge...
879 fatal: Merge requires file-level merging
880 Nope.
881 Merging HEAD with 77976da35a11db4580b80ae27e8d65caf5208086
882 Merging:
883 15e2162 world
884 77976da goodbye
885 found 1 common ancestor(s):
886 d122ed4 initial
887 Auto-merging file.txt
888 CONFLICT (content): Merge conflict in file.txt
889 Automatic merge failed; fix conflicts and then commit the result.
890 -------------------------------------------------
892 Conflict markers are left in the problematic files, and after
893 you resolve the conflicts manually, you can update the index
894 with the contents and run git commit, as you normally would when
895 creating a new file.
897 If you examine the resulting commit using gitk, you will see that it
898 has two parents, one pointing to the top of the current branch, and
899 one to the top of the other branch.
901 In more detail:
903 [[resolving-a-merge]]
904 Resolving a merge
905 -----------------
907 When a merge isn't resolved automatically, git leaves the index and
908 the working tree in a special state that gives you all the
909 information you need to help resolve the merge.
911 Files with conflicts are marked specially in the index, so until you
912 resolve the problem and update the index, git commit will fail:
914 -------------------------------------------------
915 $ git commit
916 file.txt: needs merge
917 -------------------------------------------------
919 Also, git status will list those files as "unmerged".
921 All of the changes that git was able to merge automatically are
922 already added to the index file, so gitlink:git-diff[1] shows only
923 the conflicts.  Also, it uses a somewhat unusual syntax:
925 -------------------------------------------------
926 $ git diff
927 diff --cc file.txt
928 index 802992c,2b60207..0000000
929 --- a/file.txt
930 +++ b/file.txt
931 @@@ -1,1 -1,1 +1,5 @@@
932 ++<<<<<<< HEAD:file.txt
933  +Hello world
934 ++=======
935 + Goodbye
936 ++>>>>>>> 77976da35a11db4580b80ae27e8d65caf5208086:file.txt
937 -------------------------------------------------
939 Recall that the commit which will be commited after we resolve this
940 conflict will have two parents instead of the usual one: one parent
941 will be HEAD, the tip of the current branch; the other will be the
942 tip of the other branch, which is stored temporarily in MERGE_HEAD.
944 The diff above shows the differences between the working-tree version
945 of file.txt and two previous version: one version from HEAD, and one
946 from MERGE_HEAD.  So instead of preceding each line by a single "+"
947 or "-", it now uses two columns: the first column is used for
948 differences between the first parent and the working directory copy,
949 and the second for differences between the second parent and the
950 working directory copy.  Thus after resolving the conflict in the
951 obvious way, the diff will look like:
953 -------------------------------------------------
954 $ git diff
955 diff --cc file.txt
956 index 802992c,2b60207..0000000
957 --- a/file.txt
958 +++ b/file.txt
959 @@@ -1,1 -1,1 +1,1 @@@
960 - Hello world
961  -Goodbye
962 ++Goodbye world
963 -------------------------------------------------
965 This shows that our resolved version deleted "Hello world" from the
966 first parent, deleted "Goodbye" from the second parent, and added
967 "Goodbye world", which was previously absent from both.
969 The gitlink:git-log[1] command also provides special help for merges:
971 -------------------------------------------------
972 $ git log --merge
973 -------------------------------------------------
975 This will list all commits which exist only on HEAD or on MERGE_HEAD,
976 and which touch an unmerged file.
978 We can now add the resolved version to the index and commit:
980 -------------------------------------------------
981 $ git add file.txt
982 $ git commit
983 -------------------------------------------------
985 Note that the commit message will already be filled in for you with
986 some information about the merge.  Normally you can just use this
987 default message unchanged, but you may add additional commentary of
988 your own if desired.
990 [[undoing-a-merge]]
991 undoing a merge
992 ---------------
994 If you get stuck and decide to just give up and throw the whole mess
995 away, you can always return to the pre-merge state with
997 -------------------------------------------------
998 $ git reset --hard HEAD
999 -------------------------------------------------
1001 Or, if you've already commited the merge that you want to throw away,
1003 -------------------------------------------------
1004 $ git reset --hard HEAD^
1005 -------------------------------------------------
1007 However, this last command can be dangerous in some cases--never
1008 throw away a commit you have already committed if that commit may
1009 itself have been merged into another branch, as doing so may confuse
1010 further merges.
1012 Fast-forward merges
1013 -------------------
1015 There is one special case not mentioned above, which is treated
1016 differently.  Normally, a merge results in a merge commit, with two
1017 parents, one pointing at each of the two lines of development that
1018 were merged.
1020 However, if one of the two lines of development is completely
1021 contained within the other--so every commit present in the one is
1022 already contained in the other--then git just performs a
1023 <<fast-forwards,fast forward>>; the head of the current branch is
1024 moved forward to point at the head of the merged-in branch, without
1025 any new commits being created.
1027 Ensuring good performance
1028 -------------------------
1030 On large repositories, git depends on compression to keep the history
1031 information from taking up to much space on disk or in memory.
1033 This compression is not performed automatically.  Therefore you
1034 should occasionally run
1036 -------------------------------------------------
1037 $ git gc
1038 -------------------------------------------------
1040 to recompress the archive and to prune any commits which are no
1041 longer referred to anywhere.  This can be very time-consuming, and
1042 you should not modify the repository while it is working, so you
1043 should run it while you are not working.
1045 Sharing development with others
1046 -------------------------------
1048 [[getting-updates-with-git-pull]]
1049 Getting updates with git pull
1050 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1052 After you clone a repository and make a few changes of your own, you
1053 may wish to check the original repository for updates and merge them
1054 into your own work.
1056 We have already seen <<Updating-a-repository-with-git-fetch,how to
1057 keep remote tracking branches up to date>> with gitlink:git-fetch[1],
1058 and how to merge two branches.  So you can merge in changes from the
1059 original repository's master branch with:
1061 -------------------------------------------------
1062 $ git fetch
1063 $ git merge origin/master
1064 -------------------------------------------------
1066 However, the gitlink:git-pull[1] command provides a way to do this in
1067 one step:
1069 -------------------------------------------------
1070 $ git pull origin master
1071 -------------------------------------------------
1073 In fact, "origin" is normally the default repository to pull from,
1074 and the default branch is normally the HEAD of the remote repository,
1075 so often you can accomplish the above with just
1077 -------------------------------------------------
1078 $ git pull
1079 -------------------------------------------------
1081 See the descriptions of the branch.<name>.remote and
1082 branch.<name>.merge options in gitlink:git-repo-config[1] to learn
1083 how to control these defaults depending on the current branch.
1085 In addition to saving you keystrokes, "git pull" also helps you by
1086 producing a default commit message documenting the branch and
1087 repository that you pulled from.
1089 (But note that no such commit will be created in the case of a
1090 <<fast-forwards,fast forward>>; instead, your branch will just be
1091 updated to point to the latest commit from the upstream branch).
1093 Submitting patches to a project
1094 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1096 If you just have a few changes, the simplest way to submit them may
1097 just be to send them as patches in email:
1099 First, use gitlink:git-format-patches[1]; for example:
1101 -------------------------------------------------
1102 $ git format-patches origin
1103 -------------------------------------------------
1105 will produce a numbered series of files in the current directory, one
1106 for each patch in the current branch but not in origin/HEAD.
1108 You can then import these into your mail client and send them by
1109 hand.  However, if you have a lot to send at once, you may prefer to
1110 use the gitlink:git-send-email[1] script to automate the process.
1111 Consult the mailing list for your project first to determine how they
1112 prefer such patches be handled.
1114 Importing patches to a project
1115 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1117 Git also provides a tool called gitlink:git-am[1] (am stands for
1118 "apply mailbox"), for importing such an emailed series of patches.
1119 Just save all of the patch-containing messages, in order, into a
1120 single mailbox file, say "patches.mbox", then run
1122 -------------------------------------------------
1123 $ git am patches.mbox
1124 -------------------------------------------------
1126 Git will apply each patch in order; if any conflicts are found, it
1127 will stop, and you can fix the conflicts as described in
1128 "<<resolving-a-merge,Resolving a merge>>".  Once the index is updated
1129 with the results of the conflict resolution, instead of creating a
1130 new commit, just run
1132 -------------------------------------------------
1133 $ git am --resolved
1134 -------------------------------------------------
1136 and git will create the commit for you and continue applying the
1137 remaining patches from the mailbox.
1139 The final result will be a series of commits, one for each patch in
1140 the original mailbox, with authorship and commit log message each
1141 taken from the message containing each patch.
1143 [[setting-up-a-public-repository]]
1144 Setting up a public repository
1145 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1147 Another way to submit changes to a project is to simply tell the
1148 maintainer of that project to pull from your repository, exactly as
1149 you did in the section "<<getting-updates-with-git-pull, Getting
1150 updates with git pull>>".
1152 If you and maintainer both have accounts on the same machine, then
1153 then you can just pull changes from each other's repositories
1154 directly; note that all of the command (gitlink:git-clone[1],
1155 git-fetch[1], git-pull[1], etc.) which accept a URL as an argument
1156 will also accept a local file patch; so, for example, you can
1157 use
1159 -------------------------------------------------
1160 $ git clone /path/to/repository
1161 $ git pull /path/to/other/repository
1162 -------------------------------------------------
1164 If this sort of setup is inconvenient or impossible, another (more
1165 common) option is to set up a public repository on a public server.
1166 This also allows you to cleanly separate private work in progress
1167 from publicly visible work.
1169 You will continue to do your day-to-day work in your personal
1170 repository, but periodically "push" changes from your personal
1171 repository into your public repository, allowing other developers to
1172 pull from that repository.  So the flow of changes, in a situation
1173 where there is one other developer with a public repository, looks
1174 like this:
1176                         you push
1177   your personal repo ------------------> your public repo
1178         ^                                     |
1179         |                                     |
1180         | you pull                            | they pull
1181         |                                     |
1182         |                                     |
1183         |               they push             V
1184   their public repo <------------------- their repo
1186 Now, assume your personal repository is in the directory ~/proj.  We
1187 first create a new clone of the repository:
1189 -------------------------------------------------
1190 $ git clone --bare proj-clone.git
1191 -------------------------------------------------
1193 The resulting directory proj-clone.git will contains a "bare" git
1194 repository--it is just the contents of the ".git" directory, without
1195 a checked-out copy of a working directory.
1197 Next, copy proj-clone.git to the server where you plan to host the
1198 public repository.  You can use scp, rsync, or whatever is most
1199 convenient.
1201 If somebody else maintains the public server, they may already have
1202 set up a git service for you, and you may skip to the section
1203 "<<pushing-changes-to-a-public-repository,Pushing changes to a public
1204 repository>>", below.
1206 Otherwise, the following sections explain how to export your newly
1207 created public repository:
1209 [[exporting-via-http]]
1210 Exporting a git repository via http
1211 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1213 The git protocol gives better performance and reliability, but on a
1214 host with a web server set up, http exports may be simpler to set up.
1216 All you need to do is place the newly created bare git repository in
1217 a directory that is exported by the web server, and make some
1218 adjustments to give web clients some extra information they need:
1220 -------------------------------------------------
1221 $ mv proj.git /home/you/public_html/proj.git
1222 $ cd proj.git
1223 $ git update-server-info
1224 $ chmod a+x hooks/post-update
1225 -------------------------------------------------
1227 (For an explanation of the last two lines, see
1228 gitlink:git-update-server-info[1], and the documentation
1229 link:hooks.txt[Hooks used by git].)
1231 Advertise the url of proj.git.  Anybody else should then be able to
1232 clone or pull from that url, for example with a commandline like:
1234 -------------------------------------------------
1235 $ git clone http://yourserver.com/~you/proj.git
1236 -------------------------------------------------
1238 (See also
1239 link:howto/setup-git-server-over-http.txt[setup-git-server-over-http]
1240 for a slightly more sophisticated setup using WebDAV which also
1241 allows pushing over http.)
1243 [[exporting-via-git]]
1244 Exporting a git repository via the git protocol
1245 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1247 This is the preferred method.
1249 For now, we refer you to the gitlink:git-daemon[1] man page for
1250 instructions.  (See especially the examples section.)
1252 [[pushing-changes-to-a-public-repository]]
1253 Pushing changes to a public repository
1254 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1256 Note that the two techniques outline above (exporting via
1257 <<exporting-via-http,http>> or <<exporting-via-git,git>>) allow other
1258 maintainers to fetch your latest changes, but they do not allow write
1259 access, which you will need to update the public repository with the
1260 latest changes created in your private repository.
1262 The simplest way to do this is using gitlink:git-push[1] and ssh; to
1263 update the remote branch named "master" with the latest state of your
1264 branch named "master", run
1266 -------------------------------------------------
1267 $ git push ssh://yourserver.com/~you/proj.git master:master
1268 -------------------------------------------------
1270 or just
1272 -------------------------------------------------
1273 $ git push ssh://yourserver.com/~you/proj.git master
1274 -------------------------------------------------
1276 As with git-fetch, git-push will complain if this does not result in
1277 a <<fast-forwards,fast forward>>.  Normally this is a sign of
1278 something wrong.  However, if you are sure you know what you're
1279 doing, you may force git-push to perform the update anyway by
1280 proceeding the branch name by a plus sign:
1282 -------------------------------------------------
1283 $ git push ssh://yourserver.com/~you/proj.git +master
1284 -------------------------------------------------
1286 As with git-fetch, you may also set up configuration options to
1287 save typing; so, for example, after
1289 -------------------------------------------------
1290 $ cat >.git/config <<EOF
1291 [remote "public-repo"]
1292         url = ssh://yourserver.com/~you/proj.git
1293 EOF
1294 -------------------------------------------------
1296 you should be able to perform the above push with just
1298 -------------------------------------------------
1299 $ git push public-repo master
1300 -------------------------------------------------
1302 See the explanations of the remote.<name>.url, branch.<name>.remote,
1303 and remote.<name>.push options in gitlink:git-repo-config[1] for
1304 details.
1306 Setting up a shared repository
1307 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1309 Another way to collaborate is by using a model similar to that
1310 commonly used in CVS, where several developers with special rights
1311 all push to and pull from a single shared repository.  See
1312 link:cvs-migration.txt[git for CVS users] for instructions on how to
1313 set this up.
1315 Fixing mistakes
1316 ---------------
1318 If you've messed up the working tree, but haven't yet committed your
1319 mistake, you can return the entire working tree to the last committed
1320 state with
1322 -------------------------------------------------
1323 $ git reset --hard HEAD
1324 -------------------------------------------------
1326 If you make a commit that you later wish you hadn't, there are two
1327 fundamentally different ways to fix the problem:
1329         1. You can create a new commit that undoes whatever was done
1330         by the previous commit.  This is the correct thing if your
1331         mistake has already been made public.
1333         2. You can go back and modify the old commit.  You should
1334         never do this if you have already made the history public;
1335         git does not normally expect the "history" of a project to
1336         change, and cannot correctly perform repeated merges from
1337         a branch that has had its history changed.
1339 Fixing a mistake with a new commit
1340 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1342 Creating a new commit that reverts an earlier change is very easy;
1343 just pass the gitlink:git-revert[1] command a reference to the bad
1344 commit; for example, to revert the most recent commit:
1346 -------------------------------------------------
1347 $ git revert HEAD
1348 -------------------------------------------------
1350 This will create a new commit which undoes the change in HEAD.  You
1351 will be given a chance to edit the commit message for the new commit.
1353 You can also revert an earlier change, for example, the next-to-last:
1355 -------------------------------------------------
1356 $ git revert HEAD^
1357 -------------------------------------------------
1359 In this case git will attempt to undo the old change while leaving
1360 intact any changes made since then.  If more recent changes overlap
1361 with the changes to be reverted, then you will be asked to fix
1362 conflicts manually, just as in the case of <<resolving-a-merge,
1363 resolving a merge>>.
1365 Fixing a mistake by editing history
1366 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1368 If the problematic commit is the most recent commit, and you have not
1369 yet made that commit public, then you may just
1370 <<undoing-a-merge,destroy it using git-reset>>.
1372 Alternatively, you
1373 can edit the working directory and update the index to fix your
1374 mistake, just as if you were going to <<how-to-make-a-commit,create a
1375 new commit>>, then run
1377 -------------------------------------------------
1378 $ git commit --amend
1379 -------------------------------------------------
1381 which will replace the old commit by a new commit incorporating your
1382 changes, giving you a chance to edit the old commit message first.
1384 Again, you should never do this to a commit that may already have
1385 been merged into another branch; use gitlink:git-revert[1] instead in
1386 that case.
1388 It is also possible to edit commits further back in the history, but
1389 this is an advanced topic to be left for
1390 <<cleaning-up-history,another chapter>>.
1392 Checking out an old version of a file
1393 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1395 In the process of undoing a previous bad change, you may find it
1396 useful to check out an older version of a particular file using
1397 gitlink:git-checkout[1].  We've used git checkout before to switch
1398 branches, but it has quite different behavior if it is given a path
1399 name: the command
1401 -------------------------------------------------
1402 $ git checkout HEAD^ path/to/file
1403 -------------------------------------------------
1405 replaces path/to/file by the contents it had in the commit HEAD^, and
1406 also updates the index to match.  It does not change branches.
1408 If you just want to look at an old version of the file, without
1409 modifying the working directory, you can do that with
1410 gitlink:git-show[1]:
1412 -------------------------------------------------
1413 $ git show HEAD^ path/to/file
1414 -------------------------------------------------
1416 which will display the given version of the file.
1418 Working with other version control systems
1419 ==========================================
1421 TODO: CVS, Subversion, ?
1423 [[cleaning-up-history]]
1424 Cleaning up history: rebasing, cherry-picking, and patch series
1425 ===============================================================
1427 TODO: rebase, cherry-pick, pointers to other tools (like stgit)
1429 Git internals
1430 =============
1432 Architectural overview
1433 ----------------------
1435 TODO: Sources, README, core-tutorial, tutorial-2.txt, technical/
1437 Glossary of git terms
1438 =====================
1440 include::glossary.txt[]
1442 Notes and todo list for this manual
1443 ===================================
1445 This is a work in progress.
1447 The basic requirements:
1448         - It must be readable in order, from beginning to end, by someone
1449           intelligent with a basic grasp of the unix commandline, but
1450           without any special knowledge of git.  If necessary, any other
1451           prerequisites should be specifically mentioned as they arise.
1452         - Whenever possible, section headings should clearly describe the
1453           task they explain how to do, in language that requires no more
1454           knowledge than necessary: for example, "importing patches into a
1455           project" rather than "the git-am command"
1457 Think about how to create a clear chapter dependency graph that will allow
1458 people to get to important topics without necessarily reading everything
1459 in between.
1461 Scan Documentation/ for other stuff left out; in particular:
1462         howto's
1463         README
1464         some of technical/?
1465         hooks
1466         etc.
1468 Scan email archives for other stuff left out
1470 Scan man pages to see if any assume more background than this manual
1471 provides.
1473 Mention of gitweb.
1475 Update git fetch discussion to use "git remote" setup.  That will
1476 make things simpler.  Maybe wait till git remote is done.
1478 Can also simplify beginning by suggesting disconnected head instead
1479 of temporary branch creation.
1481 Explain how to refer to file stages in the "how to resolve a merge"
1482 section: diff -1, -2, -3, --ours, --theirs :1:/path notation.
1484 Include cross-references to the glossary, where appropriate.