]> git.ipfire.org Git - thirdparty/git.git/blame - Documentation/tutorial.txt
Merge branch 'sp/mmap'
[thirdparty/git.git] / Documentation / tutorial.txt
CommitLineData
927a503c
BF
1A tutorial introduction to git
2==============================
8c7fa247 3
927a503c
BF
4This tutorial explains how to import a new project into git, make
5changes to it, and share changes with other developers.
8c7fa247 6
927a503c
BF
7First, note that you can get documentation for a command such as "git
8diff" with:
8c7fa247 9
927a503c
BF
10------------------------------------------------
11$ man git-diff
12------------------------------------------------
8c7fa247 13
66589230
JH
14It is a good idea to introduce yourself to git before doing any
15operation. The easiest way to do so is:
16
17------------------------------------------------
18$ cat >~/.gitconfig <<\EOF
19[user]
20 name = Your Name Comes Here
21 email = you@yourdomain.example.com
22EOF
23------------------------------------------------
24
25
927a503c
BF
26Importing a new project
27-----------------------
8c7fa247 28
927a503c
BF
29Assume you have a tarball project.tar.gz with your initial work. You
30can place it under git revision control as follows.
8c7fa247 31
8db9307c 32------------------------------------------------
dcc6e28f 33$ tar xzf project.tar.gz
927a503c
BF
34$ cd project
35$ git init-db
8db9307c 36------------------------------------------------
8c7fa247 37
927a503c 38Git will reply
8c7fa247 39
927a503c 40------------------------------------------------
ef0a89a6 41Initialized empty Git repository in .git/
927a503c 42------------------------------------------------
8c7fa247 43
927a503c
BF
44You've now initialized the working directory--you may notice a new
45directory created, named ".git". Tell git that you want it to track
84dee6bb 46every file under the current directory (note the '.') with:
8c7fa247 47
8db9307c 48------------------------------------------------
927a503c 49$ git add .
8db9307c 50------------------------------------------------
8c7fa247 51
927a503c 52Finally,
8c7fa247 53
927a503c 54------------------------------------------------
66589230 55$ git commit
927a503c 56------------------------------------------------
8c7fa247 57
927a503c
BF
58will prompt you for a commit message, then record the current state
59of all the files to the repository.
8c7fa247 60
84dee6bb
BF
61Making changes
62--------------
63
927a503c 64Try modifying some files, then run
8c7fa247 65
8db9307c 66------------------------------------------------
927a503c 67$ git diff
8db9307c 68------------------------------------------------
8c7fa247 69
c1d179f8
JH
70to review your changes. When you're done, tell git that you
71want the updated contents of these files in the commit and then
72make a commit, like this:
8c7fa247 73
927a503c 74------------------------------------------------
84dee6bb 75$ git add file1 file2 file3
c1d179f8 76$ git commit
927a503c 77------------------------------------------------
f2416c27 78
c1d179f8 79This will again prompt your for a message describing the change, and then
84dee6bb
BF
80record the new versions of the files you listed.
81
82Alternatively, instead of running `git add` beforehand, you can use
66589230
JH
83
84------------------------------------------------
85$ git commit -a
86------------------------------------------------
8c7fa247 87
84dee6bb
BF
88which will automatically notice modified (but not new) files.
89
927a503c
BF
90A note on commit messages: Though not required, it's a good idea to
91begin the commit message with a single short (less than 50 character)
92line summarizing the change, followed by a blank line and then a more
93thorough description. Tools that turn commits into email, for
c1d179f8 94example, use the first line on the Subject: line and the rest of the
927a503c 95commit in the body.
8c7fa247 96
8c7fa247 97
366bfcb6
NP
98Git tracks content not files
99----------------------------
100
101With git you have to explicitly "add" all the changed _content_ you
102want to commit together. This can be done in a few different ways:
103
1041) By using 'git add <file_spec>...'
105
106 This can be performed multiple times before a commit. Note that this
107 is not only for adding new files. Even modified files must be
108 added to the set of changes about to be committed. The "git status"
109 command gives you a summary of what is included so far for the
110 next commit. When done you should use the 'git commit' command to
111 make it real.
112
113 Note: don't forget to 'add' a file again if you modified it after the
114 first 'add' and before 'commit'. Otherwise only the previous added
115 state of that file will be committed. This is because git tracks
116 content, so what you're really 'add'ing to the commit is the *content*
117 of the file in the state it is in when you 'add' it.
118
1192) By using 'git commit -a' directly
120
121 This is a quick way to automatically 'add' the content from all files
122 that were modified since the previous commit, and perform the actual
123 commit without having to separately 'add' them beforehand. This will
124 not add content from new files i.e. files that were never added before.
125 Those files still have to be added explicitly before performing a
126 commit.
127
128But here's a twist. If you do 'git commit <file1> <file2> ...' then only
129the changes belonging to those explicitly specified files will be
130committed, entirely bypassing the current "added" changes. Those "added"
131changes will still remain available for a subsequent commit though.
132
133However, for normal usage you only have to remember 'git add' + 'git commit'
134and/or 'git commit -a'.
135
8c7fa247 136
366bfcb6
NP
137Viewing the changelog
138---------------------
8c7fa247 139
927a503c 140At any point you can view the history of your changes using
8c7fa247 141
927a503c 142------------------------------------------------
67e6e5c4 143$ git log
927a503c 144------------------------------------------------
8c7fa247 145
927a503c 146If you also want to see complete diffs at each step, use
8c7fa247 147
927a503c 148------------------------------------------------
67e6e5c4 149$ git log -p
927a503c 150------------------------------------------------
8c7fa247 151
c1d179f8
JH
152Often the overview of the change is useful to get a feel of
153each step
154
155------------------------------------------------
156$ git log --stat --summary
157------------------------------------------------
158
927a503c
BF
159Managing branches
160-----------------
2a29da7c 161
927a503c
BF
162A single git repository can maintain multiple branches of
163development. To create a new branch named "experimental", use
8c7fa247 164
927a503c
BF
165------------------------------------------------
166$ git branch experimental
167------------------------------------------------
8c7fa247 168
927a503c 169If you now run
8c7fa247 170
927a503c
BF
171------------------------------------------------
172$ git branch
173------------------------------------------------
8c7fa247 174
927a503c 175you'll get a list of all existing branches:
8c7fa247 176
8db9307c 177------------------------------------------------
927a503c
BF
178 experimental
179* master
8db9307c 180------------------------------------------------
8c7fa247 181
927a503c
BF
182The "experimental" branch is the one you just created, and the
183"master" branch is a default branch that was created for you
184automatically. The asterisk marks the branch you are currently on;
185type
8c7fa247 186
927a503c
BF
187------------------------------------------------
188$ git checkout experimental
189------------------------------------------------
8c7fa247 190
927a503c
BF
191to switch to the experimental branch. Now edit a file, commit the
192change, and switch back to the master branch:
8c7fa247 193
927a503c
BF
194------------------------------------------------
195(edit file)
196$ git commit -a
197$ git checkout master
198------------------------------------------------
8c7fa247 199
927a503c
BF
200Check that the change you made is no longer visible, since it was
201made on the experimental branch and you're back on the master branch.
8c7fa247 202
927a503c 203You can make a different change on the master branch:
8c7fa247 204
927a503c
BF
205------------------------------------------------
206(edit file)
207$ git commit -a
208------------------------------------------------
8c7fa247 209
927a503c 210at this point the two branches have diverged, with different changes
59427063 211made in each. To merge the changes made in experimental into master, run
ed616049 212
927a503c
BF
213------------------------------------------------
214$ git pull . experimental
215------------------------------------------------
216
217If the changes don't conflict, you're done. If there are conflicts,
218markers will be left in the problematic files showing the conflict;
8c7fa247 219
8db9307c 220------------------------------------------------
927a503c 221$ git diff
8db9307c 222------------------------------------------------
8c7fa247 223
927a503c
BF
224will show this. Once you've edited the files to resolve the
225conflicts,
8c7fa247 226
8db9307c 227------------------------------------------------
927a503c 228$ git commit -a
8db9307c 229------------------------------------------------
8c7fa247 230
927a503c 231will commit the result of the merge. Finally,
8c7fa247 232
8db9307c 233------------------------------------------------
927a503c 234$ gitk
8db9307c 235------------------------------------------------
8c7fa247 236
927a503c 237will show a nice graphical representation of the resulting history.
8c7fa247 238
9c9410e1
SB
239At this point you could delete the experimental branch with
240
241------------------------------------------------
242$ git branch -d experimental
243------------------------------------------------
244
245This command ensures that the changes in the experimental branch are
246already in the current branch.
247
927a503c
BF
248If you develop on a branch crazy-idea, then regret it, you can always
249delete the branch with
8c7fa247 250
927a503c
BF
251-------------------------------------
252$ git branch -D crazy-idea
253-------------------------------------
8c7fa247 254
927a503c
BF
255Branches are cheap and easy, so this is a good way to try something
256out.
8c7fa247 257
927a503c
BF
258Using git for collaboration
259---------------------------
3eb5128a 260
927a503c
BF
261Suppose that Alice has started a new project with a git repository in
262/home/alice/project, and that Bob, who has a home directory on the
263same machine, wants to contribute.
3eb5128a 264
927a503c 265Bob begins with:
3eb5128a 266
8db9307c 267------------------------------------------------
927a503c 268$ git clone /home/alice/project myrepo
8db9307c 269------------------------------------------------
3eb5128a 270
927a503c
BF
271This creates a new directory "myrepo" containing a clone of Alice's
272repository. The clone is on an equal footing with the original
abda1ef5 273project, possessing its own copy of the original project's history.
927a503c
BF
274
275Bob then makes some changes and commits them:
ed616049 276
927a503c
BF
277------------------------------------------------
278(edit files)
279$ git commit -a
280(repeat as necessary)
281------------------------------------------------
ed616049 282
927a503c
BF
283When he's ready, he tells Alice to pull changes from the repository
284at /home/bob/myrepo. She does this with:
ed616049 285
927a503c
BF
286------------------------------------------------
287$ cd /home/alice/project
93ee7823 288$ git pull /home/bob/myrepo master
927a503c 289------------------------------------------------
ed616049 290
93ee7823
BF
291This merges the changes from Bob's "master" branch into Alice's
292current branch. If Alice has made her own changes in the meantime,
293then she may need to manually fix any conflicts. (Note that the
294"master" argument in the above command is actually unnecessary, as it
295is the default.)
2ae6c706 296
93ee7823
BF
297The "pull" command thus performs two operations: it fetches changes
298from a remote branch, then merges them into the current branch.
2ae6c706 299
93ee7823
BF
300You can perform the first operation alone using the "git fetch"
301command. For example, Alice could create a temporary branch just to
302track Bob's changes, without merging them with her own, using:
2a29da7c 303
927a503c
BF
304-------------------------------------
305$ git fetch /home/bob/myrepo master:bob-incoming
306-------------------------------------
2a29da7c 307
927a503c 308which fetches the changes from Bob's master branch into a new branch
93ee7823 309named bob-incoming. Then
a7333f9e 310
927a503c 311-------------------------------------
67e6e5c4 312$ git log -p master..bob-incoming
927a503c 313-------------------------------------
a7333f9e 314
927a503c
BF
315shows a list of all the changes that Bob made since he branched from
316Alice's master branch.
a7333f9e 317
93ee7823
BF
318After examining those changes, and possibly fixing things, Alice
319could pull the changes into her master branch:
ed616049 320
927a503c
BF
321-------------------------------------
322$ git checkout master
323$ git pull . bob-incoming
324-------------------------------------
ed616049 325
927a503c
BF
326The last command is a pull from the "bob-incoming" branch in Alice's
327own repository.
ed616049 328
93ee7823
BF
329Alice could also perform both steps at once with:
330
331-------------------------------------
332$ git pull /home/bob/myrepo master:bob-incoming
333-------------------------------------
334
335This is just like the "git pull /home/bob/myrepo master" that we saw
336before, except that it also stores the unmerged changes from bob's
337master branch in bob-incoming before merging them into Alice's
338current branch. Note that git pull always merges into the current
339branch, regardless of what else is given on the commandline.
340
927a503c 341Later, Bob can update his repo with Alice's latest changes using
ed616049 342
927a503c
BF
343-------------------------------------
344$ git pull
345-------------------------------------
ed616049 346
927a503c
BF
347Note that he doesn't need to give the path to Alice's repository;
348when Bob cloned Alice's repository, git stored the location of her
d66409f0
BF
349repository in the repository configuration, and that location is
350used for pulls:
2a29da7c 351
927a503c 352-------------------------------------
d66409f0
BF
353$ git repo-config --get remote.origin.url
354/home/bob/myrepo
927a503c 355-------------------------------------
2a29da7c 356
d66409f0
BF
357(The complete configuration created by git-clone is visible using
358"git repo-config -l", and the gitlink:git-repo-config[1] man page
359explains the meaning of each option.)
360
361Git also keeps a pristine copy of Alice's master branch under the
362name "origin/master":
363
364-------------------------------------
365$ git branch -r
366 origin/master
367-------------------------------------
2a29da7c 368
927a503c
BF
369If Bob later decides to work from a different host, he can still
370perform clones and pulls using the ssh protocol:
2a29da7c 371
927a503c
BF
372-------------------------------------
373$ git clone alice.org:/home/alice/project myrepo
374-------------------------------------
2a29da7c 375
927a503c
BF
376Alternatively, git has a native protocol, or can use rsync or http;
377see gitlink:git-pull[1] for details.
0c04094b 378
927a503c
BF
379Git can also be used in a CVS-like mode, with a central repository
380that various users push changes to; see gitlink:git-push[1] and
381link:cvs-migration.html[git for CVS users].
0c04094b 382
f1fe3846
BF
383Exploring history
384-----------------
0c04094b 385
f1fe3846
BF
386Git history is represented as a series of interrelated commits. We
387have already seen that the git log command can list those commits.
388Note that first line of each git log entry also gives a name for the
389commit:
c9517341 390
927a503c 391-------------------------------------
f1fe3846
BF
392$ git log
393commit c82a22c39cbc32576f64f5c6b3f24b99ea8149c7
394Author: Junio C Hamano <junkio@cox.net>
395Date: Tue May 16 17:18:22 2006 -0700
396
397 merge-base: Clarify the comments on post processing.
927a503c 398-------------------------------------
0c04094b 399
f1fe3846
BF
400We can give this name to git show to see the details about this
401commit.
0c04094b 402
927a503c 403-------------------------------------
f1fe3846 404$ git show c82a22c39cbc32576f64f5c6b3f24b99ea8149c7
927a503c 405-------------------------------------
0c04094b 406
c1d179f8 407But there are other ways to refer to commits. You can use any initial
f1fe3846 408part of the name that is long enough to uniquely identify the commit:
0c04094b 409
f1fe3846
BF
410-------------------------------------
411$ git show c82a22c39c # the first few characters of the name are
412 # usually enough
413$ git show HEAD # the tip of the current branch
414$ git show experimental # the tip of the "experimental" branch
415-------------------------------------
416
9c9410e1
SB
417Every commit usually has one "parent" commit
418which points to the previous state of the project:
0c04094b 419
927a503c 420-------------------------------------
f1fe3846
BF
421$ git show HEAD^ # to see the parent of HEAD
422$ git show HEAD^^ # to see the grandparent of HEAD
423$ git show HEAD~4 # to see the great-great grandparent of HEAD
927a503c 424-------------------------------------
e7c1ca42 425
f1fe3846
BF
426Note that merge commits may have more than one parent:
427
428-------------------------------------
429$ git show HEAD^1 # show the first parent of HEAD (same as HEAD^)
430$ git show HEAD^2 # show the second parent of HEAD
431-------------------------------------
44760f1d 432
f1fe3846 433You can also give commits names of your own; after running
3eb5128a 434
927a503c 435-------------------------------------
f1fe3846 436$ git-tag v2.5 1b2e1d63ff
927a503c 437-------------------------------------
3eb5128a 438
f1fe3846
BF
439you can refer to 1b2e1d63ff by the name "v2.5". If you intend to
440share this name with other people (for example, to identify a release
441version), you should create a "tag" object, and perhaps sign it; see
442gitlink:git-tag[1] for details.
443
444Any git command that needs to know a commit can take any of these
445names. For example:
3eb5128a 446
927a503c 447-------------------------------------
f1fe3846
BF
448$ git diff v2.5 HEAD # compare the current HEAD to v2.5
449$ git branch stable v2.5 # start a new branch named "stable" based
450 # at v2.5
451$ git reset --hard HEAD^ # reset your current branch and working
37425065 452 # directory to its state at HEAD^
927a503c 453-------------------------------------
c9517341 454
f1fe3846
BF
455Be careful with that last command: in addition to losing any changes
456in the working directory, it will also remove all later commits from
457this branch. If this branch is the only branch containing those
458commits, they will be lost. (Also, don't use "git reset" on a
459publicly-visible branch that other developers pull from, as git will
460be confused by history that disappears in this way.)
c9517341 461
f1fe3846
BF
462The git grep command can search for strings in any version of your
463project, so
c9517341 464
927a503c 465-------------------------------------
f1fe3846 466$ git grep "hello" v2.5
927a503c 467-------------------------------------
c9517341 468
abda1ef5 469searches for all occurrences of "hello" in v2.5.
2a29da7c 470
f1fe3846
BF
471If you leave out the commit name, git grep will search any of the
472files it manages in your current directory. So
2a29da7c 473
927a503c 474-------------------------------------
f1fe3846 475$ git grep "hello"
dc5f9239
JH
476-------------------------------------
477
f1fe3846 478is a quick way to search just the files that are tracked by git.
927a503c 479
f1fe3846
BF
480Many git commands also take sets of commits, which can be specified
481in a number of ways. Here are some examples with git log:
927a503c
BF
482
483-------------------------------------
f1fe3846
BF
484$ git log v2.5..v2.6 # commits between v2.5 and v2.6
485$ git log v2.5.. # commits since v2.5
486$ git log --since="2 weeks ago" # commits from the last 2 weeks
487$ git log v2.5.. Makefile # commits since v2.5 which modify
488 # Makefile
927a503c 489-------------------------------------
6f60300b 490
f1fe3846
BF
491You can also give git log a "range" of commits where the first is not
492necessarily an ancestor of the second; for example, if the tips of
493the branches "stable-release" and "master" diverged from a common
494commit some time ago, then
495
496-------------------------------------
497$ git log stable..experimental
498-------------------------------------
499
500will list commits made in the experimental branch but not in the
501stable branch, while
502
503-------------------------------------
504$ git log experimental..stable
505-------------------------------------
506
507will show the list of commits made on the stable branch but not
508the experimental branch.
509
510The "git log" command has a weakness: it must present commits in a
511list. When the history has lines of development that diverged and
512then merged back together, the order in which "git log" presents
513those commits is meaningless.
514
515Most projects with multiple contributors (such as the linux kernel,
516or git itself) have frequent merges, and gitk does a better job of
517visualizing their history. For example,
518
519-------------------------------------
520$ gitk --since="2 weeks ago" drivers/
521-------------------------------------
522
523allows you to browse any commits from the last 2 weeks of commits
2be1bc48
BF
524that modified files under the "drivers" directory. (Note: you can
525adjust gitk's fonts by holding down the control key while pressing
526"-" or "+".)
f1fe3846
BF
527
528Finally, most commands that take filenames will optionally allow you
529to precede any filename by a commit, to specify a particular version
38573864 530of the file:
f1fe3846
BF
531
532-------------------------------------
533$ git diff v2.5:Makefile HEAD:Makefile.in
534-------------------------------------
927a503c 535
9c9410e1 536You can also use "git show" to see any such file:
38573864
BF
537
538-------------------------------------
9c9410e1 539$ git show v2.5:Makefile
38573864
BF
540-------------------------------------
541
927a503c
BF
542Next Steps
543----------
544
e31952da
BF
545This tutorial should be enough to perform basic distributed revision
546control for your projects. However, to fully understand the depth
547and power of git you need to understand two simple ideas on which it
548is based:
549
550 * The object database is the rather elegant system used to
551 store the history of your project--files, directories, and
552 commits.
553
554 * The index file is a cache of the state of a directory tree,
555 used to create commits, check out working directories, and
556 hold the various trees involved in a merge.
557
558link:tutorial-2.html[Part two of this tutorial] explains the object
559database, the index file, and a few other odds and ends that you'll
560need to make the most of git.
561
562If you don't want to consider with that right away, a few other
563digressions that may be interesting at this point are:
927a503c 564
927a503c
BF
565 * gitlink:git-format-patch[1], gitlink:git-am[1]: These convert
566 series of git commits into emailed patches, and vice versa,
567 useful for projects such as the linux kernel which rely heavily
568 on emailed patches.
569
570 * gitlink:git-bisect[1]: When there is a regression in your
571 project, one way to track down the bug is by searching through
572 the history to find the exact commit that's to blame. Git bisect
573 can help you perform a binary search for that commit. It is
574 smart enough to perform a close-to-optimal search even in the
575 case of complex non-linear history with lots of merged branches.
576
abda1ef5 577 * link:everyday.html[Everyday GIT with 20 Commands Or So]
e31952da
BF
578
579 * link:cvs-migration.html[git for CVS users].