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