blob: 95bed9f50f137f6f544574c80fce316bcc090556 [file] [log] [blame]
Junio C Hamano1a4e8412005-12-27 08:17:231<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
2 "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
3<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
4<head>
5<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
Junio C Hamanodecf50e2006-03-05 10:51:146<meta name="generator" content="AsciiDoc 7.0.2" />
Junio C Hamano1a4e8412005-12-27 08:17:237<style type="text/css">
8/* Debug borders */
9p, li, dt, dd, div, pre, h1, h2, h3, h4, h5, h6 {
10/*
11 border: 1px solid red;
12*/
13}
14
15body {
16 margin: 1em 5% 1em 5%;
17}
18
19a { color: blue; }
20a:visited { color: fuchsia; }
21
22em {
23 font-style: italic;
24}
25
26strong {
27 font-weight: bold;
28}
29
30tt {
31 color: navy;
32}
33
34h1, h2, h3, h4, h5, h6 {
35 color: #527bbd;
36 font-family: sans-serif;
37 margin-top: 1.2em;
38 margin-bottom: 0.5em;
39 line-height: 1.3;
40}
41
42h1 {
43 border-bottom: 2px solid silver;
44}
45h2 {
46 border-bottom: 2px solid silver;
47 padding-top: 0.5em;
48}
49
50div.sectionbody {
51 font-family: serif;
52 margin-left: 0;
53}
54
55hr {
56 border: 1px solid silver;
57}
58
59p {
60 margin-top: 0.5em;
61 margin-bottom: 0.5em;
62}
63
64pre {
65 padding: 0;
66 margin: 0;
67}
68
69span#author {
70 color: #527bbd;
71 font-family: sans-serif;
72 font-weight: bold;
73 font-size: 1.2em;
74}
75span#email {
76}
77span#revision {
78 font-family: sans-serif;
79}
80
81div#footer {
82 font-family: sans-serif;
83 font-size: small;
84 border-top: 2px solid silver;
85 padding-top: 0.5em;
86 margin-top: 4.0em;
87}
88div#footer-text {
89 float: left;
90 padding-bottom: 0.5em;
91}
92div#footer-badges {
93 float: right;
94 padding-bottom: 0.5em;
95}
96
97div#preamble,
98div.tableblock, div.imageblock, div.exampleblock, div.verseblock,
99div.quoteblock, div.literalblock, div.listingblock, div.sidebarblock,
100div.admonitionblock {
101 margin-right: 10%;
102 margin-top: 1.5em;
103 margin-bottom: 1.5em;
104}
105div.admonitionblock {
106 margin-top: 2.5em;
107 margin-bottom: 2.5em;
108}
109
110div.content { /* Block element content. */
111 padding: 0;
112}
113
114/* Block element titles. */
115div.title, caption.title {
116 font-family: sans-serif;
117 font-weight: bold;
118 text-align: left;
119 margin-top: 1.0em;
120 margin-bottom: 0.5em;
121}
122div.title + * {
123 margin-top: 0;
124}
125
126td div.title:first-child {
127 margin-top: 0.0em;
128}
129div.content div.title:first-child {
130 margin-top: 0.0em;
131}
132div.content + div.title {
133 margin-top: 0.0em;
134}
135
136div.sidebarblock > div.content {
137 background: #ffffee;
138 border: 1px solid silver;
139 padding: 0.5em;
140}
141
142div.listingblock > div.content {
143 border: 1px solid silver;
144 background: #f4f4f4;
145 padding: 0.5em;
146}
147
148div.quoteblock > div.content {
149 padding-left: 2.0em;
150}
151div.quoteblock .attribution {
152 text-align: right;
153}
154
155div.admonitionblock .icon {
156 vertical-align: top;
157 font-size: 1.1em;
158 font-weight: bold;
159 text-decoration: underline;
160 color: #527bbd;
161 padding-right: 0.5em;
162}
163div.admonitionblock td.content {
164 padding-left: 0.5em;
165 border-left: 2px solid silver;
166}
167
168div.exampleblock > div.content {
169 border-left: 2px solid silver;
170 padding: 0.5em;
171}
172
173div.verseblock div.content {
174 white-space: pre;
175}
176
177div.imageblock div.content { padding-left: 0; }
178div.imageblock img { border: 1px solid silver; }
179span.image img { border-style: none; }
180
181dl {
182 margin-top: 0.8em;
183 margin-bottom: 0.8em;
184}
185dt {
186 margin-top: 0.5em;
187 margin-bottom: 0;
188 font-style: italic;
189}
190dd > *:first-child {
191 margin-top: 0;
192}
193
194ul, ol {
195 list-style-position: outside;
196}
197ol.olist2 {
198 list-style-type: lower-alpha;
199}
200
201div.tableblock > table {
202 border-color: #527bbd;
203 border-width: 3px;
204}
205thead {
206 font-family: sans-serif;
207 font-weight: bold;
208}
209tfoot {
210 font-weight: bold;
211}
212
213div.hlist {
214 margin-top: 0.8em;
215 margin-bottom: 0.8em;
216}
217td.hlist1 {
218 vertical-align: top;
219 font-style: italic;
220 padding-right: 0.8em;
221}
222td.hlist2 {
223 vertical-align: top;
224}
225
226@media print {
227 div#footer-badges { display: none; }
228}
229/* Workarounds for IE6's broken and incomplete CSS2. */
230
231div.sidebar-content {
232 background: #ffffee;
233 border: 1px solid silver;
234 padding: 0.5em;
235}
236div.sidebar-title, div.image-title {
237 font-family: sans-serif;
238 font-weight: bold;
239 margin-top: 0.0em;
240 margin-bottom: 0.5em;
241}
242
243div.listingblock div.content {
244 border: 1px solid silver;
245 background: #f4f4f4;
246 padding: 0.5em;
247}
248
249div.quoteblock-content {
250 padding-left: 2.0em;
251}
252
253div.exampleblock-content {
254 border-left: 2px solid silver;
255 padding-left: 0.5em;
256}
257</style>
Junio C Hamano67fad6d2007-05-06 08:53:12258<title>A tutorial introduction to git (for version 1.5.1 or newer)</title>
Junio C Hamano1a4e8412005-12-27 08:17:23259</head>
260<body>
261<div id="header">
Junio C Hamano67fad6d2007-05-06 08:53:12262<h1>A tutorial introduction to git (for version 1.5.1 or newer)</h1>
Junio C Hamano1a4e8412005-12-27 08:17:23263</div>
Junio C Hamanoc2b0a492006-01-23 07:54:36264<div id="preamble">
Junio C Hamano1a4e8412005-12-27 08:17:23265<div class="sectionbody">
Junio C Hamanoc2b0a492006-01-23 07:54:36266<p>This tutorial explains how to import a new project into git, make
267changes to it, and share changes with other developers.</p>
Junio C Hamanoed7f4f62007-05-20 09:09:09268<p>If you are instead primarily interested in using git to fetch a project,
269for example, to test the latest version, you may prefer to start with
270the first two chapters of <a href="user-manual.html">The Git User's Manual</a>.</p>
Junio C Hamanoc2b0a492006-01-23 07:54:36271<p>First, note that you can get documentation for a command such as "git
272diff" with:</p>
Junio C Hamano1a4e8412005-12-27 08:17:23273<div class="listingblock">
274<div class="content">
Junio C Hamanoc2b0a492006-01-23 07:54:36275<pre><tt>$ man git-diff</tt></pre>
Junio C Hamano1a4e8412005-12-27 08:17:23276</div></div>
Junio C Hamanoedd2b0a2007-01-15 06:12:45277<p>It is a good idea to introduce yourself to git with your name and
278public email address before doing any operation. The easiest
279way to do so is:</p>
Junio C Hamano699660b2006-11-29 20:40:10280<div class="listingblock">
281<div class="content">
Junio C Hamano7ad22dc2007-01-29 02:55:48282<pre><tt>$ git config --global user.name "Your Name Comes Here"
283$ git config --global user.email you@yourdomain.example.com</tt></pre>
Junio C Hamano699660b2006-11-29 20:40:10284</div></div>
Junio C Hamanoc2b0a492006-01-23 07:54:36285</div>
286</div>
287<h2>Importing a new project</h2>
288<div class="sectionbody">
289<p>Assume you have a tarball project.tar.gz with your initial work. You
290can place it under git revision control as follows.</p>
291<div class="listingblock">
292<div class="content">
293<pre><tt>$ tar xzf project.tar.gz
294$ cd project
Junio C Hamanofc4d38c2007-01-08 06:53:32295$ git init</tt></pre>
Junio C Hamanoc2b0a492006-01-23 07:54:36296</div></div>
297<p>Git will reply</p>
Junio C Hamano1a4e8412005-12-27 08:17:23298<div class="listingblock">
299<div class="content">
Junio C Hamano7d23f5e2006-12-16 07:44:04300<pre><tt>Initialized empty Git repository in .git/</tt></pre>
Junio C Hamano1a4e8412005-12-27 08:17:23301</div></div>
Junio C Hamanoc2b0a492006-01-23 07:54:36302<p>You've now initialized the working directory&#8212;you may notice a new
Junio C Hamanoed7f4f62007-05-20 09:09:09303directory created, named ".git".</p>
304<p>Next, tell git to take a snapshot of the contents of all files under the
305current directory (note the <em>.</em>), with <a href="git-add.html">git-add(1)</a>:</p>
Junio C Hamanoc2b0a492006-01-23 07:54:36306<div class="listingblock">
307<div class="content">
308<pre><tt>$ git add .</tt></pre>
309</div></div>
Junio C Hamanoed7f4f62007-05-20 09:09:09310<p>This snapshot is now stored in a temporary staging area which git calls
311the "index". You can permanently store the contents of the index in the
312repository with <a href="git-commit.html">git-commit(1)</a>:</p>
Junio C Hamanoc2b0a492006-01-23 07:54:36313<div class="listingblock">
314<div class="content">
Junio C Hamano699660b2006-11-29 20:40:10315<pre><tt>$ git commit</tt></pre>
Junio C Hamanoc2b0a492006-01-23 07:54:36316</div></div>
Junio C Hamanoed7f4f62007-05-20 09:09:09317<p>This will prompt you for a commit message. You've now stored the first
318version of your project in git.</p>
Junio C Hamano79770b62007-01-07 07:43:58319</div>
320<h2>Making changes</h2>
321<div class="sectionbody">
Junio C Hamanoed7f4f62007-05-20 09:09:09322<p>Modify some files, then add their updated contents to the index:</p>
Junio C Hamanoc2b0a492006-01-23 07:54:36323<div class="listingblock">
324<div class="content">
Junio C Hamanoed7f4f62007-05-20 09:09:09325<pre><tt>$ git add file1 file2 file3</tt></pre>
Junio C Hamanoc2b0a492006-01-23 07:54:36326</div></div>
Junio C Hamanoed7f4f62007-05-20 09:09:09327<p>You are now ready to commit. You can see what is about to be committed
328using <a href="git-diff.html">git-diff(1)</a> with the &#8212;cached option:</p>
Junio C Hamanoc2b0a492006-01-23 07:54:36329<div class="listingblock">
330<div class="content">
Junio C Hamanoed7f4f62007-05-20 09:09:09331<pre><tt>$ git diff --cached</tt></pre>
332</div></div>
333<p>(Without &#8212;cached, <a href="git-diff.html">git-diff(1)</a> will show you any changes that
334you've made but not yet added to the index.) You can also get a brief
335summary of the situation with <a href="git-status.html">git-status(1)</a>:</p>
336<div class="listingblock">
337<div class="content">
338<pre><tt>$ git status
339# On branch master
340# Changes to be committed:
341# (use "git reset HEAD &lt;file&gt;..." to unstage)
342#
343# modified: file1
344# modified: file2
345# modified: file3
346#</tt></pre>
347</div></div>
348<p>If you need to make any further adjustments, do so now, and then add any
349newly modified content to the index. Finally, commit your changes with:</p>
350<div class="listingblock">
351<div class="content">
352<pre><tt>$ git commit</tt></pre>
Junio C Hamanoc2b0a492006-01-23 07:54:36353</div></div>
Junio C Hamanoeb692952007-01-03 22:02:12354<p>This will again prompt your for a message describing the change, and then
Junio C Hamanoed7f4f62007-05-20 09:09:09355record a new version of the project.</p>
Junio C Hamano79770b62007-01-07 07:43:58356<p>Alternatively, instead of running <tt>git add</tt> beforehand, you can use</p>
Junio C Hamano699660b2006-11-29 20:40:10357<div class="listingblock">
358<div class="content">
359<pre><tt>$ git commit -a</tt></pre>
360</div></div>
Junio C Hamanoed7f4f62007-05-20 09:09:09361<p>which will automatically notice any modified (but not new) files, add
362them to the index, and commit, all in one step.</p>
Junio C Hamanoc2b0a492006-01-23 07:54:36363<p>A note on commit messages: Though not required, it's a good idea to
364begin the commit message with a single short (less than 50 character)
365line summarizing the change, followed by a blank line and then a more
366thorough description. Tools that turn commits into email, for
Junio C Hamanoeb692952007-01-03 22:02:12367example, use the first line on the Subject: line and the rest of the
Junio C Hamanoc2b0a492006-01-23 07:54:36368commit in the body.</p>
Junio C Hamanoe7935c42006-12-13 21:32:17369</div>
370<h2>Git tracks content not files</h2>
371<div class="sectionbody">
Junio C Hamanoed7f4f62007-05-20 09:09:09372<p>Many revision control systems provide an "add" command that tells the
373system to start tracking changes to a new file. Git's "add" command
374does something simpler and more powerful: <tt>git add</tt> is used both for new
375and newly modified files, and in both cases it takes a snapshot of the
376given files and stages that content in the index, ready for inclusion in
377the next commit.</p>
Junio C Hamanoe7935c42006-12-13 21:32:17378</div>
Junio C Hamanof614c642007-06-11 01:21:54379<h2>Viewing project history</h2>
Junio C Hamanoe7935c42006-12-13 21:32:17380<div class="sectionbody">
Junio C Hamanoc2b0a492006-01-23 07:54:36381<p>At any point you can view the history of your changes using</p>
382<div class="listingblock">
383<div class="content">
Junio C Hamano6f8a7902006-05-22 01:10:13384<pre><tt>$ git log</tt></pre>
Junio C Hamanoc2b0a492006-01-23 07:54:36385</div></div>
386<p>If you also want to see complete diffs at each step, use</p>
387<div class="listingblock">
388<div class="content">
Junio C Hamano6f8a7902006-05-22 01:10:13389<pre><tt>$ git log -p</tt></pre>
Junio C Hamanoc2b0a492006-01-23 07:54:36390</div></div>
Junio C Hamanoeb692952007-01-03 22:02:12391<p>Often the overview of the change is useful to get a feel of
392each step</p>
393<div class="listingblock">
394<div class="content">
395<pre><tt>$ git log --stat --summary</tt></pre>
396</div></div>
Junio C Hamano1a4e8412005-12-27 08:17:23397</div>
Junio C Hamanoc2b0a492006-01-23 07:54:36398<h2>Managing branches</h2>
Junio C Hamano1a4e8412005-12-27 08:17:23399<div class="sectionbody">
Junio C Hamanoc2b0a492006-01-23 07:54:36400<p>A single git repository can maintain multiple branches of
401development. To create a new branch named "experimental", use</p>
Junio C Hamano1a4e8412005-12-27 08:17:23402<div class="listingblock">
403<div class="content">
Junio C Hamanoc2b0a492006-01-23 07:54:36404<pre><tt>$ git branch experimental</tt></pre>
Junio C Hamano1a4e8412005-12-27 08:17:23405</div></div>
Junio C Hamanoc2b0a492006-01-23 07:54:36406<p>If you now run</p>
Junio C Hamano1a4e8412005-12-27 08:17:23407<div class="listingblock">
408<div class="content">
409<pre><tt>$ git branch</tt></pre>
410</div></div>
Junio C Hamanoc2b0a492006-01-23 07:54:36411<p>you'll get a list of all existing branches:</p>
Junio C Hamano1a4e8412005-12-27 08:17:23412<div class="listingblock">
413<div class="content">
Junio C Hamanoc2b0a492006-01-23 07:54:36414<pre><tt> experimental
415* master</tt></pre>
Junio C Hamano1a4e8412005-12-27 08:17:23416</div></div>
Junio C Hamanoc2b0a492006-01-23 07:54:36417<p>The "experimental" branch is the one you just created, and the
418"master" branch is a default branch that was created for you
419automatically. The asterisk marks the branch you are currently on;
420type</p>
421<div class="listingblock">
422<div class="content">
423<pre><tt>$ git checkout experimental</tt></pre>
424</div></div>
425<p>to switch to the experimental branch. Now edit a file, commit the
426change, and switch back to the master branch:</p>
427<div class="listingblock">
428<div class="content">
429<pre><tt>(edit file)
430$ git commit -a
431$ git checkout master</tt></pre>
432</div></div>
433<p>Check that the change you made is no longer visible, since it was
434made on the experimental branch and you're back on the master branch.</p>
435<p>You can make a different change on the master branch:</p>
436<div class="listingblock">
437<div class="content">
438<pre><tt>(edit file)
439$ git commit -a</tt></pre>
440</div></div>
441<p>at this point the two branches have diverged, with different changes
Junio C Hamano0df34342006-11-22 08:28:50442made in each. To merge the changes made in experimental into master, run</p>
Junio C Hamanoc2b0a492006-01-23 07:54:36443<div class="listingblock">
444<div class="content">
Junio C Hamanoedd2b0a2007-01-15 06:12:45445<pre><tt>$ git merge experimental</tt></pre>
Junio C Hamanoc2b0a492006-01-23 07:54:36446</div></div>
447<p>If the changes don't conflict, you're done. If there are conflicts,
448markers will be left in the problematic files showing the conflict;</p>
449<div class="listingblock">
450<div class="content">
451<pre><tt>$ git diff</tt></pre>
452</div></div>
453<p>will show this. Once you've edited the files to resolve the
454conflicts,</p>
455<div class="listingblock">
456<div class="content">
457<pre><tt>$ git commit -a</tt></pre>
458</div></div>
459<p>will commit the result of the merge. Finally,</p>
460<div class="listingblock">
461<div class="content">
462<pre><tt>$ gitk</tt></pre>
463</div></div>
464<p>will show a nice graphical representation of the resulting history.</p>
Junio C Hamanoeb692952007-01-03 22:02:12465<p>At this point you could delete the experimental branch with</p>
466<div class="listingblock">
467<div class="content">
468<pre><tt>$ git branch -d experimental</tt></pre>
469</div></div>
470<p>This command ensures that the changes in the experimental branch are
471already in the current branch.</p>
Junio C Hamanoc2b0a492006-01-23 07:54:36472<p>If you develop on a branch crazy-idea, then regret it, you can always
473delete the branch with</p>
474<div class="listingblock">
475<div class="content">
476<pre><tt>$ git branch -D crazy-idea</tt></pre>
477</div></div>
478<p>Branches are cheap and easy, so this is a good way to try something
479out.</p>
Junio C Hamano1a4e8412005-12-27 08:17:23480</div>
Junio C Hamanoc2b0a492006-01-23 07:54:36481<h2>Using git for collaboration</h2>
Junio C Hamano1a4e8412005-12-27 08:17:23482<div class="sectionbody">
Junio C Hamanoc2b0a492006-01-23 07:54:36483<p>Suppose that Alice has started a new project with a git repository in
484/home/alice/project, and that Bob, who has a home directory on the
485same machine, wants to contribute.</p>
486<p>Bob begins with:</p>
Junio C Hamano1a4e8412005-12-27 08:17:23487<div class="listingblock">
488<div class="content">
Junio C Hamanoc2b0a492006-01-23 07:54:36489<pre><tt>$ git clone /home/alice/project myrepo</tt></pre>
Junio C Hamano1a4e8412005-12-27 08:17:23490</div></div>
Junio C Hamanoc2b0a492006-01-23 07:54:36491<p>This creates a new directory "myrepo" containing a clone of Alice's
492repository. The clone is on an equal footing with the original
Junio C Hamano341071d2006-06-04 07:24:48493project, possessing its own copy of the original project's history.</p>
Junio C Hamanoc2b0a492006-01-23 07:54:36494<p>Bob then makes some changes and commits them:</p>
Junio C Hamano1a4e8412005-12-27 08:17:23495<div class="listingblock">
496<div class="content">
Junio C Hamanoc2b0a492006-01-23 07:54:36497<pre><tt>(edit files)
498$ git commit -a
499(repeat as necessary)</tt></pre>
Junio C Hamano1a4e8412005-12-27 08:17:23500</div></div>
Junio C Hamanoc2b0a492006-01-23 07:54:36501<p>When he's ready, he tells Alice to pull changes from the repository
502at /home/bob/myrepo. She does this with:</p>
Junio C Hamano1a4e8412005-12-27 08:17:23503<div class="listingblock">
504<div class="content">
Junio C Hamanoc2b0a492006-01-23 07:54:36505<pre><tt>$ cd /home/alice/project
Junio C Hamanof98fd882006-11-26 07:28:29506$ git pull /home/bob/myrepo master</tt></pre>
Junio C Hamano1a4e8412005-12-27 08:17:23507</div></div>
Junio C Hamanof98fd882006-11-26 07:28:29508<p>This merges the changes from Bob's "master" branch into Alice's
509current branch. If Alice has made her own changes in the meantime,
510then she may need to manually fix any conflicts. (Note that the
511"master" argument in the above command is actually unnecessary, as it
512is the default.)</p>
513<p>The "pull" command thus performs two operations: it fetches changes
514from a remote branch, then merges them into the current branch.</p>
Junio C Hamano35bd0252007-01-17 05:40:22515<p>When you are working in a small closely knit group, it is not
516unusual to interact with the same repository over and over
517again. By defining <em>remote</em> repository shorthand, you can make
518it easier:</p>
Junio C Hamano1a4e8412005-12-27 08:17:23519<div class="listingblock">
520<div class="content">
Junio C Hamano35bd0252007-01-17 05:40:22521<pre><tt>$ git remote add bob /home/bob/myrepo</tt></pre>
Junio C Hamano1a4e8412005-12-27 08:17:23522</div></div>
Junio C Hamano2a8f6dc2007-07-09 08:48:38523<p>With this, Alice can perform the first operation alone using the
Junio C Hamano35bd0252007-01-17 05:40:22524"git fetch" command without merging them with her own branch,
525using:</p>
Junio C Hamano1a4e8412005-12-27 08:17:23526<div class="listingblock">
527<div class="content">
Junio C Hamano35bd0252007-01-17 05:40:22528<pre><tt>$ git fetch bob</tt></pre>
529</div></div>
530<p>Unlike the longhand form, when Alice fetches from Bob using a
531remote repository shorthand set up with <tt>git remote</tt>, what was
532fetched is stored in a remote tracking branch, in this case
533<tt>bob/master</tt>. So after this:</p>
534<div class="listingblock">
535<div class="content">
536<pre><tt>$ git log -p master..bob/master</tt></pre>
Junio C Hamano1a4e8412005-12-27 08:17:23537</div></div>
Junio C Hamanoc2b0a492006-01-23 07:54:36538<p>shows a list of all the changes that Bob made since he branched from
539Alice's master branch.</p>
Junio C Hamano35bd0252007-01-17 05:40:22540<p>After examining those changes, Alice
Junio C Hamanoedd2b0a2007-01-15 06:12:45541could merge the changes into her master branch:</p>
Junio C Hamano1a4e8412005-12-27 08:17:23542<div class="listingblock">
543<div class="content">
Junio C Hamano35bd0252007-01-17 05:40:22544<pre><tt>$ git merge bob/master</tt></pre>
Junio C Hamano1a4e8412005-12-27 08:17:23545</div></div>
Junio C Hamano35bd0252007-01-17 05:40:22546<p>This <tt>merge</tt> can also be done by <em>pulling from her own remote
547tracking branch</em>, like this:</p>
Junio C Hamanof98fd882006-11-26 07:28:29548<div class="listingblock">
549<div class="content">
Junio C Hamano35bd0252007-01-17 05:40:22550<pre><tt>$ git pull . remotes/bob/master</tt></pre>
Junio C Hamanof98fd882006-11-26 07:28:29551</div></div>
Junio C Hamano35bd0252007-01-17 05:40:22552<p>Note that git pull always merges into the current branch,
553regardless of what else is given on the commandline.</p>
Junio C Hamanoc2b0a492006-01-23 07:54:36554<p>Later, Bob can update his repo with Alice's latest changes using</p>
Junio C Hamano1a4e8412005-12-27 08:17:23555<div class="listingblock">
556<div class="content">
Junio C Hamanoc2b0a492006-01-23 07:54:36557<pre><tt>$ git pull</tt></pre>
Junio C Hamano1a4e8412005-12-27 08:17:23558</div></div>
Junio C Hamanoc2b0a492006-01-23 07:54:36559<p>Note that he doesn't need to give the path to Alice's repository;
560when Bob cloned Alice's repository, git stored the location of her
Junio C Hamanod3361ad2007-01-01 03:20:24561repository in the repository configuration, and that location is
562used for pulls:</p>
Junio C Hamano1a4e8412005-12-27 08:17:23563<div class="listingblock">
564<div class="content">
Junio C Hamano7ad22dc2007-01-29 02:55:48565<pre><tt>$ git config --get remote.origin.url
Junio C Hamano330aae62007-07-06 17:01:58566/home/alice/project</tt></pre>
Junio C Hamano1a4e8412005-12-27 08:17:23567</div></div>
Junio C Hamanod3361ad2007-01-01 03:20:24568<p>(The complete configuration created by git-clone is visible using
Junio C Hamano7ad22dc2007-01-29 02:55:48569"git config -l", and the <a href="git-config.html">git-config(1)</a> man page
Junio C Hamanod3361ad2007-01-01 03:20:24570explains the meaning of each option.)</p>
571<p>Git also keeps a pristine copy of Alice's master branch under the
572name "origin/master":</p>
573<div class="listingblock">
574<div class="content">
575<pre><tt>$ git branch -r
576 origin/master</tt></pre>
577</div></div>
Junio C Hamanoc2b0a492006-01-23 07:54:36578<p>If Bob later decides to work from a different host, he can still
579perform clones and pulls using the ssh protocol:</p>
Junio C Hamano1a4e8412005-12-27 08:17:23580<div class="listingblock">
581<div class="content">
Junio C Hamanoc2b0a492006-01-23 07:54:36582<pre><tt>$ git clone alice.org:/home/alice/project myrepo</tt></pre>
Junio C Hamano1a4e8412005-12-27 08:17:23583</div></div>
Junio C Hamanoc2b0a492006-01-23 07:54:36584<p>Alternatively, git has a native protocol, or can use rsync or http;
585see <a href="git-pull.html">git-pull(1)</a> for details.</p>
586<p>Git can also be used in a CVS-like mode, with a central repository
587that various users push changes to; see <a href="git-push.html">git-push(1)</a> and
588<a href="cvs-migration.html">git for CVS users</a>.</p>
Junio C Hamano1a4e8412005-12-27 08:17:23589</div>
Junio C Hamano6f8a7902006-05-22 01:10:13590<h2>Exploring history</h2>
Junio C Hamano1a4e8412005-12-27 08:17:23591<div class="sectionbody">
Junio C Hamano6f8a7902006-05-22 01:10:13592<p>Git history is represented as a series of interrelated commits. We
593have already seen that the git log command can list those commits.
594Note that first line of each git log entry also gives a name for the
595commit:</p>
Junio C Hamano1a4e8412005-12-27 08:17:23596<div class="listingblock">
597<div class="content">
Junio C Hamano6f8a7902006-05-22 01:10:13598<pre><tt>$ git log
599commit c82a22c39cbc32576f64f5c6b3f24b99ea8149c7
600Author: Junio C Hamano &lt;junkio@cox.net&gt;
601Date: Tue May 16 17:18:22 2006 -0700
602
603 merge-base: Clarify the comments on post processing.</tt></pre>
Junio C Hamano1a4e8412005-12-27 08:17:23604</div></div>
Junio C Hamano6f8a7902006-05-22 01:10:13605<p>We can give this name to git show to see the details about this
606commit.</p>
Junio C Hamano1a4e8412005-12-27 08:17:23607<div class="listingblock">
608<div class="content">
Junio C Hamano6f8a7902006-05-22 01:10:13609<pre><tt>$ git show c82a22c39cbc32576f64f5c6b3f24b99ea8149c7</tt></pre>
Junio C Hamano1a4e8412005-12-27 08:17:23610</div></div>
Junio C Hamanoeb692952007-01-03 22:02:12611<p>But there are other ways to refer to commits. You can use any initial
Junio C Hamano6f8a7902006-05-22 01:10:13612part of the name that is long enough to uniquely identify the commit:</p>
Junio C Hamanoc2b0a492006-01-23 07:54:36613<div class="listingblock">
614<div class="content">
Junio C Hamano6f8a7902006-05-22 01:10:13615<pre><tt>$ git show c82a22c39c # the first few characters of the name are
616 # usually enough
617$ git show HEAD # the tip of the current branch
618$ git show experimental # the tip of the "experimental" branch</tt></pre>
Junio C Hamanoc2b0a492006-01-23 07:54:36619</div></div>
Junio C Hamanoeb692952007-01-03 22:02:12620<p>Every commit usually has one "parent" commit
621which points to the previous state of the project:</p>
Junio C Hamanoc2b0a492006-01-23 07:54:36622<div class="listingblock">
623<div class="content">
Junio C Hamano6f8a7902006-05-22 01:10:13624<pre><tt>$ git show HEAD^ # to see the parent of HEAD
625$ git show HEAD^^ # to see the grandparent of HEAD
626$ git show HEAD~4 # to see the great-great grandparent of HEAD</tt></pre>
Junio C Hamanoc2b0a492006-01-23 07:54:36627</div></div>
Junio C Hamano6f8a7902006-05-22 01:10:13628<p>Note that merge commits may have more than one parent:</p>
Junio C Hamanoc2b0a492006-01-23 07:54:36629<div class="listingblock">
630<div class="content">
Junio C Hamano6f8a7902006-05-22 01:10:13631<pre><tt>$ git show HEAD^1 # show the first parent of HEAD (same as HEAD^)
632$ git show HEAD^2 # show the second parent of HEAD</tt></pre>
Junio C Hamanoc2b0a492006-01-23 07:54:36633</div></div>
Junio C Hamano6f8a7902006-05-22 01:10:13634<p>You can also give commits names of your own; after running</p>
Junio C Hamanoc2b0a492006-01-23 07:54:36635<div class="listingblock">
636<div class="content">
Junio C Hamano6f8a7902006-05-22 01:10:13637<pre><tt>$ git-tag v2.5 1b2e1d63ff</tt></pre>
Junio C Hamanoc2b0a492006-01-23 07:54:36638</div></div>
Junio C Hamano6f8a7902006-05-22 01:10:13639<p>you can refer to 1b2e1d63ff by the name "v2.5". If you intend to
640share this name with other people (for example, to identify a release
Junio C Hamanoc2b0a492006-01-23 07:54:36641version), you should create a "tag" object, and perhaps sign it; see
642<a href="git-tag.html">git-tag(1)</a> for details.</p>
Junio C Hamano6f8a7902006-05-22 01:10:13643<p>Any git command that needs to know a commit can take any of these
644names. For example:</p>
Junio C Hamanoc2b0a492006-01-23 07:54:36645<div class="listingblock">
646<div class="content">
Junio C Hamano6f8a7902006-05-22 01:10:13647<pre><tt>$ git diff v2.5 HEAD # compare the current HEAD to v2.5
648$ git branch stable v2.5 # start a new branch named "stable" based
649 # at v2.5
650$ git reset --hard HEAD^ # reset your current branch and working
Junio C Hamano33db4372006-06-07 19:51:45651 # directory to its state at HEAD^</tt></pre>
Junio C Hamanoc2b0a492006-01-23 07:54:36652</div></div>
Junio C Hamano6f8a7902006-05-22 01:10:13653<p>Be careful with that last command: in addition to losing any changes
654in the working directory, it will also remove all later commits from
655this branch. If this branch is the only branch containing those
Junio C Hamanoee1e4282007-02-04 08:32:04656commits, they will be lost. Also, don't use "git reset" on a
657publicly-visible branch that other developers pull from, as it will
Junio C Hamano16cf1582007-02-05 07:21:48658force needless merges on other developers to clean up the history.
659If you need to undo changes that you have pushed, use <a href="git-revert.html">git-revert(1)</a>
660instead.</p>
Junio C Hamano6f8a7902006-05-22 01:10:13661<p>The git grep command can search for strings in any version of your
662project, so</p>
663<div class="listingblock">
664<div class="content">
665<pre><tt>$ git grep "hello" v2.5</tt></pre>
666</div></div>
Junio C Hamano341071d2006-06-04 07:24:48667<p>searches for all occurrences of "hello" in v2.5.</p>
Junio C Hamano6f8a7902006-05-22 01:10:13668<p>If you leave out the commit name, git grep will search any of the
669files it manages in your current directory. So</p>
670<div class="listingblock">
671<div class="content">
672<pre><tt>$ git grep "hello"</tt></pre>
673</div></div>
674<p>is a quick way to search just the files that are tracked by git.</p>
675<p>Many git commands also take sets of commits, which can be specified
676in a number of ways. Here are some examples with git log:</p>
677<div class="listingblock">
678<div class="content">
679<pre><tt>$ git log v2.5..v2.6 # commits between v2.5 and v2.6
680$ git log v2.5.. # commits since v2.5
681$ git log --since="2 weeks ago" # commits from the last 2 weeks
682$ git log v2.5.. Makefile # commits since v2.5 which modify
683 # Makefile</tt></pre>
684</div></div>
685<p>You can also give git log a "range" of commits where the first is not
686necessarily an ancestor of the second; for example, if the tips of
687the branches "stable-release" and "master" diverged from a common
688commit some time ago, then</p>
689<div class="listingblock">
690<div class="content">
691<pre><tt>$ git log stable..experimental</tt></pre>
692</div></div>
693<p>will list commits made in the experimental branch but not in the
694stable branch, while</p>
695<div class="listingblock">
696<div class="content">
697<pre><tt>$ git log experimental..stable</tt></pre>
698</div></div>
699<p>will show the list of commits made on the stable branch but not
700the experimental branch.</p>
701<p>The "git log" command has a weakness: it must present commits in a
702list. When the history has lines of development that diverged and
703then merged back together, the order in which "git log" presents
704those commits is meaningless.</p>
705<p>Most projects with multiple contributors (such as the linux kernel,
706or git itself) have frequent merges, and gitk does a better job of
707visualizing their history. For example,</p>
708<div class="listingblock">
709<div class="content">
710<pre><tt>$ gitk --since="2 weeks ago" drivers/</tt></pre>
711</div></div>
712<p>allows you to browse any commits from the last 2 weeks of commits
Junio C Hamanobb8fb052006-05-30 07:21:12713that modified files under the "drivers" directory. (Note: you can
714adjust gitk's fonts by holding down the control key while pressing
715"-" or "+".)</p>
Junio C Hamano6f8a7902006-05-22 01:10:13716<p>Finally, most commands that take filenames will optionally allow you
717to precede any filename by a commit, to specify a particular version
Junio C Hamanobb8fb052006-05-30 07:21:12718of the file:</p>
Junio C Hamano6f8a7902006-05-22 01:10:13719<div class="listingblock">
720<div class="content">
721<pre><tt>$ git diff v2.5:Makefile HEAD:Makefile.in</tt></pre>
722</div></div>
Junio C Hamanoeb692952007-01-03 22:02:12723<p>You can also use "git show" to see any such file:</p>
Junio C Hamanobb8fb052006-05-30 07:21:12724<div class="listingblock">
725<div class="content">
Junio C Hamanoeb692952007-01-03 22:02:12726<pre><tt>$ git show v2.5:Makefile</tt></pre>
Junio C Hamanobb8fb052006-05-30 07:21:12727</div></div>
Junio C Hamano1a4e8412005-12-27 08:17:23728</div>
Junio C Hamanoc2b0a492006-01-23 07:54:36729<h2>Next Steps</h2>
Junio C Hamano1a4e8412005-12-27 08:17:23730<div class="sectionbody">
Junio C Hamano6f8a7902006-05-22 01:10:13731<p>This tutorial should be enough to perform basic distributed revision
732control for your projects. However, to fully understand the depth
733and power of git you need to understand two simple ideas on which it
734is based:</p>
Junio C Hamano1a4e8412005-12-27 08:17:23735<ul>
736<li>
737<p>
Junio C Hamano6f8a7902006-05-22 01:10:13738The object database is the rather elegant system used to
739 store the history of your project&#8212;files, directories, and
740 commits.
Junio C Hamanoc2b0a492006-01-23 07:54:36741</p>
742</li>
743<li>
744<p>
Junio C Hamano6f8a7902006-05-22 01:10:13745The index file is a cache of the state of a directory tree,
746 used to create commits, check out working directories, and
747 hold the various trees involved in a merge.
748</p>
749</li>
750</ul>
751<p><a href="tutorial-2.html">Part two of this tutorial</a> explains the object
752database, the index file, and a few other odds and ends that you'll
753need to make the most of git.</p>
Junio C Hamanoed7f4f62007-05-20 09:09:09754<p>If you don't want to continue with that right away, a few other
Junio C Hamano6f8a7902006-05-22 01:10:13755digressions that may be interesting at this point are:</p>
756<ul>
757<li>
758<p>
Junio C Hamanoc2b0a492006-01-23 07:54:36759<a href="git-format-patch.html">git-format-patch(1)</a>, <a href="git-am.html">git-am(1)</a>: These convert
760 series of git commits into emailed patches, and vice versa,
761 useful for projects such as the linux kernel which rely heavily
762 on emailed patches.
763</p>
764</li>
765<li>
766<p>
767<a href="git-bisect.html">git-bisect(1)</a>: When there is a regression in your
768 project, one way to track down the bug is by searching through
769 the history to find the exact commit that's to blame. Git bisect
770 can help you perform a binary search for that commit. It is
771 smart enough to perform a close-to-optimal search even in the
772 case of complex non-linear history with lots of merged branches.
Junio C Hamano1a4e8412005-12-27 08:17:23773</p>
774</li>
Junio C Hamano6f8a7902006-05-22 01:10:13775<li>
776<p>
Junio C Hamano341071d2006-06-04 07:24:48777<a href="everyday.html">Everyday GIT with 20 Commands Or So</a>
Junio C Hamano6f8a7902006-05-22 01:10:13778</p>
779</li>
780<li>
781<p>
782<a href="cvs-migration.html">git for CVS users</a>.
783</p>
784</li>
Junio C Hamano1a4e8412005-12-27 08:17:23785</ul>
Junio C Hamano1a4e8412005-12-27 08:17:23786</div>
787<div id="footer">
788<div id="footer-text">
Junio C Hamano2a8f6dc2007-07-09 08:48:38789Last updated 09-Jul-2007 08:47:51 UTC
Junio C Hamano1a4e8412005-12-27 08:17:23790</div>
791</div>
792</body>
793</html>