Add Git official document to help
[TortoiseGit.git] / doc / source / en / TortoiseGit / git_doc / gitcore-tutorial.html.xml
blobfb9a43468c462ffffef111fd60eec6c408072c33
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" id="gitcore-tutorial(7)">\r
5 <articleinfo>\r
6     <title>gitcore-tutorial(7)</title>\r
7         <indexterm>\r
8                 <primary>gitcore-tutorial(7)</primary>\r
9         </indexterm>\r
10 </articleinfo>\r
11 <simplesect id="_name">\r
12 <title>NAME</title>\r
13 <simpara>gitcore-tutorial - A git core tutorial for developers</simpara>\r
14 </simplesect>\r
15 <simplesect id="_synopsis">\r
16 <title>SYNOPSIS</title>\r
17 <simpara>git *</simpara>\r
18 </simplesect>\r
19 <simplesect id="_description">\r
20 <title>DESCRIPTION</title>\r
21 <simpara>This tutorial explains how to use the "core" git programs to set up and\r
22 work with a git repository.</simpara>\r
23 <simpara>If you just need to use git as a revision control system you may prefer\r
24 to start with "A Tutorial Introduction to GIT" (<xref linkend="gittutorial(7)"/>) or\r
25 <ulink url="user-manual.html">the GIT User Manual</ulink>.</simpara>\r
26 <simpara>However, an understanding of these low-level tools can be helpful if\r
27 you want to understand git&#8217;s internals.</simpara>\r
28 <simpara>The core git is often called "plumbing", with the prettier user\r
29 interfaces on top of it called "porcelain". You may not want to use the\r
30 plumbing directly very often, but it can be good to know what the\r
31 plumbing does for when the porcelain isn&#8217;t flushing.</simpara>\r
32 <note><simpara>Deeper technical details are often marked as Notes, which you can\r
33 skip on your first reading.</simpara></note>\r
34 </simplesect>\r
35 <simplesect id="_creating_a_git_repository">\r
36 <title>Creating a git repository</title>\r
37 <simpara>Creating a new git repository couldn&#8217;t be easier: all git repositories start\r
38 out empty, and the only thing you need to do is find yourself a\r
39 subdirectory that you want to use as a working tree - either an empty\r
40 one for a totally new project, or an existing working tree that you want\r
41 to import into git.</simpara>\r
42 <simpara>For our first example, we&#8217;re going to start a totally new repository from\r
43 scratch, with no pre-existing files, and we&#8217;ll call it <emphasis>git-tutorial</emphasis>.\r
44 To start up, create a subdirectory for it, change into that\r
45 subdirectory, and initialize the git infrastructure with <emphasis>git-init</emphasis>:</simpara>\r
46 <literallayout>$ mkdir git-tutorial\r
47 $ cd git-tutorial\r
48 $ git init</literallayout>\r
49 <simpara>to which git will reply</simpara>\r
50 <literallayout>Initialized empty Git repository in .git/</literallayout>\r
51 <simpara>which is just git&#8217;s way of saying that you haven&#8217;t been doing anything\r
52 strange, and that it will have created a local <literal>.git</literal> directory setup for\r
53 your new project. You will now have a <literal>.git</literal> directory, and you can\r
54 inspect that with <emphasis>ls</emphasis>. For your new empty project, it should show you\r
55 three entries, among other things:</simpara>\r
56 <itemizedlist>\r
57 <listitem>\r
58 <simpara>\r
59 a file called <literal>HEAD</literal>, that has <literal>ref: refs/heads/master</literal> in it.\r
60    This is similar to a symbolic link and points at\r
61    <literal>refs/heads/master</literal> relative to the <literal>HEAD</literal> file.\r
62 </simpara>\r
63 <simpara>Don&#8217;t worry about the fact that the file that the <literal>HEAD</literal> link points to\r
64 doesn&#8217;t even exist yet&#8201;&#8212;&#8201;you haven&#8217;t created the commit that will\r
65 start your <literal>HEAD</literal> development branch yet.</simpara>\r
66 </listitem>\r
67 <listitem>\r
68 <simpara>\r
69 a subdirectory called <literal>objects</literal>, which will contain all the\r
70    objects of your project. You should never have any real reason to\r
71    look at the objects directly, but you might want to know that these\r
72    objects are what contains all the real <emphasis>data</emphasis> in your repository.\r
73 </simpara>\r
74 </listitem>\r
75 <listitem>\r
76 <simpara>\r
77 a subdirectory called <literal>refs</literal>, which contains references to objects.\r
78 </simpara>\r
79 </listitem>\r
80 </itemizedlist>\r
81 <simpara>In particular, the <literal>refs</literal> subdirectory will contain two other\r
82 subdirectories, named <literal>heads</literal> and <literal>tags</literal> respectively. They do\r
83 exactly what their names imply: they contain references to any number\r
84 of different <emphasis>heads</emphasis> of development (aka <emphasis>branches</emphasis>), and to any\r
85 <emphasis>tags</emphasis> that you have created to name specific versions in your\r
86 repository.</simpara>\r
87 <simpara>One note: the special <literal>master</literal> head is the default branch, which is\r
88 why the <literal>.git/HEAD</literal> file was created points to it even if it\r
89 doesn&#8217;t yet exist. Basically, the <literal>HEAD</literal> link is supposed to always\r
90 point to the branch you are working on right now, and you always\r
91 start out expecting to work on the <literal>master</literal> branch.</simpara>\r
92 <simpara>However, this is only a convention, and you can name your branches\r
93 anything you want, and don&#8217;t have to ever even <emphasis>have</emphasis> a <literal>master</literal>\r
94 branch. A number of the git tools will assume that <literal>.git/HEAD</literal> is\r
95 valid, though.</simpara>\r
96 <note><simpara>An <emphasis>object</emphasis> is identified by its 160-bit SHA1 hash, aka <emphasis>object name</emphasis>,\r
97 and a reference to an object is always the 40-byte hex\r
98 representation of that SHA1 name. The files in the &#8216;refs`\r
99 subdirectory are expected to contain these hex references\r
100 (usually with a final <literal>&#8217;\n\'</literal> at the end), and you should thus\r
101 expect to see a number of 41-byte files containing these\r
102 references in these <literal>refs</literal> subdirectories when you actually start\r
103 populating your tree.</simpara></note>\r
104 <note><simpara>An advanced user may want to take a look at <xref linkend="gitrepository-layout(5)"/>\r
105 after finishing this tutorial.</simpara></note>\r
106 <simpara>You have now created your first git repository. Of course, since it&#8217;s\r
107 empty, that&#8217;s not very useful, so let&#8217;s start populating it with data.</simpara>\r
108 </simplesect>\r
109 <simplesect id="_populating_a_git_repository">\r
110 <title>Populating a git repository</title>\r
111 <simpara>We&#8217;ll keep this simple and stupid, so we&#8217;ll start off with populating a\r
112 few trivial files just to get a feel for it.</simpara>\r
113 <simpara>Start off with just creating any random files that you want to maintain\r
114 in your git repository. We&#8217;ll start off with a few bad examples, just to\r
115 get a feel for how this works:</simpara>\r
116 <literallayout>$ echo "Hello World" &gt;hello\r
117 $ echo "Silly example" &gt;example</literallayout>\r
118 <simpara>you have now created two files in your working tree (aka <emphasis>working directory</emphasis>),\r
119 but to actually check in your hard work, you will have to go through two steps:</simpara>\r
120 <itemizedlist>\r
121 <listitem>\r
122 <simpara>\r
123 fill in the <emphasis>index</emphasis> file (aka <emphasis>cache</emphasis>) with the information about your\r
124    working tree state.\r
125 </simpara>\r
126 </listitem>\r
127 <listitem>\r
128 <simpara>\r
129 commit that index file as an object.\r
130 </simpara>\r
131 </listitem>\r
132 </itemizedlist>\r
133 <simpara>The first step is trivial: when you want to tell git about any changes\r
134 to your working tree, you use the <emphasis>git-update-index</emphasis> program. That\r
135 program normally just takes a list of filenames you want to update, but\r
136 to avoid trivial mistakes, it refuses to add new entries to the index\r
137 (or remove existing ones) unless you explicitly tell it that you&#8217;re\r
138 adding a new entry with the <literal>--add</literal> flag (or removing an entry with the\r
139 <literal>--remove</literal>) flag.</simpara>\r
140 <simpara>So to populate the index with the two files you just created, you can do</simpara>\r
141 <literallayout>$ git update-index --add hello example</literallayout>\r
142 <simpara>and you have now told git to track those two files.</simpara>\r
143 <simpara>In fact, as you did that, if you now look into your object directory,\r
144 you&#8217;ll notice that git will have added two new objects to the object\r
145 database. If you did exactly the steps above, you should now be able to do</simpara>\r
146 <literallayout>$ ls .git/objects/??/*</literallayout>\r
147 <simpara>and see two files:</simpara>\r
148 <literallayout>.git/objects/55/7db03de997c86a4a028e1ebd3a1ceb225be238\r
149 .git/objects/f2/4c74a2e500f5ee1332c86b94199f52b1d1d962</literallayout>\r
150 <simpara>which correspond with the objects with names of <literal>557db&#8230;</literal> and\r
151 <literal>f24c7&#8230;</literal> respectively.</simpara>\r
152 <simpara>If you want to, you can use <emphasis>git-cat-file</emphasis> to look at those objects, but\r
153 you&#8217;ll have to use the object name, not the filename of the object:</simpara>\r
154 <literallayout>$ git cat-file -t 557db03de997c86a4a028e1ebd3a1ceb225be238</literallayout>\r
155 <simpara>where the <literal>-t</literal> tells <emphasis>git-cat-file</emphasis> to tell you what the "type" of the\r
156 object is. git will tell you that you have a "blob" object (i.e., just a\r
157 regular file), and you can see the contents with</simpara>\r
158 <literallayout>$ git cat-file "blob" 557db03</literallayout>\r
159 <simpara>which will print out "Hello World". The object <literal>557db03</literal> is nothing\r
160 more than the contents of your file <literal>hello</literal>.</simpara>\r
161 <note><simpara>Don&#8217;t confuse that object with the file <literal>hello</literal> itself. The\r
162 object is literally just those specific <emphasis role="strong">contents</emphasis> of the file, and\r
163 however much you later change the contents in file <literal>hello</literal>, the object\r
164 we just looked at will never change. Objects are immutable.</simpara></note>\r
165 <note><simpara>The second example demonstrates that you can\r
166 abbreviate the object name to only the first several\r
167 hexadecimal digits in most places.</simpara></note>\r
168 <simpara>Anyway, as we mentioned previously, you normally never actually take a\r
169 look at the objects themselves, and typing long 40-character hex\r
170 names is not something you&#8217;d normally want to do. The above digression\r
171 was just to show that <emphasis>git-update-index</emphasis> did something magical, and\r
172 actually saved away the contents of your files into the git object\r
173 database.</simpara>\r
174 <simpara>Updating the index did something else too: it created a <literal>.git/index</literal>\r
175 file. This is the index that describes your current working tree, and\r
176 something you should be very aware of. Again, you normally never worry\r
177 about the index file itself, but you should be aware of the fact that\r
178 you have not actually really "checked in" your files into git so far,\r
179 you&#8217;ve only <emphasis role="strong">told</emphasis> git about them.</simpara>\r
180 <simpara>However, since git knows about them, you can now start using some of the\r
181 most basic git commands to manipulate the files or look at their status.</simpara>\r
182 <simpara>In particular, let&#8217;s not even check in the two files into git yet, we&#8217;ll\r
183 start off by adding another line to <literal>hello</literal> first:</simpara>\r
184 <literallayout>$ echo "It's a new day for git" &gt;&gt;hello</literallayout>\r
185 <simpara>and you can now, since you told git about the previous state of <literal>hello</literal>, ask\r
186 git what has changed in the tree compared to your old index, using the\r
187 <emphasis>git-diff-files</emphasis> command:</simpara>\r
188 <literallayout>$ git diff-files</literallayout>\r
189 <simpara>Oops. That wasn&#8217;t very readable. It just spit out its own internal\r
190 version of a <emphasis>diff</emphasis>, but that internal version really just tells you\r
191 that it has noticed that "hello" has been modified, and that the old object\r
192 contents it had have been replaced with something else.</simpara>\r
193 <simpara>To make it readable, we can tell <emphasis>git-diff-files</emphasis> to output the\r
194 differences as a patch, using the <literal>-p</literal> flag:</simpara>\r
195 <literallayout>$ git diff-files -p\r
196 diff --git a/hello b/hello\r
197 index 557db03..263414f 100644\r
198 --- a/hello\r
199 +++ b/hello\r
200 @@ -1 +1,2 @@\r
201  Hello World\r
202 +It's a new day for git</literallayout>\r
203 <simpara>i.e. the diff of the change we caused by adding another line to <literal>hello</literal>.</simpara>\r
204 <simpara>In other words, <emphasis>git-diff-files</emphasis> always shows us the difference between\r
205 what is recorded in the index, and what is currently in the working\r
206 tree. That&#8217;s very useful.</simpara>\r
207 <simpara>A common shorthand for <literal>git diff-files -p</literal> is to just write <literal>git\r
208 diff</literal>, which will do the same thing.</simpara>\r
209 <literallayout>$ git diff\r
210 diff --git a/hello b/hello\r
211 index 557db03..263414f 100644\r
212 --- a/hello\r
213 +++ b/hello\r
214 @@ -1 +1,2 @@\r
215  Hello World\r
216 +It's a new day for git</literallayout>\r
217 </simplesect>\r
218 <simplesect id="_committing_git_state">\r
219 <title>Committing git state</title>\r
220 <simpara>Now, we want to go to the next stage in git, which is to take the files\r
221 that git knows about in the index, and commit them as a real tree. We do\r
222 that in two phases: creating a <emphasis>tree</emphasis> object, and committing that <emphasis>tree</emphasis>\r
223 object as a <emphasis>commit</emphasis> object together with an explanation of what the\r
224 tree was all about, along with information of how we came to that state.</simpara>\r
225 <simpara>Creating a tree object is trivial, and is done with <emphasis>git-write-tree</emphasis>.\r
226 There are no options or other input: <literal>git write-tree</literal> will take the\r
227 current index state, and write an object that describes that whole\r
228 index. In other words, we&#8217;re now tying together all the different\r
229 filenames with their contents (and their permissions), and we&#8217;re\r
230 creating the equivalent of a git "directory" object:</simpara>\r
231 <literallayout>$ git write-tree</literallayout>\r
232 <simpara>and this will just output the name of the resulting tree, in this case\r
233 (if you have done exactly as I&#8217;ve described) it should be</simpara>\r
234 <literallayout>8988da15d077d4829fc51d8544c097def6644dbb</literallayout>\r
235 <simpara>which is another incomprehensible object name. Again, if you want to,\r
236 you can use <literal>git cat-file -t 8988d...</literal> to see that this time the object\r
237 is not a "blob" object, but a "tree" object (you can also use\r
238 <literal>git cat-file</literal> to actually output the raw object contents, but you&#8217;ll see\r
239 mainly a binary mess, so that&#8217;s less interesting).</simpara>\r
240 <simpara>However&#8201;&#8212;&#8201;normally you&#8217;d never use <emphasis>git-write-tree</emphasis> on its own, because\r
241 normally you always commit a tree into a commit object using the\r
242 <emphasis>git-commit-tree</emphasis> command. In fact, it&#8217;s easier to not actually use\r
243 <emphasis>git-write-tree</emphasis> on its own at all, but to just pass its result in as an\r
244 argument to <emphasis>git-commit-tree</emphasis>.</simpara>\r
245 <simpara><emphasis>git-commit-tree</emphasis> normally takes several arguments&#8201;&#8212;&#8201;it wants to know\r
246 what the <emphasis>parent</emphasis> of a commit was, but since this is the first commit\r
247 ever in this new repository, and it has no parents, we only need to pass in\r
248 the object name of the tree. However, <emphasis>git-commit-tree</emphasis> also wants to get a\r
249 commit message on its standard input, and it will write out the resulting\r
250 object name for the commit to its standard output.</simpara>\r
251 <simpara>And this is where we create the <literal>.git/refs/heads/master</literal> file\r
252 which is pointed at by <literal>HEAD</literal>. This file is supposed to contain\r
253 the reference to the top-of-tree of the master branch, and since\r
254 that&#8217;s exactly what <emphasis>git-commit-tree</emphasis> spits out, we can do this\r
255 all with a sequence of simple shell commands:</simpara>\r
256 <literallayout>$ tree=$(git write-tree)\r
257 $ commit=$(echo 'Initial commit' | git commit-tree $tree)\r
258 $ git update-ref HEAD $commit</literallayout>\r
259 <simpara>In this case this creates a totally new commit that is not related to\r
260 anything else. Normally you do this only <emphasis role="strong">once</emphasis> for a project ever, and\r
261 all later commits will be parented on top of an earlier commit.</simpara>\r
262 <simpara>Again, normally you&#8217;d never actually do this by hand. There is a\r
263 helpful script called <literal>git commit</literal> that will do all of this for you. So\r
264 you could have just written <literal>git commit</literal>\r
265 instead, and it would have done the above magic scripting for you.</simpara>\r
266 </simplesect>\r
267 <simplesect id="_making_a_change">\r
268 <title>Making a change</title>\r
269 <simpara>Remember how we did the <emphasis>git-update-index</emphasis> on file <literal>hello</literal> and then we\r
270 changed <literal>hello</literal> afterward, and could compare the new state of <literal>hello</literal> with the\r
271 state we saved in the index file?</simpara>\r
272 <simpara>Further, remember how I said that <emphasis>git-write-tree</emphasis> writes the contents\r
273 of the <emphasis role="strong">index</emphasis> file to the tree, and thus what we just committed was in\r
274 fact the <emphasis role="strong">original</emphasis> contents of the file <literal>hello</literal>, not the new ones. We did\r
275 that on purpose, to show the difference between the index state, and the\r
276 state in the working tree, and how they don&#8217;t have to match, even\r
277 when we commit things.</simpara>\r
278 <simpara>As before, if we do <literal>git diff-files -p</literal> in our git-tutorial project,\r
279 we&#8217;ll still see the same difference we saw last time: the index file\r
280 hasn&#8217;t changed by the act of committing anything. However, now that we\r
281 have committed something, we can also learn to use a new command:\r
282 <emphasis>git-diff-index</emphasis>.</simpara>\r
283 <simpara>Unlike <emphasis>git-diff-files</emphasis>, which showed the difference between the index\r
284 file and the working tree, <emphasis>git-diff-index</emphasis> shows the differences\r
285 between a committed <emphasis role="strong">tree</emphasis> and either the index file or the working\r
286 tree. In other words, <emphasis>git-diff-index</emphasis> wants a tree to be diffed\r
287 against, and before we did the commit, we couldn&#8217;t do that, because we\r
288 didn&#8217;t have anything to diff against.</simpara>\r
289 <simpara>But now we can do</simpara>\r
290 <literallayout>$ git diff-index -p HEAD</literallayout>\r
291 <simpara>(where <literal>-p</literal> has the same meaning as it did in <emphasis>git-diff-files</emphasis>), and it\r
292 will show us the same difference, but for a totally different reason.\r
293 Now we&#8217;re comparing the working tree not against the index file,\r
294 but against the tree we just wrote. It just so happens that those two\r
295 are obviously the same, so we get the same result.</simpara>\r
296 <simpara>Again, because this is a common operation, you can also just shorthand\r
297 it with</simpara>\r
298 <literallayout>$ git diff HEAD</literallayout>\r
299 <simpara>which ends up doing the above for you.</simpara>\r
300 <simpara>In other words, <emphasis>git-diff-index</emphasis> normally compares a tree against the\r
301 working tree, but when given the <literal>--cached</literal> flag, it is told to\r
302 instead compare against just the index cache contents, and ignore the\r
303 current working tree state entirely. Since we just wrote the index\r
304 file to HEAD, doing <literal>git diff-index --cached -p HEAD</literal> should thus return\r
305 an empty set of differences, and that&#8217;s exactly what it does.</simpara>\r
306 <note>\r
307 <simpara><emphasis>git-diff-index</emphasis> really always uses the index for its\r
308 comparisons, and saying that it compares a tree against the working\r
309 tree is thus not strictly accurate. In particular, the list of\r
310 files to compare (the "meta-data") <emphasis role="strong">always</emphasis> comes from the index file,\r
311 regardless of whether the <literal>--cached</literal> flag is used or not. The <literal>--cached</literal>\r
312 flag really only determines whether the file <emphasis role="strong">contents</emphasis> to be compared\r
313 come from the working tree or not.</simpara>\r
314 <simpara>This is not hard to understand, as soon as you realize that git simply\r
315 never knows (or cares) about files that it is not told about\r
316 explicitly. git will never go <emphasis role="strong">looking</emphasis> for files to compare, it\r
317 expects you to tell it what the files are, and that&#8217;s what the index\r
318 is there for.</simpara>\r
319 </note>\r
320 <simpara>However, our next step is to commit the <emphasis role="strong">change</emphasis> we did, and again, to\r
321 understand what&#8217;s going on, keep in mind the difference between "working\r
322 tree contents", "index file" and "committed tree". We have changes\r
323 in the working tree that we want to commit, and we always have to\r
324 work through the index file, so the first thing we need to do is to\r
325 update the index cache:</simpara>\r
326 <literallayout>$ git update-index hello</literallayout>\r
327 <simpara>(note how we didn&#8217;t need the <literal>--add</literal> flag this time, since git knew\r
328 about the file already).</simpara>\r
329 <simpara>Note what happens to the different <emphasis>git-diff-*</emphasis> versions here. After\r
330 we&#8217;ve updated <literal>hello</literal> in the index, <literal>git diff-files -p</literal> now shows no\r
331 differences, but <literal>git diff-index -p HEAD</literal> still <emphasis role="strong">does</emphasis> show that the\r
332 current state is different from the state we committed. In fact, now\r
333 <emphasis>git-diff-index</emphasis> shows the same difference whether we use the <literal>--cached</literal>\r
334 flag or not, since now the index is coherent with the working tree.</simpara>\r
335 <simpara>Now, since we&#8217;ve updated <literal>hello</literal> in the index, we can commit the new\r
336 version. We could do it by writing the tree by hand again, and\r
337 committing the tree (this time we&#8217;d have to use the <literal>-p HEAD</literal> flag to\r
338 tell commit that the HEAD was the <emphasis role="strong">parent</emphasis> of the new commit, and that\r
339 this wasn&#8217;t an initial commit any more), but you&#8217;ve done that once\r
340 already, so let&#8217;s just use the helpful script this time:</simpara>\r
341 <literallayout>$ git commit</literallayout>\r
342 <simpara>which starts an editor for you to write the commit message and tells you\r
343 a bit about what you have done.</simpara>\r
344 <simpara>Write whatever message you want, and all the lines that start with <emphasis>#</emphasis>\r
345 will be pruned out, and the rest will be used as the commit message for\r
346 the change. If you decide you don&#8217;t want to commit anything after all at\r
347 this point (you can continue to edit things and update the index), you\r
348 can just leave an empty message. Otherwise <literal>git commit</literal> will commit\r
349 the change for you.</simpara>\r
350 <simpara>You&#8217;ve now made your first real git commit. And if you&#8217;re interested in\r
351 looking at what <literal>git commit</literal> really does, feel free to investigate:\r
352 it&#8217;s a few very simple shell scripts to generate the helpful (?) commit\r
353 message headers, and a few one-liners that actually do the\r
354 commit itself (<emphasis>git-commit</emphasis>).</simpara>\r
355 </simplesect>\r
356 <simplesect id="_inspecting_changes">\r
357 <title>Inspecting Changes</title>\r
358 <simpara>While creating changes is useful, it&#8217;s even more useful if you can tell\r
359 later what changed. The most useful command for this is another of the\r
360 <emphasis>diff</emphasis> family, namely <emphasis>git-diff-tree</emphasis>.</simpara>\r
361 <simpara><emphasis>git-diff-tree</emphasis> can be given two arbitrary trees, and it will tell you the\r
362 differences between them. Perhaps even more commonly, though, you can\r
363 give it just a single commit object, and it will figure out the parent\r
364 of that commit itself, and show the difference directly. Thus, to get\r
365 the same diff that we&#8217;ve already seen several times, we can now do</simpara>\r
366 <literallayout>$ git diff-tree -p HEAD</literallayout>\r
367 <simpara>(again, <literal>-p</literal> means to show the difference as a human-readable patch),\r
368 and it will show what the last commit (in <literal>HEAD</literal>) actually changed.</simpara>\r
369 <note>\r
370 <simpara>Here is an ASCII art by Jon Loeliger that illustrates how\r
371 various diff-\* commands compare things.</simpara>\r
372 <literallayout class="monospaced">            diff-tree\r
373              +----+\r
374              |    |\r
375              |    |\r
376              V    V\r
377           +-----------+\r
378           | Object DB |\r
379           |  Backing  |\r
380           |   Store   |\r
381           +-----------+\r
382             ^    ^\r
383             |    |\r
384             |    |  diff-index --cached\r
385             |    |\r
386 diff-index  |    V\r
387             |  +-----------+\r
388             |  |   Index   |\r
389             |  |  "cache"  |\r
390             |  +-----------+\r
391             |    ^\r
392             |    |\r
393             |    |  diff-files\r
394             |    |\r
395             V    V\r
396           +-----------+\r
397           |  Working  |\r
398           | Directory |\r
399           +-----------+</literallayout>\r
400 </note>\r
401 <simpara>More interestingly, you can also give <emphasis>git-diff-tree</emphasis> the <literal>--pretty</literal> flag,\r
402 which tells it to also show the commit message and author and date of the\r
403 commit, and you can tell it to show a whole series of diffs.\r
404 Alternatively, you can tell it to be "silent", and not show the diffs at\r
405 all, but just show the actual commit message.</simpara>\r
406 <simpara>In fact, together with the <emphasis>git-rev-list</emphasis> program (which generates a\r
407 list of revisions), <emphasis>git-diff-tree</emphasis> ends up being a veritable fount of\r
408 changes. A trivial (but very useful) script called <emphasis>git-whatchanged</emphasis> is\r
409 included with git which does exactly this, and shows a log of recent\r
410 activities.</simpara>\r
411 <simpara>To see the whole history of our pitiful little git-tutorial project, you\r
412 can do</simpara>\r
413 <literallayout>$ git log</literallayout>\r
414 <simpara>which shows just the log messages, or if we want to see the log together\r
415 with the associated patches use the more complex (and much more\r
416 powerful)</simpara>\r
417 <literallayout>$ git whatchanged -p</literallayout>\r
418 <simpara>and you will see exactly what has changed in the repository over its\r
419 short history.</simpara>\r
420 <note><simpara>When using the above two commands, the initial commit will be shown.\r
421 If this is a problem because it is huge, you can hide it by setting\r
422 the log.showroot configuration variable to false. Having this, you\r
423 can still show it for each command just adding the <literal>--root</literal> option,\r
424 which is a flag for <emphasis>git-diff-tree</emphasis> accepted by both commands.</simpara></note>\r
425 <simpara>With that, you should now be having some inkling of what git does, and\r
426 can explore on your own.</simpara>\r
427 <note><simpara>Most likely, you are not directly using the core\r
428 git Plumbing commands, but using Porcelain such as <emphasis>git-add</emphasis>, &#8216;git-rm&#8217;\r
429 and &#8216;git-commit&#8217;.</simpara></note>\r
430 </simplesect>\r
431 <simplesect id="_tagging_a_version">\r
432 <title>Tagging a version</title>\r
433 <simpara>In git, there are two kinds of tags, a "light" one, and an "annotated tag".</simpara>\r
434 <simpara>A "light" tag is technically nothing more than a branch, except we put\r
435 it in the <literal>.git/refs/tags/</literal> subdirectory instead of calling it a <literal>head</literal>.\r
436 So the simplest form of tag involves nothing more than</simpara>\r
437 <literallayout>$ git tag my-first-tag</literallayout>\r
438 <simpara>which just writes the current <literal>HEAD</literal> into the <literal>.git/refs/tags/my-first-tag</literal>\r
439 file, after which point you can then use this symbolic name for that\r
440 particular state. You can, for example, do</simpara>\r
441 <literallayout>$ git diff my-first-tag</literallayout>\r
442 <simpara>to diff your current state against that tag which at this point will\r
443 obviously be an empty diff, but if you continue to develop and commit\r
444 stuff, you can use your tag as an "anchor-point" to see what has changed\r
445 since you tagged it.</simpara>\r
446 <simpara>An "annotated tag" is actually a real git object, and contains not only a\r
447 pointer to the state you want to tag, but also a small tag name and\r
448 message, along with optionally a PGP signature that says that yes,\r
449 you really did\r
450 that tag. You create these annotated tags with either the <literal>-a</literal> or\r
451 <literal>-s</literal> flag to <emphasis>git-tag</emphasis>:</simpara>\r
452 <literallayout>$ git tag -s &lt;tagname&gt;</literallayout>\r
453 <simpara>which will sign the current <literal>HEAD</literal> (but you can also give it another\r
454 argument that specifies the thing to tag, i.e., you could have tagged the\r
455 current <literal>mybranch</literal> point by using <literal>git tag &lt;tagname&gt; mybranch</literal>).</simpara>\r
456 <simpara>You normally only do signed tags for major releases or things\r
457 like that, while the light-weight tags are useful for any marking you\r
458 want to do&#8201;&#8212;&#8201;any time you decide that you want to remember a certain\r
459 point, just create a private tag for it, and you have a nice symbolic\r
460 name for the state at that point.</simpara>\r
461 </simplesect>\r
462 <simplesect id="_copying_repositories">\r
463 <title>Copying repositories</title>\r
464 <simpara>git repositories are normally totally self-sufficient and relocatable.\r
465 Unlike CVS, for example, there is no separate notion of\r
466 "repository" and "working tree". A git repository normally <emphasis role="strong">is</emphasis> the\r
467 working tree, with the local git information hidden in the <literal>.git</literal>\r
468 subdirectory. There is nothing else. What you see is what you got.</simpara>\r
469 <note><simpara>You can tell git to split the git internal information from\r
470 the directory that it tracks, but we&#8217;ll ignore that for now: it&#8217;s not\r
471 how normal projects work, and it&#8217;s really only meant for special uses.\r
472 So the mental model of "the git information is always tied directly to\r
473 the working tree that it describes" may not be technically 100%\r
474 accurate, but it&#8217;s a good model for all normal use.</simpara></note>\r
475 <simpara>This has two implications:</simpara>\r
476 <itemizedlist>\r
477 <listitem>\r
478 <simpara>\r
479 if you grow bored with the tutorial repository you created (or you&#8217;ve\r
480    made a mistake and want to start all over), you can just do simple\r
481 </simpara>\r
482 <literallayout>$ rm -rf git-tutorial</literallayout>\r
483 <simpara>and it will be gone. There&#8217;s no external repository, and there&#8217;s no\r
484 history outside the project you created.</simpara>\r
485 </listitem>\r
486 <listitem>\r
487 <simpara>\r
488 if you want to move or duplicate a git repository, you can do so. There\r
489    is <emphasis>git-clone</emphasis> command, but if all you want to do is just to\r
490    create a copy of your repository (with all the full history that\r
491    went along with it), you can do so with a regular\r
492    <literal>cp -a git-tutorial new-git-tutorial</literal>.\r
493 </simpara>\r
494 <simpara>Note that when you&#8217;ve moved or copied a git repository, your git index\r
495 file (which caches various information, notably some of the "stat"\r
496 information for the files involved) will likely need to be refreshed.\r
497 So after you do a <literal>cp -a</literal> to create a new copy, you&#8217;ll want to do</simpara>\r
498 <literallayout>$ git update-index --refresh</literallayout>\r
499 <simpara>in the new repository to make sure that the index file is up-to-date.</simpara>\r
500 </listitem>\r
501 </itemizedlist>\r
502 <simpara>Note that the second point is true even across machines. You can\r
503 duplicate a remote git repository with <emphasis role="strong">any</emphasis> regular copy mechanism, be it\r
504 <emphasis>scp</emphasis>, <emphasis>rsync</emphasis> or <emphasis>wget</emphasis>.</simpara>\r
505 <simpara>When copying a remote repository, you&#8217;ll want to at a minimum update the\r
506 index cache when you do this, and especially with other peoples'\r
507 repositories you often want to make sure that the index cache is in some\r
508 known state (you don&#8217;t know <emphasis role="strong">what</emphasis> they&#8217;ve done and not yet checked in),\r
509 so usually you&#8217;ll precede the <emphasis>git-update-index</emphasis> with a</simpara>\r
510 <literallayout>$ git read-tree --reset HEAD\r
511 $ git update-index --refresh</literallayout>\r
512 <simpara>which will force a total index re-build from the tree pointed to by <literal>HEAD</literal>.\r
513 It resets the index contents to <literal>HEAD</literal>, and then the <emphasis>git-update-index</emphasis>\r
514 makes sure to match up all index entries with the checked-out files.\r
515 If the original repository had uncommitted changes in its\r
516 working tree, <literal>git update-index --refresh</literal> notices them and\r
517 tells you they need to be updated.</simpara>\r
518 <simpara>The above can also be written as simply</simpara>\r
519 <literallayout>$ git reset</literallayout>\r
520 <simpara>and in fact a lot of the common git command combinations can be scripted\r
521 with the <literal>git xyz</literal> interfaces.  You can learn things by just looking\r
522 at what the various git scripts do.  For example, <literal>git reset</literal> used to be\r
523 the above two lines implemented in <emphasis>git-reset</emphasis>, but some things like\r
524 <emphasis>git-status</emphasis> and <emphasis>git-commit</emphasis> are slightly more complex scripts around\r
525 the basic git commands.</simpara>\r
526 <simpara>Many (most?) public remote repositories will not contain any of\r
527 the checked out files or even an index file, and will <emphasis role="strong">only</emphasis> contain the\r
528 actual core git files. Such a repository usually doesn&#8217;t even have the\r
529 <literal>.git</literal> subdirectory, but has all the git files directly in the\r
530 repository.</simpara>\r
531 <simpara>To create your own local live copy of such a "raw" git repository, you&#8217;d\r
532 first create your own subdirectory for the project, and then copy the\r
533 raw repository contents into the <literal>.git</literal> directory. For example, to\r
534 create your own copy of the git repository, you&#8217;d do the following</simpara>\r
535 <literallayout>$ mkdir my-git\r
536 $ cd my-git\r
537 $ rsync -rL rsync://rsync.kernel.org/pub/scm/git/git.git/ .git</literallayout>\r
538 <simpara>followed by</simpara>\r
539 <literallayout>$ git read-tree HEAD</literallayout>\r
540 <simpara>to populate the index. However, now you have populated the index, and\r
541 you have all the git internal files, but you will notice that you don&#8217;t\r
542 actually have any of the working tree files to work on. To get\r
543 those, you&#8217;d check them out with</simpara>\r
544 <literallayout>$ git checkout-index -u -a</literallayout>\r
545 <simpara>where the <literal>-u</literal> flag means that you want the checkout to keep the index\r
546 up-to-date (so that you don&#8217;t have to refresh it afterward), and the\r
547 <literal>-a</literal> flag means "check out all files" (if you have a stale copy or an\r
548 older version of a checked out tree you may also need to add the <literal>-f</literal>\r
549 flag first, to tell <emphasis>git-checkout-index</emphasis> to <emphasis role="strong">force</emphasis> overwriting of any old\r
550 files).</simpara>\r
551 <simpara>Again, this can all be simplified with</simpara>\r
552 <literallayout>$ git clone rsync://rsync.kernel.org/pub/scm/git/git.git/ my-git\r
553 $ cd my-git\r
554 $ git checkout</literallayout>\r
555 <simpara>which will end up doing all of the above for you.</simpara>\r
556 <simpara>You have now successfully copied somebody else&#8217;s (mine) remote\r
557 repository, and checked it out.</simpara>\r
558 </simplesect>\r
559 <simplesect id="_creating_a_new_branch">\r
560 <title>Creating a new branch</title>\r
561 <simpara>Branches in git are really nothing more than pointers into the git\r
562 object database from within the <literal>.git/refs/</literal> subdirectory, and as we\r
563 already discussed, the <literal>HEAD</literal> branch is nothing but a symlink to one of\r
564 these object pointers.</simpara>\r
565 <simpara>You can at any time create a new branch by just picking an arbitrary\r
566 point in the project history, and just writing the SHA1 name of that\r
567 object into a file under <literal>.git/refs/heads/</literal>. You can use any filename you\r
568 want (and indeed, subdirectories), but the convention is that the\r
569 "normal" branch is called <literal>master</literal>. That&#8217;s just a convention, though,\r
570 and nothing enforces it.</simpara>\r
571 <simpara>To show that as an example, let&#8217;s go back to the git-tutorial repository we\r
572 used earlier, and create a branch in it. You do that by simply just\r
573 saying that you want to check out a new branch:</simpara>\r
574 <literallayout>$ git checkout -b mybranch</literallayout>\r
575 <simpara>will create a new branch based at the current <literal>HEAD</literal> position, and switch\r
576 to it.</simpara>\r
577 <note>\r
578 <simpara>If you make the decision to start your new branch at some\r
579 other point in the history than the current <literal>HEAD</literal>, you can do so by\r
580 just telling <emphasis>git-checkout</emphasis> what the base of the checkout would be.\r
581 In other words, if you have an earlier tag or branch, you&#8217;d just do</simpara>\r
582 <literallayout>$ git checkout -b mybranch earlier-commit</literallayout>\r
583 <simpara>and it would create the new branch <literal>mybranch</literal> at the earlier commit,\r
584 and check out the state at that time.</simpara>\r
585 </note>\r
586 <simpara>You can always just jump back to your original <literal>master</literal> branch by doing</simpara>\r
587 <literallayout>$ git checkout master</literallayout>\r
588 <simpara>(or any other branch-name, for that matter) and if you forget which\r
589 branch you happen to be on, a simple</simpara>\r
590 <literallayout>$ cat .git/HEAD</literallayout>\r
591 <simpara>will tell you where it&#8217;s pointing.  To get the list of branches\r
592 you have, you can say</simpara>\r
593 <literallayout>$ git branch</literallayout>\r
594 <simpara>which used to be nothing more than a simple script around <literal>ls .git/refs/heads</literal>.\r
595 There will be an asterisk in front of the branch you are currently on.</simpara>\r
596 <simpara>Sometimes you may wish to create a new branch <emphasis>without</emphasis> actually\r
597 checking it out and switching to it. If so, just use the command</simpara>\r
598 <literallayout>$ git branch &lt;branchname&gt; [startingpoint]</literallayout>\r
599 <simpara>which will simply <emphasis>create</emphasis> the branch, but will not do anything further.\r
600 You can then later&#8201;&#8212;&#8201;once you decide that you want to actually develop\r
601 on that branch&#8201;&#8212;&#8201;switch to that branch with a regular <emphasis>git-checkout</emphasis>\r
602 with the branchname as the argument.</simpara>\r
603 </simplesect>\r
604 <simplesect id="_merging_two_branches">\r
605 <title>Merging two branches</title>\r
606 <simpara>One of the ideas of having a branch is that you do some (possibly\r
607 experimental) work in it, and eventually merge it back to the main\r
608 branch. So assuming you created the above <literal>mybranch</literal> that started out\r
609 being the same as the original <literal>master</literal> branch, let&#8217;s make sure we&#8217;re in\r
610 that branch, and do some work there.</simpara>\r
611 <literallayout>$ git checkout mybranch\r
612 $ echo "Work, work, work" &gt;&gt;hello\r
613 $ git commit -m "Some work." -i hello</literallayout>\r
614 <simpara>Here, we just added another line to <literal>hello</literal>, and we used a shorthand for\r
615 doing both <literal>git update-index hello</literal> and <literal>git commit</literal> by just giving the\r
616 filename directly to <literal>git commit</literal>, with an <literal>-i</literal> flag (it tells\r
617 git to <emphasis>include</emphasis> that file in addition to what you have done to\r
618 the index file so far when making the commit).  The <literal>-m</literal> flag is to give the\r
619 commit log message from the command line.</simpara>\r
620 <simpara>Now, to make it a bit more interesting, let&#8217;s assume that somebody else\r
621 does some work in the original branch, and simulate that by going back\r
622 to the master branch, and editing the same file differently there:</simpara>\r
623 <literallayout>$ git checkout master</literallayout>\r
624 <simpara>Here, take a moment to look at the contents of <literal>hello</literal>, and notice how they\r
625 don&#8217;t contain the work we just did in <literal>mybranch</literal>&#8201;&#8212;&#8201;because that work\r
626 hasn&#8217;t happened in the <literal>master</literal> branch at all. Then do</simpara>\r
627 <literallayout>$ echo "Play, play, play" &gt;&gt;hello\r
628 $ echo "Lots of fun" &gt;&gt;example\r
629 $ git commit -m "Some fun." -i hello example</literallayout>\r
630 <simpara>since the master branch is obviously in a much better mood.</simpara>\r
631 <simpara>Now, you&#8217;ve got two branches, and you decide that you want to merge the\r
632 work done. Before we do that, let&#8217;s introduce a cool graphical tool that\r
633 helps you view what&#8217;s going on:</simpara>\r
634 <literallayout>$ gitk --all</literallayout>\r
635 <simpara>will show you graphically both of your branches (that&#8217;s what the <literal>--all</literal>\r
636 means: normally it will just show you your current <literal>HEAD</literal>) and their\r
637 histories. You can also see exactly how they came to be from a common\r
638 source.</simpara>\r
639 <simpara>Anyway, let&#8217;s exit <emphasis>gitk</emphasis> (<literal>^Q</literal> or the File menu), and decide that we want\r
640 to merge the work we did on the <literal>mybranch</literal> branch into the <literal>master</literal>\r
641 branch (which is currently our <literal>HEAD</literal> too). To do that, there&#8217;s a nice\r
642 script called <emphasis>git-merge</emphasis>, which wants to know which branches you want\r
643 to resolve and what the merge is all about:</simpara>\r
644 <literallayout>$ git merge -m "Merge work in mybranch" mybranch</literallayout>\r
645 <simpara>where the first argument is going to be used as the commit message if\r
646 the merge can be resolved automatically.</simpara>\r
647 <simpara>Now, in this case we&#8217;ve intentionally created a situation where the\r
648 merge will need to be fixed up by hand, though, so git will do as much\r
649 of it as it can automatically (which in this case is just merge the <literal>example</literal>\r
650 file, which had no differences in the <literal>mybranch</literal> branch), and say:</simpara>\r
651 <literallayout>        Auto-merging hello\r
652         CONFLICT (content): Merge conflict in hello\r
653         Automatic merge failed; fix conflicts and then commit the result.</literallayout>\r
654 <simpara>It tells you that it did an "Automatic merge", which\r
655 failed due to conflicts in <literal>hello</literal>.</simpara>\r
656 <simpara>Not to worry. It left the (trivial) conflict in <literal>hello</literal> in the same form you\r
657 should already be well used to if you&#8217;ve ever used CVS, so let&#8217;s just\r
658 open <literal>hello</literal> in our editor (whatever that may be), and fix it up somehow.\r
659 I&#8217;d suggest just making it so that <literal>hello</literal> contains all four lines:</simpara>\r
660 <literallayout>Hello World\r
661 It's a new day for git\r
662 Play, play, play\r
663 Work, work, work</literallayout>\r
664 <simpara>and once you&#8217;re happy with your manual merge, just do a</simpara>\r
665 <literallayout>$ git commit -i hello</literallayout>\r
666 <simpara>which will very loudly warn you that you&#8217;re now committing a merge\r
667 (which is correct, so never mind), and you can write a small merge\r
668 message about your adventures in <emphasis>git-merge</emphasis>-land.</simpara>\r
669 <simpara>After you&#8217;re done, start up <literal>gitk --all</literal> to see graphically what the\r
670 history looks like. Notice that <literal>mybranch</literal> still exists, and you can\r
671 switch to it, and continue to work with it if you want to. The\r
672 <literal>mybranch</literal> branch will not contain the merge, but next time you merge it\r
673 from the <literal>master</literal> branch, git will know how you merged it, so you&#8217;ll not\r
674 have to do <emphasis>that</emphasis> merge again.</simpara>\r
675 <simpara>Another useful tool, especially if you do not always work in X-Window\r
676 environment, is <literal>git show-branch</literal>.</simpara>\r
677 <literallayout>$ git show-branch --topo-order --more=1 master mybranch\r
678 * [master] Merge work in mybranch\r
679  ! [mybranch] Some work.\r
680 --\r
681 -  [master] Merge work in mybranch\r
682 *+ [mybranch] Some work.\r
683 *  [master^] Some fun.</literallayout>\r
684 <simpara>The first two lines indicate that it is showing the two branches\r
685 and the first line of the commit log message from their\r
686 top-of-the-tree commits, you are currently on <literal>master</literal> branch\r
687 (notice the asterisk <literal>*</literal> character), and the first column for\r
688 the later output lines is used to show commits contained in the\r
689 <literal>master</literal> branch, and the second column for the <literal>mybranch</literal>\r
690 branch. Three commits are shown along with their log messages.\r
691 All of them have non blank characters in the first column (<literal>*</literal>\r
692 shows an ordinary commit on the current branch, <literal>-</literal> is a merge commit), which\r
693 means they are now part of the <literal>master</literal> branch. Only the "Some\r
694 work" commit has the plus <literal>+</literal> character in the second column,\r
695 because <literal>mybranch</literal> has not been merged to incorporate these\r
696 commits from the master branch.  The string inside brackets\r
697 before the commit log message is a short name you can use to\r
698 name the commit.  In the above example, <emphasis>master</emphasis> and <emphasis>mybranch</emphasis>\r
699 are branch heads.  <emphasis>master^</emphasis> is the first parent of <emphasis>master</emphasis>\r
700 branch head.  Please see <xref linkend="git-rev-parse(1)"/> if you want to\r
701 see more complex cases.</simpara>\r
702 <note><simpara>Without the <emphasis>--more=1</emphasis> option, <emphasis>git-show-branch</emphasis> would not output the\r
703 <emphasis>[master^]</emphasis> commit, as <emphasis>[mybranch]</emphasis> commit is a common ancestor of\r
704 both <emphasis>master</emphasis> and <emphasis>mybranch</emphasis> tips.  Please see <xref linkend="git-show-branch(1)"/>\r
705 for details.</simpara></note>\r
706 <note><simpara>If there were more commits on the <emphasis>master</emphasis> branch after the merge, the\r
707 merge commit itself would not be shown by <emphasis>git-show-branch</emphasis> by\r
708 default.  You would need to provide <emphasis>--sparse</emphasis> option to make the\r
709 merge commit visible in this case.</simpara></note>\r
710 <simpara>Now, let&#8217;s pretend you are the one who did all the work in\r
711 <literal>mybranch</literal>, and the fruit of your hard work has finally been merged\r
712 to the <literal>master</literal> branch. Let&#8217;s go back to <literal>mybranch</literal>, and run\r
713 <emphasis>git-merge</emphasis> to get the "upstream changes" back to your branch.</simpara>\r
714 <literallayout>$ git checkout mybranch\r
715 $ git merge -m "Merge upstream changes." master</literallayout>\r
716 <simpara>This outputs something like this (the actual commit object names\r
717 would be different)</simpara>\r
718 <literallayout>Updating from ae3a2da... to a80b4aa....\r
719 Fast forward (no commit created; -m option ignored)\r
720  example |    1 +\r
721  hello   |    1 +\r
722  2 files changed, 2 insertions(+), 0 deletions(-)</literallayout>\r
723 <simpara>Because your branch did not contain anything more than what had\r
724 already been merged into the <literal>master</literal> branch, the merge operation did\r
725 not actually do a merge. Instead, it just updated the top of\r
726 the tree of your branch to that of the <literal>master</literal> branch. This is\r
727 often called <emphasis>fast forward</emphasis> merge.</simpara>\r
728 <simpara>You can run <literal>gitk --all</literal> again to see how the commit ancestry\r
729 looks like, or run <emphasis>show-branch</emphasis>, which tells you this.</simpara>\r
730 <literallayout>$ git show-branch master mybranch\r
731 ! [master] Merge work in mybranch\r
732  * [mybranch] Merge work in mybranch\r
733 --\r
734 -- [master] Merge work in mybranch</literallayout>\r
735 </simplesect>\r
736 <simplesect id="_merging_external_work">\r
737 <title>Merging external work</title>\r
738 <simpara>It&#8217;s usually much more common that you merge with somebody else than\r
739 merging with your own branches, so it&#8217;s worth pointing out that git\r
740 makes that very easy too, and in fact, it&#8217;s not that different from\r
741 doing a <emphasis>git-merge</emphasis>. In fact, a remote merge ends up being nothing\r
742 more than "fetch the work from a remote repository into a temporary tag"\r
743 followed by a <emphasis>git-merge</emphasis>.</simpara>\r
744 <simpara>Fetching from a remote repository is done by, unsurprisingly,\r
745 <emphasis>git-fetch</emphasis>:</simpara>\r
746 <literallayout>$ git fetch &lt;remote-repository&gt;</literallayout>\r
747 <simpara>One of the following transports can be used to name the\r
748 repository to download from:</simpara>\r
749 <variablelist>\r
750 <varlistentry>\r
751 <term>\r
752 Rsync\r
753 </term>\r
754 <listitem>\r
755 <simpara>\r
756         <literal>rsync://remote.machine/path/to/repo.git/</literal>\r
757 </simpara>\r
758 <simpara>Rsync transport is usable for both uploading and downloading,\r
759 but is completely unaware of what git does, and can produce\r
760 unexpected results when you download from the public repository\r
761 while the repository owner is uploading into it via <literal>rsync</literal>\r
762 transport.  Most notably, it could update the files under\r
763 <literal>refs/</literal> which holds the object name of the topmost commits\r
764 before uploading the files in <literal>objects/</literal>&#8201;&#8212;&#8201;the downloader would\r
765 obtain head commit object name while that object itself is still\r
766 not available in the repository.  For this reason, it is\r
767 considered deprecated.</simpara>\r
768 </listitem>\r
769 </varlistentry>\r
770 <varlistentry>\r
771 <term>\r
772 SSH\r
773 </term>\r
774 <listitem>\r
775 <simpara>\r
776         <literal>remote.machine:/path/to/repo.git/</literal> or\r
777 </simpara>\r
778 <simpara><literal>ssh://remote.machine/path/to/repo.git/</literal></simpara>\r
779 <simpara>This transport can be used for both uploading and downloading,\r
780 and requires you to have a log-in privilege over <literal>ssh</literal> to the\r
781 remote machine.  It finds out the set of objects the other side\r
782 lacks by exchanging the head commits both ends have and\r
783 transfers (close to) minimum set of objects.  It is by far the\r
784 most efficient way to exchange git objects between repositories.</simpara>\r
785 </listitem>\r
786 </varlistentry>\r
787 <varlistentry>\r
788 <term>\r
789 Local directory\r
790 </term>\r
791 <listitem>\r
792 <simpara>\r
793         <literal>/path/to/repo.git/</literal>\r
794 </simpara>\r
795 <simpara>This transport is the same as SSH transport but uses <emphasis>sh</emphasis> to run\r
796 both ends on the local machine instead of running other end on\r
797 the remote machine via <emphasis>ssh</emphasis>.</simpara>\r
798 </listitem>\r
799 </varlistentry>\r
800 <varlistentry>\r
801 <term>\r
802 git Native\r
803 </term>\r
804 <listitem>\r
805 <simpara>\r
806         <literal>git://remote.machine/path/to/repo.git/</literal>\r
807 </simpara>\r
808 <simpara>This transport was designed for anonymous downloading.  Like SSH\r
809 transport, it finds out the set of objects the downstream side\r
810 lacks and transfers (close to) minimum set of objects.</simpara>\r
811 </listitem>\r
812 </varlistentry>\r
813 <varlistentry>\r
814 <term>\r
815 HTTP(S)\r
816 </term>\r
817 <listitem>\r
818 <simpara>\r
819         <literal>http://remote.machine/path/to/repo.git/</literal>\r
820 </simpara>\r
821 <simpara>Downloader from http and https URL\r
822 first obtains the topmost commit object name from the remote site\r
823 by looking at the specified refname under <literal>repo.git/refs/</literal> directory,\r
824 and then tries to obtain the\r
825 commit object by downloading from <literal>repo.git/objects/xx/xxx...</literal>\r
826 using the object name of that commit object.  Then it reads the\r
827 commit object to find out its parent commits and the associate\r
828 tree object; it repeats this process until it gets all the\r
829 necessary objects.  Because of this behavior, they are\r
830 sometimes also called <emphasis>commit walkers</emphasis>.</simpara>\r
831 <simpara>The <emphasis>commit walkers</emphasis> are sometimes also called <emphasis>dumb\r
832 transports</emphasis>, because they do not require any git aware smart\r
833 server like git Native transport does.  Any stock HTTP server\r
834 that does not even support directory index would suffice.  But\r
835 you must prepare your repository with <emphasis>git-update-server-info</emphasis>\r
836 to help dumb transport downloaders.</simpara>\r
837 </listitem>\r
838 </varlistentry>\r
839 </variablelist>\r
840 <simpara>Once you fetch from the remote repository, you <literal>merge</literal> that\r
841 with your current branch.</simpara>\r
842 <simpara>However&#8201;&#8212;&#8201;it&#8217;s such a common thing to <literal>fetch</literal> and then\r
843 immediately <literal>merge</literal>, that it&#8217;s called <literal>git pull</literal>, and you can\r
844 simply do</simpara>\r
845 <literallayout>$ git pull &lt;remote-repository&gt;</literallayout>\r
846 <simpara>and optionally give a branch-name for the remote end as a second\r
847 argument.</simpara>\r
848 <note><simpara>You could do without using any branches at all, by\r
849 keeping as many local repositories as you would like to have\r
850 branches, and merging between them with <emphasis>git-pull</emphasis>, just like\r
851 you merge between branches. The advantage of this approach is\r
852 that it lets you keep a set of files for each <literal>branch</literal> checked\r
853 out and you may find it easier to switch back and forth if you\r
854 juggle multiple lines of development simultaneously. Of\r
855 course, you will pay the price of more disk usage to hold\r
856 multiple working trees, but disk space is cheap these days.</simpara></note>\r
857 <simpara>It is likely that you will be pulling from the same remote\r
858 repository from time to time. As a short hand, you can store\r
859 the remote repository URL in the local repository&#8217;s config file\r
860 like this:</simpara>\r
861 <literallayout>$ git config remote.linus.url http://www.kernel.org/pub/scm/git/git.git/</literallayout>\r
862 <simpara>and use the "linus" keyword with <emphasis>git-pull</emphasis> instead of the full URL.</simpara>\r
863 <simpara>Examples.</simpara>\r
864 <orderedlist numeration="arabic">\r
865 <listitem>\r
866 <simpara>\r
867 <literal>git pull linus</literal>\r
868 </simpara>\r
869 </listitem>\r
870 <listitem>\r
871 <simpara>\r
872 <literal>git pull linus tag v0.99.1</literal>\r
873 </simpara>\r
874 </listitem>\r
875 </orderedlist>\r
876 <simpara>the above are equivalent to:</simpara>\r
877 <orderedlist numeration="arabic">\r
878 <listitem>\r
879 <simpara>\r
880 <literal>git pull <ulink url="http://www.kernel.org/pub/scm/git/git.git/">http://www.kernel.org/pub/scm/git/git.git/</ulink> HEAD</literal>\r
881 </simpara>\r
882 </listitem>\r
883 <listitem>\r
884 <simpara>\r
885 <literal>git pull <ulink url="http://www.kernel.org/pub/scm/git/git.git/">http://www.kernel.org/pub/scm/git/git.git/</ulink> tag v0.99.1</literal>\r
886 </simpara>\r
887 </listitem>\r
888 </orderedlist>\r
889 </simplesect>\r
890 <simplesect id="_how_does_the_merge_work">\r
891 <title>How does the merge work?</title>\r
892 <simpara>We said this tutorial shows what plumbing does to help you cope\r
893 with the porcelain that isn&#8217;t flushing, but we so far did not\r
894 talk about how the merge really works.  If you are following\r
895 this tutorial the first time, I&#8217;d suggest to skip to "Publishing\r
896 your work" section and come back here later.</simpara>\r
897 <simpara>OK, still with me?  To give us an example to look at, let&#8217;s go\r
898 back to the earlier repository with "hello" and "example" file,\r
899 and bring ourselves back to the pre-merge state:</simpara>\r
900 <literallayout>$ git show-branch --more=2 master mybranch\r
901 ! [master] Merge work in mybranch\r
902  * [mybranch] Merge work in mybranch\r
903 --\r
904 -- [master] Merge work in mybranch\r
905 +* [master^2] Some work.\r
906 +* [master^] Some fun.</literallayout>\r
907 <simpara>Remember, before running <emphasis>git-merge</emphasis>, our <literal>master</literal> head was at\r
908 "Some fun." commit, while our <literal>mybranch</literal> head was at "Some\r
909 work." commit.</simpara>\r
910 <literallayout>$ git checkout mybranch\r
911 $ git reset --hard master^2\r
912 $ git checkout master\r
913 $ git reset --hard master^</literallayout>\r
914 <simpara>After rewinding, the commit structure should look like this:</simpara>\r
915 <literallayout>$ git show-branch\r
916 * [master] Some fun.\r
917  ! [mybranch] Some work.\r
918 --\r
919  + [mybranch] Some work.\r
920 *  [master] Some fun.\r
921 *+ [mybranch^] New day.</literallayout>\r
922 <simpara>Now we are ready to experiment with the merge by hand.</simpara>\r
923 <simpara><literal>git merge</literal> command, when merging two branches, uses 3-way merge\r
924 algorithm.  First, it finds the common ancestor between them.\r
925 The command it uses is <emphasis>git-merge-base</emphasis>:</simpara>\r
926 <literallayout>$ mb=$(git merge-base HEAD mybranch)</literallayout>\r
927 <simpara>The command writes the commit object name of the common ancestor\r
928 to the standard output, so we captured its output to a variable,\r
929 because we will be using it in the next step.  By the way, the common\r
930 ancestor commit is the "New day." commit in this case.  You can\r
931 tell it by:</simpara>\r
932 <literallayout>$ git name-rev $mb\r
933 my-first-tag</literallayout>\r
934 <simpara>After finding out a common ancestor commit, the second step is\r
935 this:</simpara>\r
936 <literallayout>$ git read-tree -m -u $mb HEAD mybranch</literallayout>\r
937 <simpara>This is the same <emphasis>git-read-tree</emphasis> command we have already seen,\r
938 but it takes three trees, unlike previous examples.  This reads\r
939 the contents of each tree into different <emphasis>stage</emphasis> in the index\r
940 file (the first tree goes to stage 1, the second to stage 2,\r
941 etc.).  After reading three trees into three stages, the paths\r
942 that are the same in all three stages are <emphasis>collapsed</emphasis> into stage\r
943 0.  Also paths that are the same in two of three stages are\r
944 collapsed into stage 0, taking the SHA1 from either stage 2 or\r
945 stage 3, whichever is different from stage 1 (i.e. only one side\r
946 changed from the common ancestor).</simpara>\r
947 <simpara>After <emphasis>collapsing</emphasis> operation, paths that are different in three\r
948 trees are left in non-zero stages.  At this point, you can\r
949 inspect the index file with this command:</simpara>\r
950 <literallayout>$ git ls-files --stage\r
951 100644 7f8b141b65fdcee47321e399a2598a235a032422 0       example\r
952 100644 263414f423d0e4d70dae8fe53fa34614ff3e2860 1       hello\r
953 100644 06fa6a24256dc7e560efa5687fa84b51f0263c3a 2       hello\r
954 100644 cc44c73eb783565da5831b4d820c962954019b69 3       hello</literallayout>\r
955 <simpara>In our example of only two files, we did not have unchanged\r
956 files so only <emphasis>example</emphasis> resulted in collapsing.  But in real-life\r
957 large projects, when only a small number of files change in one commit,\r
958 this <emphasis>collapsing</emphasis> tends to trivially merge most of the paths\r
959 fairly quickly, leaving only a handful of real changes in non-zero\r
960 stages.</simpara>\r
961 <simpara>To look at only non-zero stages, use <literal>--unmerged</literal> flag:</simpara>\r
962 <literallayout>$ git ls-files --unmerged\r
963 100644 263414f423d0e4d70dae8fe53fa34614ff3e2860 1       hello\r
964 100644 06fa6a24256dc7e560efa5687fa84b51f0263c3a 2       hello\r
965 100644 cc44c73eb783565da5831b4d820c962954019b69 3       hello</literallayout>\r
966 <simpara>The next step of merging is to merge these three versions of the\r
967 file, using 3-way merge.  This is done by giving\r
968 <emphasis>git-merge-one-file</emphasis> command as one of the arguments to\r
969 <emphasis>git-merge-index</emphasis> command:</simpara>\r
970 <literallayout>$ git merge-index git-merge-one-file hello\r
971 Auto-merging hello\r
972 ERROR: Merge conflict in hello\r
973 fatal: merge program failed</literallayout>\r
974 <simpara><emphasis>git-merge-one-file</emphasis> script is called with parameters to\r
975 describe those three versions, and is responsible to leave the\r
976 merge results in the working tree.\r
977 It is a fairly straightforward shell script, and\r
978 eventually calls <emphasis>merge</emphasis> program from RCS suite to perform a\r
979 file-level 3-way merge.  In this case, <emphasis>merge</emphasis> detects\r
980 conflicts, and the merge result with conflict marks is left in\r
981 the working tree..  This can be seen if you run <literal>ls-files\r
982 --stage</literal> again at this point:</simpara>\r
983 <literallayout>$ git ls-files --stage\r
984 100644 7f8b141b65fdcee47321e399a2598a235a032422 0       example\r
985 100644 263414f423d0e4d70dae8fe53fa34614ff3e2860 1       hello\r
986 100644 06fa6a24256dc7e560efa5687fa84b51f0263c3a 2       hello\r
987 100644 cc44c73eb783565da5831b4d820c962954019b69 3       hello</literallayout>\r
988 <simpara>This is the state of the index file and the working file after\r
989 <emphasis>git-merge</emphasis> returns control back to you, leaving the conflicting\r
990 merge for you to resolve.  Notice that the path <literal>hello</literal> is still\r
991 unmerged, and what you see with <emphasis>git-diff</emphasis> at this point is\r
992 differences since stage 2 (i.e. your version).</simpara>\r
993 </simplesect>\r
994 <simplesect id="_publishing_your_work">\r
995 <title>Publishing your work</title>\r
996 <simpara>So, we can use somebody else&#8217;s work from a remote repository, but\r
997 how can <emphasis role="strong">you</emphasis> prepare a repository to let other people pull from\r
998 it?</simpara>\r
999 <simpara>You do your real work in your working tree that has your\r
1000 primary repository hanging under it as its <literal>.git</literal> subdirectory.\r
1001 You <emphasis role="strong">could</emphasis> make that repository accessible remotely and ask\r
1002 people to pull from it, but in practice that is not the way\r
1003 things are usually done. A recommended way is to have a public\r
1004 repository, make it reachable by other people, and when the\r
1005 changes you made in your primary working tree are in good shape,\r
1006 update the public repository from it. This is often called\r
1007 <emphasis>pushing</emphasis>.</simpara>\r
1008 <note><simpara>This public repository could further be mirrored, and that is\r
1009 how git repositories at <literal>kernel.org</literal> are managed.</simpara></note>\r
1010 <simpara>Publishing the changes from your local (private) repository to\r
1011 your remote (public) repository requires a write privilege on\r
1012 the remote machine. You need to have an SSH account there to\r
1013 run a single command, <emphasis>git-receive-pack</emphasis>.</simpara>\r
1014 <simpara>First, you need to create an empty repository on the remote\r
1015 machine that will house your public repository. This empty\r
1016 repository will be populated and be kept up-to-date by pushing\r
1017 into it later. Obviously, this repository creation needs to be\r
1018 done only once.</simpara>\r
1019 <note><simpara><emphasis>git-push</emphasis> uses a pair of programs,\r
1020 <emphasis>git-send-pack</emphasis> on your local machine, and <emphasis>git-receive-pack</emphasis>\r
1021 on the remote machine. The communication between the two over\r
1022 the network internally uses an SSH connection.</simpara></note>\r
1023 <simpara>Your private repository&#8217;s git directory is usually <literal>.git</literal>, but\r
1024 your public repository is often named after the project name,\r
1025 i.e. <literal>&lt;project&gt;.git</literal>. Let&#8217;s create such a public repository for\r
1026 project <literal>my-git</literal>. After logging into the remote machine, create\r
1027 an empty directory:</simpara>\r
1028 <literallayout>$ mkdir my-git.git</literallayout>\r
1029 <simpara>Then, make that directory into a git repository by running\r
1030 <emphasis>git-init</emphasis>, but this time, since its name is not the usual\r
1031 <literal>.git</literal>, we do things slightly differently:</simpara>\r
1032 <literallayout>$ GIT_DIR=my-git.git git init</literallayout>\r
1033 <simpara>Make sure this directory is available for others you want your\r
1034 changes to be pulled via the transport of your choice. Also\r
1035 you need to make sure that you have the <emphasis>git-receive-pack</emphasis>\r
1036 program on the <literal>$PATH</literal>.</simpara>\r
1037 <note><simpara>Many installations of sshd do not invoke your shell as the login\r
1038 shell when you directly run programs; what this means is that if\r
1039 your login shell is <emphasis>bash</emphasis>, only <literal>.bashrc</literal> is read and not\r
1040 <literal>.bash_profile</literal>. As a workaround, make sure <literal>.bashrc</literal> sets up\r
1041 <literal>$PATH</literal> so that you can run <emphasis>git-receive-pack</emphasis> program.</simpara></note>\r
1042 <note><simpara>If you plan to publish this repository to be accessed over http,\r
1043 you should do <literal>mv my-git.git/hooks/post-update.sample\r
1044 my-git.git/hooks/post-update</literal> at this point.\r
1045 This makes sure that every time you push into this\r
1046 repository, <literal>git update-server-info</literal> is run.</simpara></note>\r
1047 <simpara>Your "public repository" is now ready to accept your changes.\r
1048 Come back to the machine you have your private repository. From\r
1049 there, run this command:</simpara>\r
1050 <literallayout>$ git push &lt;public-host&gt;:/path/to/my-git.git master</literallayout>\r
1051 <simpara>This synchronizes your public repository to match the named\r
1052 branch head (i.e. <literal>master</literal> in this case) and objects reachable\r
1053 from them in your current repository.</simpara>\r
1054 <simpara>As a real example, this is how I update my public git\r
1055 repository. Kernel.org mirror network takes care of the\r
1056 propagation to other publicly visible machines:</simpara>\r
1057 <literallayout>$ git push master.kernel.org:/pub/scm/git/git.git/</literallayout>\r
1058 </simplesect>\r
1059 <simplesect id="_packing_your_repository">\r
1060 <title>Packing your repository</title>\r
1061 <simpara>Earlier, we saw that one file under <literal>.git/objects/??/</literal> directory\r
1062 is stored for each git object you create. This representation\r
1063 is efficient to create atomically and safely, but\r
1064 not so convenient to transport over the network. Since git objects are\r
1065 immutable once they are created, there is a way to optimize the\r
1066 storage by "packing them together". The command</simpara>\r
1067 <literallayout>$ git repack</literallayout>\r
1068 <simpara>will do it for you. If you followed the tutorial examples, you\r
1069 would have accumulated about 17 objects in <literal>.git/objects/??/</literal>\r
1070 directories by now. <emphasis>git-repack</emphasis> tells you how many objects it\r
1071 packed, and stores the packed file in <literal>.git/objects/pack</literal>\r
1072 directory.</simpara>\r
1073 <note><simpara>You will see two files, <literal>pack-*.pack</literal> and <literal>pack-\*.idx</literal>,\r
1074 in <literal>.git/objects/pack</literal> directory. They are closely related to\r
1075 each other, and if you ever copy them by hand to a different\r
1076 repository for whatever reason, you should make sure you copy\r
1077 them together. The former holds all the data from the objects\r
1078 in the pack, and the latter holds the index for random\r
1079 access.</simpara></note>\r
1080 <simpara>If you are paranoid, running <emphasis>git-verify-pack</emphasis> command would\r
1081 detect if you have a corrupt pack, but do not worry too much.\r
1082 Our programs are always perfect ;-).</simpara>\r
1083 <simpara>Once you have packed objects, you do not need to leave the\r
1084 unpacked objects that are contained in the pack file anymore.</simpara>\r
1085 <literallayout>$ git prune-packed</literallayout>\r
1086 <simpara>would remove them for you.</simpara>\r
1087 <simpara>You can try running <literal>find .git/objects -type f</literal> before and after\r
1088 you run <literal>git prune-packed</literal> if you are curious.  Also <literal>git\r
1089 count-objects</literal> would tell you how many unpacked objects are in\r
1090 your repository and how much space they are consuming.</simpara>\r
1091 <note><simpara><literal>git pull</literal> is slightly cumbersome for HTTP transport, as a\r
1092 packed repository may contain relatively few objects in a\r
1093 relatively large pack. If you expect many HTTP pulls from your\r
1094 public repository you might want to repack &amp; prune often, or\r
1095 never.</simpara></note>\r
1096 <simpara>If you run <literal>git repack</literal> again at this point, it will say\r
1097 "Nothing new to pack.". Once you continue your development and\r
1098 accumulate the changes, running <literal>git repack</literal> again will create a\r
1099 new pack, that contains objects created since you packed your\r
1100 repository the last time. We recommend that you pack your project\r
1101 soon after the initial import (unless you are starting your\r
1102 project from scratch), and then run <literal>git repack</literal> every once in a\r
1103 while, depending on how active your project is.</simpara>\r
1104 <simpara>When a repository is synchronized via <literal>git push</literal> and <literal>git pull</literal>\r
1105 objects packed in the source repository are usually stored\r
1106 unpacked in the destination, unless rsync transport is used.\r
1107 While this allows you to use different packing strategies on\r
1108 both ends, it also means you may need to repack both\r
1109 repositories every once in a while.</simpara>\r
1110 </simplesect>\r
1111 <simplesect id="_working_with_others">\r
1112 <title>Working with Others</title>\r
1113 <simpara>Although git is a truly distributed system, it is often\r
1114 convenient to organize your project with an informal hierarchy\r
1115 of developers. Linux kernel development is run this way. There\r
1116 is a nice illustration (page 17, "Merges to Mainline") in\r
1117 <ulink url="http://www.xenotime.net/linux/mentor/linux-mentoring-2006.pdf">Randy Dunlap&#8217;s presentation</ulink>.</simpara>\r
1118 <simpara>It should be stressed that this hierarchy is purely <emphasis role="strong">informal</emphasis>.\r
1119 There is nothing fundamental in git that enforces the "chain of\r
1120 patch flow" this hierarchy implies. You do not have to pull\r
1121 from only one remote repository.</simpara>\r
1122 <simpara>A recommended workflow for a "project lead" goes like this:</simpara>\r
1123 <orderedlist numeration="arabic">\r
1124 <listitem>\r
1125 <simpara>\r
1126 Prepare your primary repository on your local machine. Your\r
1127    work is done there.\r
1128 </simpara>\r
1129 </listitem>\r
1130 <listitem>\r
1131 <simpara>\r
1132 Prepare a public repository accessible to others.\r
1133 </simpara>\r
1134 <simpara>If other people are pulling from your repository over dumb\r
1135 transport protocols (HTTP), you need to keep this repository\r
1136 <emphasis>dumb transport friendly</emphasis>.  After <literal>git init</literal>,\r
1137 <literal>$GIT_DIR/hooks/post-update.sample</literal> copied from the standard templates\r
1138 would contain a call to <emphasis>git-update-server-info</emphasis>\r
1139 but you need to manually enable the hook with\r
1140 <literal>mv post-update.sample post-update</literal>.  This makes sure\r
1141 <emphasis>git-update-server-info</emphasis> keeps the necessary files up-to-date.</simpara>\r
1142 </listitem>\r
1143 <listitem>\r
1144 <simpara>\r
1145 Push into the public repository from your primary\r
1146    repository.\r
1147 </simpara>\r
1148 </listitem>\r
1149 <listitem>\r
1150 <simpara>\r
1151 <emphasis>git-repack</emphasis> the public repository. This establishes a big\r
1152    pack that contains the initial set of objects as the\r
1153    baseline, and possibly <emphasis>git-prune</emphasis> if the transport\r
1154    used for pulling from your repository supports packed\r
1155    repositories.\r
1156 </simpara>\r
1157 </listitem>\r
1158 <listitem>\r
1159 <simpara>\r
1160 Keep working in your primary repository. Your changes\r
1161    include modifications of your own, patches you receive via\r
1162    e-mails, and merges resulting from pulling the "public"\r
1163    repositories of your "subsystem maintainers".\r
1164 </simpara>\r
1165 <simpara>You can repack this private repository whenever you feel like.</simpara>\r
1166 </listitem>\r
1167 <listitem>\r
1168 <simpara>\r
1169 Push your changes to the public repository, and announce it\r
1170    to the public.\r
1171 </simpara>\r
1172 </listitem>\r
1173 <listitem>\r
1174 <simpara>\r
1175 Every once in a while, <emphasis>git-repack</emphasis> the public repository.\r
1176    Go back to step 5. and continue working.\r
1177 </simpara>\r
1178 </listitem>\r
1179 </orderedlist>\r
1180 <simpara>A recommended work cycle for a "subsystem maintainer" who works\r
1181 on that project and has an own "public repository" goes like this:</simpara>\r
1182 <orderedlist numeration="arabic">\r
1183 <listitem>\r
1184 <simpara>\r
1185 Prepare your work repository, by <emphasis>git-clone</emphasis> the public\r
1186    repository of the "project lead". The URL used for the\r
1187    initial cloning is stored in the remote.origin.url\r
1188    configuration variable.\r
1189 </simpara>\r
1190 </listitem>\r
1191 <listitem>\r
1192 <simpara>\r
1193 Prepare a public repository accessible to others, just like\r
1194    the "project lead" person does.\r
1195 </simpara>\r
1196 </listitem>\r
1197 <listitem>\r
1198 <simpara>\r
1199 Copy over the packed files from "project lead" public\r
1200    repository to your public repository, unless the "project\r
1201    lead" repository lives on the same machine as yours.  In the\r
1202    latter case, you can use <literal>objects/info/alternates</literal> file to\r
1203    point at the repository you are borrowing from.\r
1204 </simpara>\r
1205 </listitem>\r
1206 <listitem>\r
1207 <simpara>\r
1208 Push into the public repository from your primary\r
1209    repository. Run <emphasis>git-repack</emphasis>, and possibly <emphasis>git-prune</emphasis> if the\r
1210    transport used for pulling from your repository supports\r
1211    packed repositories.\r
1212 </simpara>\r
1213 </listitem>\r
1214 <listitem>\r
1215 <simpara>\r
1216 Keep working in your primary repository. Your changes\r
1217    include modifications of your own, patches you receive via\r
1218    e-mails, and merges resulting from pulling the "public"\r
1219    repositories of your "project lead" and possibly your\r
1220    "sub-subsystem maintainers".\r
1221 </simpara>\r
1222 <simpara>You can repack this private repository whenever you feel\r
1223 like.</simpara>\r
1224 </listitem>\r
1225 <listitem>\r
1226 <simpara>\r
1227 Push your changes to your public repository, and ask your\r
1228    "project lead" and possibly your "sub-subsystem\r
1229    maintainers" to pull from it.\r
1230 </simpara>\r
1231 </listitem>\r
1232 <listitem>\r
1233 <simpara>\r
1234 Every once in a while, <emphasis>git-repack</emphasis> the public repository.\r
1235    Go back to step 5. and continue working.\r
1236 </simpara>\r
1237 </listitem>\r
1238 </orderedlist>\r
1239 <simpara>A recommended work cycle for an "individual developer" who does\r
1240 not have a "public" repository is somewhat different. It goes\r
1241 like this:</simpara>\r
1242 <orderedlist numeration="arabic">\r
1243 <listitem>\r
1244 <simpara>\r
1245 Prepare your work repository, by <emphasis>git-clone</emphasis> the public\r
1246    repository of the "project lead" (or a "subsystem\r
1247    maintainer", if you work on a subsystem). The URL used for\r
1248    the initial cloning is stored in the remote.origin.url\r
1249    configuration variable.\r
1250 </simpara>\r
1251 </listitem>\r
1252 <listitem>\r
1253 <simpara>\r
1254 Do your work in your repository on <emphasis>master</emphasis> branch.\r
1255 </simpara>\r
1256 </listitem>\r
1257 <listitem>\r
1258 <simpara>\r
1259 Run <literal>git fetch origin</literal> from the public repository of your\r
1260    upstream every once in a while. This does only the first\r
1261    half of <literal>git pull</literal> but does not merge. The head of the\r
1262    public repository is stored in <literal>.git/refs/remotes/origin/master</literal>.\r
1263 </simpara>\r
1264 </listitem>\r
1265 <listitem>\r
1266 <simpara>\r
1267 Use <literal>git cherry origin</literal> to see which ones of your patches\r
1268    were accepted, and/or use <literal>git rebase origin</literal> to port your\r
1269    unmerged changes forward to the updated upstream.\r
1270 </simpara>\r
1271 </listitem>\r
1272 <listitem>\r
1273 <simpara>\r
1274 Use <literal>git format-patch origin</literal> to prepare patches for e-mail\r
1275    submission to your upstream and send it out. Go back to\r
1276    step 2. and continue.\r
1277 </simpara>\r
1278 </listitem>\r
1279 </orderedlist>\r
1280 </simplesect>\r
1281 <simplesect id="_working_with_others_shared_repository_style">\r
1282 <title>Working with Others, Shared Repository Style</title>\r
1283 <simpara>If you are coming from CVS background, the style of cooperation\r
1284 suggested in the previous section may be new to you. You do not\r
1285 have to worry. git supports "shared public repository" style of\r
1286 cooperation you are probably more familiar with as well.</simpara>\r
1287 <simpara>See <xref linkend="gitcvs-migration(7)"/> for the details.</simpara>\r
1288 </simplesect>\r
1289 <simplesect id="_bundling_your_work_together">\r
1290 <title>Bundling your work together</title>\r
1291 <simpara>It is likely that you will be working on more than one thing at\r
1292 a time.  It is easy to manage those more-or-less independent tasks\r
1293 using branches with git.</simpara>\r
1294 <simpara>We have already seen how branches work previously,\r
1295 with "fun and work" example using two branches.  The idea is the\r
1296 same if there are more than two branches.  Let&#8217;s say you started\r
1297 out from "master" head, and have some new code in the "master"\r
1298 branch, and two independent fixes in the "commit-fix" and\r
1299 "diff-fix" branches:</simpara>\r
1300 <literallayout>$ git show-branch\r
1301 ! [commit-fix] Fix commit message normalization.\r
1302  ! [diff-fix] Fix rename detection.\r
1303   * [master] Release candidate #1\r
1304 ---\r
1305  +  [diff-fix] Fix rename detection.\r
1306  +  [diff-fix~1] Better common substring algorithm.\r
1307 +   [commit-fix] Fix commit message normalization.\r
1308   * [master] Release candidate #1\r
1309 ++* [diff-fix~2] Pretty-print messages.</literallayout>\r
1310 <simpara>Both fixes are tested well, and at this point, you want to merge\r
1311 in both of them.  You could merge in <emphasis>diff-fix</emphasis> first and then\r
1312 <emphasis>commit-fix</emphasis> next, like this:</simpara>\r
1313 <literallayout>$ git merge -m "Merge fix in diff-fix" diff-fix\r
1314 $ git merge -m "Merge fix in commit-fix" commit-fix</literallayout>\r
1315 <simpara>Which would result in:</simpara>\r
1316 <literallayout>$ git show-branch\r
1317 ! [commit-fix] Fix commit message normalization.\r
1318  ! [diff-fix] Fix rename detection.\r
1319   * [master] Merge fix in commit-fix\r
1320 ---\r
1321   - [master] Merge fix in commit-fix\r
1322 + * [commit-fix] Fix commit message normalization.\r
1323   - [master~1] Merge fix in diff-fix\r
1324  +* [diff-fix] Fix rename detection.\r
1325  +* [diff-fix~1] Better common substring algorithm.\r
1326   * [master~2] Release candidate #1\r
1327 ++* [master~3] Pretty-print messages.</literallayout>\r
1328 <simpara>However, there is no particular reason to merge in one branch\r
1329 first and the other next, when what you have are a set of truly\r
1330 independent changes (if the order mattered, then they are not\r
1331 independent by definition).  You could instead merge those two\r
1332 branches into the current branch at once.  First let&#8217;s undo what\r
1333 we just did and start over.  We would want to get the master\r
1334 branch before these two merges by resetting it to <emphasis>master~2</emphasis>:</simpara>\r
1335 <literallayout>$ git reset --hard master~2</literallayout>\r
1336 <simpara>You can make sure <literal>git show-branch</literal> matches the state before\r
1337 those two <emphasis>git-merge</emphasis> you just did.  Then, instead of running\r
1338 two <emphasis>git-merge</emphasis> commands in a row, you would merge these two\r
1339 branch heads (this is known as <emphasis>making an Octopus</emphasis>):</simpara>\r
1340 <literallayout>$ git merge commit-fix diff-fix\r
1341 $ git show-branch\r
1342 ! [commit-fix] Fix commit message normalization.\r
1343  ! [diff-fix] Fix rename detection.\r
1344   * [master] Octopus merge of branches 'diff-fix' and 'commit-fix'\r
1345 ---\r
1346   - [master] Octopus merge of branches 'diff-fix' and 'commit-fix'\r
1347 + * [commit-fix] Fix commit message normalization.\r
1348  +* [diff-fix] Fix rename detection.\r
1349  +* [diff-fix~1] Better common substring algorithm.\r
1350   * [master~1] Release candidate #1\r
1351 ++* [master~2] Pretty-print messages.</literallayout>\r
1352 <simpara>Note that you should not do Octopus because you can.  An octopus\r
1353 is a valid thing to do and often makes it easier to view the\r
1354 commit history if you are merging more than two independent\r
1355 changes at the same time.  However, if you have merge conflicts\r
1356 with any of the branches you are merging in and need to hand\r
1357 resolve, that is an indication that the development happened in\r
1358 those branches were not independent after all, and you should\r
1359 merge two at a time, documenting how you resolved the conflicts,\r
1360 and the reason why you preferred changes made in one side over\r
1361 the other.  Otherwise it would make the project history harder\r
1362 to follow, not easier.</simpara>\r
1363 </simplesect>\r
1364 <simplesect id="_see_also">\r
1365 <title>SEE ALSO</title>\r
1366 <simpara><xref linkend="gittutorial(7)"/>,\r
1367 <xref linkend="gittutorial-2(7)"/>,\r
1368 <xref linkend="gitcvs-migration(7)"/>,\r
1369 <xref linkend="git-help(1)"/>,\r
1370 <ulink url="everyday.html">Everyday git</ulink>,\r
1371 <ulink url="user-manual.html">The Git User&#8217;s Manual</ulink></simpara>\r
1372 </simplesect>\r
1373 <simplesect id="_git">\r
1374 <title>GIT</title>\r
1375 <simpara>Part of the <xref linkend="git(1)"/> suite.</simpara>\r
1376 </simplesect>\r
1377 </article>\r