OSDN Git Service

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