set_git_dir: handle feeding gitdir to itself
[git.git] / Documentation / gittutorial.txt
blob794b83393ea21a8d569c34791ee4af1dfb99a57a
1 gittutorial(7)
2 ==============
4 NAME
5 ----
6 gittutorial - A tutorial introduction to Git
8 SYNOPSIS
9 --------
10 [verse]
11 git *
13 DESCRIPTION
14 -----------
16 This tutorial explains how to import a new project into Git, make
17 changes to it, and share changes with other developers.
19 If you are instead primarily interested in using Git to fetch a project,
20 for example, to test the latest version, you may prefer to start with
21 the first two chapters of link:user-manual.html[The Git User's Manual].
23 First, note that you can get documentation for a command such as
24 `git log --graph` with:
26 ------------------------------------------------
27 $ man git-log
28 ------------------------------------------------
30 or:
32 ------------------------------------------------
33 $ git help log
34 ------------------------------------------------
36 With the latter, you can use the manual viewer of your choice; see
37 linkgit:git-help[1] for more information.
39 It is a good idea to introduce yourself to Git with your name and
40 public email address before doing any operation.  The easiest
41 way to do so is:
43 ------------------------------------------------
44 $ git config --global user.name "Your Name Comes Here"
45 $ git config --global user.email you@yourdomain.example.com
46 ------------------------------------------------
49 Importing a new project
50 -----------------------
52 Assume you have a tarball project.tar.gz with your initial work.  You
53 can place it under Git revision control as follows.
55 ------------------------------------------------
56 $ tar xzf project.tar.gz
57 $ cd project
58 $ git init
59 ------------------------------------------------
61 Git will reply
63 ------------------------------------------------
64 Initialized empty Git repository in .git/
65 ------------------------------------------------
67 You've now initialized the working directory--you may notice a new
68 directory created, named ".git".
70 Next, tell Git to take a snapshot of the contents of all files under the
71 current directory (note the '.'), with 'git add':
73 ------------------------------------------------
74 $ git add .
75 ------------------------------------------------
77 This snapshot is now stored in a temporary staging area which Git calls
78 the "index".  You can permanently store the contents of the index in the
79 repository with 'git commit':
81 ------------------------------------------------
82 $ git commit
83 ------------------------------------------------
85 This will prompt you for a commit message.  You've now stored the first
86 version of your project in Git.
88 Making changes
89 --------------
91 Modify some files, then add their updated contents to the index:
93 ------------------------------------------------
94 $ git add file1 file2 file3
95 ------------------------------------------------
97 You are now ready to commit.  You can see what is about to be committed
98 using 'git diff' with the --cached option:
100 ------------------------------------------------
101 $ git diff --cached
102 ------------------------------------------------
104 (Without --cached, 'git diff' will show you any changes that
105 you've made but not yet added to the index.)  You can also get a brief
106 summary of the situation with 'git status':
108 ------------------------------------------------
109 $ git status
110 On branch master
111 Changes to be committed:
112 Your branch is up-to-date with 'origin/master'.
113   (use "git reset HEAD <file>..." to unstage)
115         modified:   file1
116         modified:   file2
117         modified:   file3
119 ------------------------------------------------
121 If you need to make any further adjustments, do so now, and then add any
122 newly modified content to the index.  Finally, commit your changes with:
124 ------------------------------------------------
125 $ git commit
126 ------------------------------------------------
128 This will again prompt you for a message describing the change, and then
129 record a new version of the project.
131 Alternatively, instead of running 'git add' beforehand, you can use
133 ------------------------------------------------
134 $ git commit -a
135 ------------------------------------------------
137 which will automatically notice any modified (but not new) files, add
138 them to the index, and commit, all in one step.
140 A note on commit messages: Though not required, it's a good idea to
141 begin the commit message with a single short (less than 50 character)
142 line summarizing the change, followed by a blank line and then a more
143 thorough description. The text up to the first blank line in a commit
144 message is treated as the commit title, and that title is used
145 throughout Git.  For example, linkgit:git-format-patch[1] turns a
146 commit into email, and it uses the title on the Subject line and the
147 rest of the commit in the body.
149 Git tracks content not files
150 ----------------------------
152 Many revision control systems provide an `add` command that tells the
153 system to start tracking changes to a new file.  Git's `add` command
154 does something simpler and more powerful: 'git add' is used both for new
155 and newly modified files, and in both cases it takes a snapshot of the
156 given files and stages that content in the index, ready for inclusion in
157 the next commit.
159 Viewing project history
160 -----------------------
162 At any point you can view the history of your changes using
164 ------------------------------------------------
165 $ git log
166 ------------------------------------------------
168 If you also want to see complete diffs at each step, use
170 ------------------------------------------------
171 $ git log -p
172 ------------------------------------------------
174 Often the overview of the change is useful to get a feel of
175 each step
177 ------------------------------------------------
178 $ git log --stat --summary
179 ------------------------------------------------
181 Managing branches
182 -----------------
184 A single Git repository can maintain multiple branches of
185 development.  To create a new branch named "experimental", use
187 ------------------------------------------------
188 $ git branch experimental
189 ------------------------------------------------
191 If you now run
193 ------------------------------------------------
194 $ git branch
195 ------------------------------------------------
197 you'll get a list of all existing branches:
199 ------------------------------------------------
200   experimental
201 * master
202 ------------------------------------------------
204 The "experimental" branch is the one you just created, and the
205 "master" branch is a default branch that was created for you
206 automatically.  The asterisk marks the branch you are currently on;
207 type
209 ------------------------------------------------
210 $ git checkout experimental
211 ------------------------------------------------
213 to switch to the experimental branch.  Now edit a file, commit the
214 change, and switch back to the master branch:
216 ------------------------------------------------
217 (edit file)
218 $ git commit -a
219 $ git checkout master
220 ------------------------------------------------
222 Check that the change you made is no longer visible, since it was
223 made on the experimental branch and you're back on the master branch.
225 You can make a different change on the master branch:
227 ------------------------------------------------
228 (edit file)
229 $ git commit -a
230 ------------------------------------------------
232 at this point the two branches have diverged, with different changes
233 made in each.  To merge the changes made in experimental into master, run
235 ------------------------------------------------
236 $ git merge experimental
237 ------------------------------------------------
239 If the changes don't conflict, you're done.  If there are conflicts,
240 markers will be left in the problematic files showing the conflict;
242 ------------------------------------------------
243 $ git diff
244 ------------------------------------------------
246 will show this.  Once you've edited the files to resolve the
247 conflicts,
249 ------------------------------------------------
250 $ git commit -a
251 ------------------------------------------------
253 will commit the result of the merge. Finally,
255 ------------------------------------------------
256 $ gitk
257 ------------------------------------------------
259 will show a nice graphical representation of the resulting history.
261 At this point you could delete the experimental branch with
263 ------------------------------------------------
264 $ git branch -d experimental
265 ------------------------------------------------
267 This command ensures that the changes in the experimental branch are
268 already in the current branch.
270 If you develop on a branch crazy-idea, then regret it, you can always
271 delete the branch with
273 -------------------------------------
274 $ git branch -D crazy-idea
275 -------------------------------------
277 Branches are cheap and easy, so this is a good way to try something
278 out.
280 Using Git for collaboration
281 ---------------------------
283 Suppose that Alice has started a new project with a Git repository in
284 /home/alice/project, and that Bob, who has a home directory on the
285 same machine, wants to contribute.
287 Bob begins with:
289 ------------------------------------------------
290 bob$ git clone /home/alice/project myrepo
291 ------------------------------------------------
293 This creates a new directory "myrepo" containing a clone of Alice's
294 repository.  The clone is on an equal footing with the original
295 project, possessing its own copy of the original project's history.
297 Bob then makes some changes and commits them:
299 ------------------------------------------------
300 (edit files)
301 bob$ git commit -a
302 (repeat as necessary)
303 ------------------------------------------------
305 When he's ready, he tells Alice to pull changes from the repository
306 at /home/bob/myrepo.  She does this with:
308 ------------------------------------------------
309 alice$ cd /home/alice/project
310 alice$ git pull /home/bob/myrepo master
311 ------------------------------------------------
313 This merges the changes from Bob's "master" branch into Alice's
314 current branch.  If Alice has made her own changes in the meantime,
315 then she may need to manually fix any conflicts.
317 The "pull" command thus performs two operations: it fetches changes
318 from a remote branch, then merges them into the current branch.
320 Note that in general, Alice would want her local changes committed before
321 initiating this "pull".  If Bob's work conflicts with what Alice did since
322 their histories forked, Alice will use her working tree and the index to
323 resolve conflicts, and existing local changes will interfere with the
324 conflict resolution process (Git will still perform the fetch but will
325 refuse to merge --- Alice will have to get rid of her local changes in
326 some way and pull again when this happens).
328 Alice can peek at what Bob did without merging first, using the "fetch"
329 command; this allows Alice to inspect what Bob did, using a special
330 symbol "FETCH_HEAD", in order to determine if he has anything worth
331 pulling, like this:
333 ------------------------------------------------
334 alice$ git fetch /home/bob/myrepo master
335 alice$ git log -p HEAD..FETCH_HEAD
336 ------------------------------------------------
338 This operation is safe even if Alice has uncommitted local changes.
339 The range notation "HEAD..FETCH_HEAD" means "show everything that is reachable
340 from the FETCH_HEAD but exclude anything that is reachable from HEAD".
341 Alice already knows everything that leads to her current state (HEAD),
342 and reviews what Bob has in his state (FETCH_HEAD) that she has not
343 seen with this command.
345 If Alice wants to visualize what Bob did since their histories forked
346 she can issue the following command:
348 ------------------------------------------------
349 $ gitk HEAD..FETCH_HEAD
350 ------------------------------------------------
352 This uses the same two-dot range notation we saw earlier with 'git log'.
354 Alice may want to view what both of them did since they forked.
355 She can use three-dot form instead of the two-dot form:
357 ------------------------------------------------
358 $ gitk HEAD...FETCH_HEAD
359 ------------------------------------------------
361 This means "show everything that is reachable from either one, but
362 exclude anything that is reachable from both of them".
364 Please note that these range notation can be used with both gitk
365 and "git log".
367 After inspecting what Bob did, if there is nothing urgent, Alice may
368 decide to continue working without pulling from Bob.  If Bob's history
369 does have something Alice would immediately need, Alice may choose to
370 stash her work-in-progress first, do a "pull", and then finally unstash
371 her work-in-progress on top of the resulting history.
373 When you are working in a small closely knit group, it is not
374 unusual to interact with the same repository over and over
375 again.  By defining 'remote' repository shorthand, you can make
376 it easier:
378 ------------------------------------------------
379 alice$ git remote add bob /home/bob/myrepo
380 ------------------------------------------------
382 With this, Alice can perform the first part of the "pull" operation
383 alone using the 'git fetch' command without merging them with her own
384 branch, using:
386 -------------------------------------
387 alice$ git fetch bob
388 -------------------------------------
390 Unlike the longhand form, when Alice fetches from Bob using a
391 remote repository shorthand set up with 'git remote', what was
392 fetched is stored in a remote-tracking branch, in this case
393 `bob/master`.  So after this:
395 -------------------------------------
396 alice$ git log -p master..bob/master
397 -------------------------------------
399 shows a list of all the changes that Bob made since he branched from
400 Alice's master branch.
402 After examining those changes, Alice
403 could merge the changes into her master branch:
405 -------------------------------------
406 alice$ git merge bob/master
407 -------------------------------------
409 This `merge` can also be done by 'pulling from her own remote-tracking
410 branch', like this:
412 -------------------------------------
413 alice$ git pull . remotes/bob/master
414 -------------------------------------
416 Note that git pull always merges into the current branch,
417 regardless of what else is given on the command line.
419 Later, Bob can update his repo with Alice's latest changes using
421 -------------------------------------
422 bob$ git pull
423 -------------------------------------
425 Note that he doesn't need to give the path to Alice's repository;
426 when Bob cloned Alice's repository, Git stored the location of her
427 repository in the repository configuration, and that location is
428 used for pulls:
430 -------------------------------------
431 bob$ git config --get remote.origin.url
432 /home/alice/project
433 -------------------------------------
435 (The complete configuration created by 'git clone' is visible using
436 `git config -l`, and the linkgit:git-config[1] man page
437 explains the meaning of each option.)
439 Git also keeps a pristine copy of Alice's master branch under the
440 name "origin/master":
442 -------------------------------------
443 bob$ git branch -r
444   origin/master
445 -------------------------------------
447 If Bob later decides to work from a different host, he can still
448 perform clones and pulls using the ssh protocol:
450 -------------------------------------
451 bob$ git clone alice.org:/home/alice/project myrepo
452 -------------------------------------
454 Alternatively, Git has a native protocol, or can use http;
455 see linkgit:git-pull[1] for details.
457 Git can also be used in a CVS-like mode, with a central repository
458 that various users push changes to; see linkgit:git-push[1] and
459 linkgit:gitcvs-migration[7].
461 Exploring history
462 -----------------
464 Git history is represented as a series of interrelated commits.  We
465 have already seen that the 'git log' command can list those commits.
466 Note that first line of each git log entry also gives a name for the
467 commit:
469 -------------------------------------
470 $ git log
471 commit c82a22c39cbc32576f64f5c6b3f24b99ea8149c7
472 Author: Junio C Hamano <junkio@cox.net>
473 Date:   Tue May 16 17:18:22 2006 -0700
475     merge-base: Clarify the comments on post processing.
476 -------------------------------------
478 We can give this name to 'git show' to see the details about this
479 commit.
481 -------------------------------------
482 $ git show c82a22c39cbc32576f64f5c6b3f24b99ea8149c7
483 -------------------------------------
485 But there are other ways to refer to commits.  You can use any initial
486 part of the name that is long enough to uniquely identify the commit:
488 -------------------------------------
489 $ git show c82a22c39c   # the first few characters of the name are
490                         # usually enough
491 $ git show HEAD         # the tip of the current branch
492 $ git show experimental # the tip of the "experimental" branch
493 -------------------------------------
495 Every commit usually has one "parent" commit
496 which points to the previous state of the project:
498 -------------------------------------
499 $ git show HEAD^  # to see the parent of HEAD
500 $ git show HEAD^^ # to see the grandparent of HEAD
501 $ git show HEAD~4 # to see the great-great grandparent of HEAD
502 -------------------------------------
504 Note that merge commits may have more than one parent:
506 -------------------------------------
507 $ git show HEAD^1 # show the first parent of HEAD (same as HEAD^)
508 $ git show HEAD^2 # show the second parent of HEAD
509 -------------------------------------
511 You can also give commits names of your own; after running
513 -------------------------------------
514 $ git tag v2.5 1b2e1d63ff
515 -------------------------------------
517 you can refer to 1b2e1d63ff by the name "v2.5".  If you intend to
518 share this name with other people (for example, to identify a release
519 version), you should create a "tag" object, and perhaps sign it; see
520 linkgit:git-tag[1] for details.
522 Any Git command that needs to know a commit can take any of these
523 names.  For example:
525 -------------------------------------
526 $ git diff v2.5 HEAD     # compare the current HEAD to v2.5
527 $ git branch stable v2.5 # start a new branch named "stable" based
528                          # at v2.5
529 $ git reset --hard HEAD^ # reset your current branch and working
530                          # directory to its state at HEAD^
531 -------------------------------------
533 Be careful with that last command: in addition to losing any changes
534 in the working directory, it will also remove all later commits from
535 this branch.  If this branch is the only branch containing those
536 commits, they will be lost.  Also, don't use 'git reset' on a
537 publicly-visible branch that other developers pull from, as it will
538 force needless merges on other developers to clean up the history.
539 If you need to undo changes that you have pushed, use 'git revert'
540 instead.
542 The 'git grep' command can search for strings in any version of your
543 project, so
545 -------------------------------------
546 $ git grep "hello" v2.5
547 -------------------------------------
549 searches for all occurrences of "hello" in v2.5.
551 If you leave out the commit name, 'git grep' will search any of the
552 files it manages in your current directory.  So
554 -------------------------------------
555 $ git grep "hello"
556 -------------------------------------
558 is a quick way to search just the files that are tracked by Git.
560 Many Git commands also take sets of commits, which can be specified
561 in a number of ways.  Here are some examples with 'git log':
563 -------------------------------------
564 $ git log v2.5..v2.6            # commits between v2.5 and v2.6
565 $ git log v2.5..                # commits since v2.5
566 $ git log --since="2 weeks ago" # commits from the last 2 weeks
567 $ git log v2.5.. Makefile       # commits since v2.5 which modify
568                                 # Makefile
569 -------------------------------------
571 You can also give 'git log' a "range" of commits where the first is not
572 necessarily an ancestor of the second; for example, if the tips of
573 the branches "stable" and "master" diverged from a common
574 commit some time ago, then
576 -------------------------------------
577 $ git log stable..master
578 -------------------------------------
580 will list commits made in the master branch but not in the
581 stable branch, while
583 -------------------------------------
584 $ git log master..stable
585 -------------------------------------
587 will show the list of commits made on the stable branch but not
588 the master branch.
590 The 'git log' command has a weakness: it must present commits in a
591 list.  When the history has lines of development that diverged and
592 then merged back together, the order in which 'git log' presents
593 those commits is meaningless.
595 Most projects with multiple contributors (such as the Linux kernel,
596 or Git itself) have frequent merges, and 'gitk' does a better job of
597 visualizing their history.  For example,
599 -------------------------------------
600 $ gitk --since="2 weeks ago" drivers/
601 -------------------------------------
603 allows you to browse any commits from the last 2 weeks of commits
604 that modified files under the "drivers" directory.  (Note: you can
605 adjust gitk's fonts by holding down the control key while pressing
606 "-" or "+".)
608 Finally, most commands that take filenames will optionally allow you
609 to precede any filename by a commit, to specify a particular version
610 of the file:
612 -------------------------------------
613 $ git diff v2.5:Makefile HEAD:Makefile.in
614 -------------------------------------
616 You can also use 'git show' to see any such file:
618 -------------------------------------
619 $ git show v2.5:Makefile
620 -------------------------------------
622 Next Steps
623 ----------
625 This tutorial should be enough to perform basic distributed revision
626 control for your projects.  However, to fully understand the depth
627 and power of Git you need to understand two simple ideas on which it
628 is based:
630   * The object database is the rather elegant system used to
631     store the history of your project--files, directories, and
632     commits.
634   * The index file is a cache of the state of a directory tree,
635     used to create commits, check out working directories, and
636     hold the various trees involved in a merge.
638 Part two of this tutorial explains the object
639 database, the index file, and a few other odds and ends that you'll
640 need to make the most of Git. You can find it at linkgit:gittutorial-2[7].
642 If you don't want to continue with that right away, a few other
643 digressions that may be interesting at this point are:
645   * linkgit:git-format-patch[1], linkgit:git-am[1]: These convert
646     series of git commits into emailed patches, and vice versa,
647     useful for projects such as the Linux kernel which rely heavily
648     on emailed patches.
650   * linkgit:git-bisect[1]: When there is a regression in your
651     project, one way to track down the bug is by searching through
652     the history to find the exact commit that's to blame.  Git bisect
653     can help you perform a binary search for that commit.  It is
654     smart enough to perform a close-to-optimal search even in the
655     case of complex non-linear history with lots of merged branches.
657   * linkgit:gitworkflows[7]: Gives an overview of recommended
658     workflows.
660   * linkgit:giteveryday[7]: Everyday Git with 20 Commands Or So.
662   * linkgit:gitcvs-migration[7]: Git for CVS users.
664 SEE ALSO
665 --------
666 linkgit:gittutorial-2[7],
667 linkgit:gitcvs-migration[7],
668 linkgit:gitcore-tutorial[7],
669 linkgit:gitglossary[7],
670 linkgit:git-help[1],
671 linkgit:gitworkflows[7],
672 linkgit:giteveryday[7],
673 link:user-manual.html[The Git User's Manual]
677 Part of the linkgit:git[1] suite