Merge branch 'ab/config-based-hooks-base' into seen
[git] / Documentation / gittutorial.txt
1 gittutorial(7)
2 ==============
3
4 NAME
5 ----
6 gittutorial - A tutorial introduction to Git
7
8 SYNOPSIS
9 --------
10 [verse]
11 git *
12
13 DESCRIPTION
14 -----------
15
16 This tutorial explains how to import a new project into Git, make
17 changes to it, and share changes with other developers.
18
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].
22
23 First, note that you can get documentation for a command such as
24 `git log --graph` with:
25
26 ------------------------------------------------
27 $ man git-log
28 ------------------------------------------------
29
30 or:
31
32 ------------------------------------------------
33 $ git help log
34 ------------------------------------------------
35
36 With the latter, you can use the manual viewer of your choice; see
37 linkgit:git-help[1] for more information.
38
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:
42
43 ------------------------------------------------
44 $ git config --global user.name "Your Name Comes Here"
45 $ git config --global user.email you@yourdomain.example.com
46 ------------------------------------------------
47
48
49 Importing a new project
50 -----------------------
51
52 Assume you have a tarball project.tar.gz with your initial work.  You
53 can place it under Git revision control as follows.
54
55 ------------------------------------------------
56 $ tar xzf project.tar.gz
57 $ cd project
58 $ git init
59 ------------------------------------------------
60
61 Git will reply
62
63 ------------------------------------------------
64 Initialized empty Git repository in .git/
65 ------------------------------------------------
66
67 You've now initialized the working directory--you may notice a new
68 directory created, named ".git".
69
70 Next, tell Git to take a snapshot of the contents of all files under the
71 current directory (note the '.'), with 'git add':
72
73 ------------------------------------------------
74 $ git add .
75 ------------------------------------------------
76
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':
80
81 ------------------------------------------------
82 $ git commit
83 ------------------------------------------------
84
85 This will prompt you for a commit message.  You've now stored the first
86 version of your project in Git.
87
88 Making changes
89 --------------
90
91 Modify some files, then add their updated contents to the index:
92
93 ------------------------------------------------
94 $ git add file1 file2 file3
95 ------------------------------------------------
96
97 You are now ready to commit.  You can see what is about to be committed
98 using 'git diff' with the --cached option:
99
100 ------------------------------------------------
101 $ git diff --cached
102 ------------------------------------------------
103
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':
107
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 restore --staged <file>..." to unstage)
114
115         modified:   file1
116         modified:   file2
117         modified:   file3
118
119 ------------------------------------------------
120
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:
123
124 ------------------------------------------------
125 $ git commit
126 ------------------------------------------------
127
128 This will again prompt you for a message describing the change, and then
129 record a new version of the project.
130
131 Alternatively, instead of running 'git add' beforehand, you can use
132
133 ------------------------------------------------
134 $ git commit -a
135 ------------------------------------------------
136
137 which will automatically notice any modified (but not new) files, add
138 them to the index, and commit, all in one step.
139
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.
148
149 Git tracks content not files
150 ----------------------------
151
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.
158
159 Viewing project history
160 -----------------------
161
162 At any point you can view the history of your changes using
163
164 ------------------------------------------------
165 $ git log
166 ------------------------------------------------
167
168 If you also want to see complete diffs at each step, use
169
170 ------------------------------------------------
171 $ git log -p
172 ------------------------------------------------
173
174 Often the overview of the change is useful to get a feel of
175 each step
176
177 ------------------------------------------------
178 $ git log --stat --summary
179 ------------------------------------------------
180
181 Managing branches
182 -----------------
183
184 A single Git repository can maintain multiple branches of
185 development.  To create a new branch named "experimental", use
186
187 ------------------------------------------------
188 $ git branch experimental
189 ------------------------------------------------
190
191 If you now run
192
193 ------------------------------------------------
194 $ git branch
195 ------------------------------------------------
196
197 you'll get a list of all existing branches:
198
199 ------------------------------------------------
200   experimental
201 * master
202 ------------------------------------------------
203
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
208
209 ------------------------------------------------
210 $ git switch experimental
211 ------------------------------------------------
212
213 to switch to the experimental branch.  Now edit a file, commit the
214 change, and switch back to the master branch:
215
216 ------------------------------------------------
217 (edit file)
218 $ git commit -a
219 $ git switch master
220 ------------------------------------------------
221
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.
224
225 You can make a different change on the master branch:
226
227 ------------------------------------------------
228 (edit file)
229 $ git commit -a
230 ------------------------------------------------
231
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
234
235 ------------------------------------------------
236 $ git merge experimental
237 ------------------------------------------------
238
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;
241
242 ------------------------------------------------
243 $ git diff
244 ------------------------------------------------
245
246 will show this.  Once you've edited the files to resolve the
247 conflicts,
248
249 ------------------------------------------------
250 $ git commit -a
251 ------------------------------------------------
252
253 will commit the result of the merge. Finally,
254
255 ------------------------------------------------
256 $ gitk
257 ------------------------------------------------
258
259 will show a nice graphical representation of the resulting history.
260
261 At this point you could delete the experimental branch with
262
263 ------------------------------------------------
264 $ git branch -d experimental
265 ------------------------------------------------
266
267 This command ensures that the changes in the experimental branch are
268 already in the current branch.
269
270 If you develop on a branch crazy-idea, then regret it, you can always
271 delete the branch with
272
273 -------------------------------------
274 $ git branch -D crazy-idea
275 -------------------------------------
276
277 Branches are cheap and easy, so this is a good way to try something
278 out.
279
280 Using Git for collaboration
281 ---------------------------
282
283 Suppose that you've started a new project with a Git repository in
284 /home/you/project, and you'd like another user on the same local
285 machine to be able to contribute to it. E.g. a www-data user to serve
286 the content up with a webserver.
287
288 As the `www-data` user do:
289
290 ------------------------------------------------
291 www-data$ git clone /home/you/project /var/www-data/deployment
292 ------------------------------------------------
293
294 This creates a new directory "deployment" containing a clone of your
295 repository.  The clone is on an equal footing with the original
296 project, possessing its own copy of the original project's history.
297
298 As `www-data` you then makes some changes and commit them:
299
300 ------------------------------------------------
301 (edit files)
302 www-data$ git commit -a
303 (repeat as necessary)
304 ------------------------------------------------
305
306 You can then pull those changes to the checkout in your home directory
307 at /home/you/project:
308
309 ------------------------------------------------
310 you$ cd /home/you/project
311 you$ git pull /var/www-data/deployment master
312 ------------------------------------------------
313
314 This merges the changes from the deployment repo's "master" branch into your
315 current branch.  If you've made other changes there in the meantime,
316 you may need to manually fix any conflicts.
317
318 The "pull" command thus performs two operations: it fetches changes
319 from a remote branch, then merges them into the current branch.
320
321 In general you'd want changes in your home directory to be committed before
322 initiating this "pull".  If your www-data work conflicts with them you
323 can use your working tree and the index to
324 resolve conflicts, and existing local changes will interfere with the
325 conflict resolution process (Git will still perform the fetch but will
326 refuse to merge --- You'll have to get rid of your local changes in
327 some way and pull again when this happens).
328
329 You can look at those changes merging first, using the "fetch"
330 command; this allows you to inspect the remote state, using a special
331 symbol "FETCH_HEAD", in order to determine if there's anything worth
332 pulling, like this:
333
334 ------------------------------------------------
335 $ git fetch /var/www-data/deployment master
336 you$ git log -p HEAD..FETCH_HEAD
337 ------------------------------------------------
338
339 This operation is safe even if you've got uncommitted local changes.
340 The range notation "HEAD..FETCH_HEAD" means "show everything that is reachable
341 from the FETCH_HEAD but exclude anything that is reachable from HEAD".
342 You know about everything that leads to your current state (HEAD),
343 and can review the state of the www-data repoistory (FETCH_HEAD).
344
345 If you want to visualize that difference
346 you can issue the following command:
347
348 ------------------------------------------------
349 $ gitk HEAD..FETCH_HEAD
350 ------------------------------------------------
351
352 This uses the same two-dot range notation that 'git log' does.
353
354 To see commits from both repositories did since they forked.
355 use three-dot form instead of the two-dot form:
356
357 ------------------------------------------------
358 $ gitk HEAD...FETCH_HEAD
359 ------------------------------------------------
360
361 This means "show everything that is reachable from either one, but
362 exclude anything that is reachable from both of them".
363
364 Please note that these range notation can be used with both gitk
365 and "git log".
366
367 After inspecting the difference you may
368 decide to continue working without pulling from www-data.  If that history
369 does have something you need you can
370 stash your work-in-progress first, do a "pull", and then finally unstash
371 it on top of the resulting history.
372
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:
377
378 ------------------------------------------------
379 you$ git remote add deployment /var/www-data/deployment
380 ------------------------------------------------
381
382 With this, you can use the "deployment" name for that remote
383 as an argument to 'git pull' or 'git fetch':
384
385 -------------------------------------
386 you$ git fetch deployment
387 -------------------------------------
388
389 Unlike the longhand form, when you fetch using a
390 remote repository shorthand set up with 'git remote', what was
391 fetched is stored in a remote-tracking branch, in this case
392 `deployment/master`.  So after this:
393
394 -------------------------------------
395 you$ git log -p master..deployment/master
396 -------------------------------------
397
398 shows a list of all the changes in deployment/master since it branched
399 off from your master branch.
400
401 After examining those changes, you can merge them into your master branch:
402
403 -------------------------------------
404 you$ git merge deployment/master
405 -------------------------------------
406
407 This `merge` can also be done by 'pulling from the remote-tracking
408 branch':
409
410 -------------------------------------
411 you$ git pull . remotes/deployment/master
412 -------------------------------------
413
414 Note that git pull always merges into the current branch,
415 regardless of what else is given on the command line.
416
417 Later, the www-data clone can be updated with your latest changes using
418
419 -------------------------------------
420 www-data$ git pull
421 -------------------------------------
422
423 Note that you don't need to supply the path to the original repository;
424 when you cloned it the path was stored in
425 the repository configuration, and that location is
426 used for pulls:
427
428 -------------------------------------
429 www-data$ git config --get remote.origin.url
430 /home/you/project
431 -------------------------------------
432
433 (The complete configuration created by 'git clone' is visible using
434 `git config -l`, and the linkgit:git-config[1] man page
435 explains the meaning of each option.)
436
437 That cloned repository also keeps a copy of its remote master branch under the
438 name "origin/master":
439
440 -------------------------------------
441 www-data$ git branch -r
442   origin/master
443 -------------------------------------
444
445 If you decide to move that deployment clone to a diffeent host, you can still
446 perform clones and pulls using the ssh protocol:
447
448 -------------------------------------
449 www-data$ git clone you.example.org:/home/you/project myrepo
450 -------------------------------------
451
452 Alternatively, Git has a native protocol, or can use http;
453 see linkgit:git-pull[1] for details.
454
455 Git can also be used in a CVS-like mode, with a central repository
456 that various users push changes to; see linkgit:git-push[1] and
457 linkgit:gitcvs-migration[7].
458
459 Exploring history
460 -----------------
461
462 Git history is represented as a series of interrelated commits.  We
463 have already seen that the 'git log' command can list those commits.
464 Note that first line of each git log entry also gives a name for the
465 commit:
466
467 -------------------------------------
468 $ git log
469 commit c82a22c39cbc32576f64f5c6b3f24b99ea8149c7
470 Author: Junio C Hamano <junkio@cox.net>
471 Date:   Tue May 16 17:18:22 2006 -0700
472
473     merge-base: Clarify the comments on post processing.
474 -------------------------------------
475
476 We can give this name to 'git show' to see the details about this
477 commit.
478
479 -------------------------------------
480 $ git show c82a22c39cbc32576f64f5c6b3f24b99ea8149c7
481 -------------------------------------
482
483 But there are other ways to refer to commits.  You can use any initial
484 part of the name that is long enough to uniquely identify the commit:
485
486 -------------------------------------
487 $ git show c82a22c39c   # the first few characters of the name are
488                         # usually enough
489 $ git show HEAD         # the tip of the current branch
490 $ git show experimental # the tip of the "experimental" branch
491 -------------------------------------
492
493 Every commit usually has one "parent" commit
494 which points to the previous state of the project:
495
496 -------------------------------------
497 $ git show HEAD^  # to see the parent of HEAD
498 $ git show HEAD^^ # to see the grandparent of HEAD
499 $ git show HEAD~4 # to see the great-great grandparent of HEAD
500 -------------------------------------
501
502 Note that merge commits may have more than one parent:
503
504 -------------------------------------
505 $ git show HEAD^1 # show the first parent of HEAD (same as HEAD^)
506 $ git show HEAD^2 # show the second parent of HEAD
507 -------------------------------------
508
509 You can also give commits names of your own; after running
510
511 -------------------------------------
512 $ git tag v2.5 1b2e1d63ff
513 -------------------------------------
514
515 you can refer to 1b2e1d63ff by the name "v2.5".  If you intend to
516 share this name with other people (for example, to identify a release
517 version), you should create a "tag" object, and perhaps sign it; see
518 linkgit:git-tag[1] for details.
519
520 Any Git command that needs to know a commit can take any of these
521 names.  For example:
522
523 -------------------------------------
524 $ git diff v2.5 HEAD     # compare the current HEAD to v2.5
525 $ git branch stable v2.5 # start a new branch named "stable" based
526                          # at v2.5
527 $ git reset --hard HEAD^ # reset your current branch and working
528                          # directory to its state at HEAD^
529 -------------------------------------
530
531 Be careful with that last command: in addition to losing any changes
532 in the working directory, it will also remove all later commits from
533 this branch.  If this branch is the only branch containing those
534 commits, they will be lost.  Also, don't use 'git reset' on a
535 publicly-visible branch that other developers pull from, as it will
536 force needless merges on other developers to clean up the history.
537 If you need to undo changes that you have pushed, use 'git revert'
538 instead.
539
540 The 'git grep' command can search for strings in any version of your
541 project, so
542
543 -------------------------------------
544 $ git grep "hello" v2.5
545 -------------------------------------
546
547 searches for all occurrences of "hello" in v2.5.
548
549 If you leave out the commit name, 'git grep' will search any of the
550 files it manages in your current directory.  So
551
552 -------------------------------------
553 $ git grep "hello"
554 -------------------------------------
555
556 is a quick way to search just the files that are tracked by Git.
557
558 Many Git commands also take sets of commits, which can be specified
559 in a number of ways.  Here are some examples with 'git log':
560
561 -------------------------------------
562 $ git log v2.5..v2.6            # commits between v2.5 and v2.6
563 $ git log v2.5..                # commits since v2.5
564 $ git log --since="2 weeks ago" # commits from the last 2 weeks
565 $ git log v2.5.. Makefile       # commits since v2.5 which modify
566                                 # Makefile
567 -------------------------------------
568
569 You can also give 'git log' a "range" of commits where the first is not
570 necessarily an ancestor of the second; for example, if the tips of
571 the branches "stable" and "master" diverged from a common
572 commit some time ago, then
573
574 -------------------------------------
575 $ git log stable..master
576 -------------------------------------
577
578 will list commits made in the master branch but not in the
579 stable branch, while
580
581 -------------------------------------
582 $ git log master..stable
583 -------------------------------------
584
585 will show the list of commits made on the stable branch but not
586 the master branch.
587
588 The 'git log' command has a weakness: it must present commits in a
589 list.  When the history has lines of development that diverged and
590 then merged back together, the order in which 'git log' presents
591 those commits is meaningless.
592
593 Most projects with multiple contributors (such as the Linux kernel,
594 or Git itself) have frequent merges, and 'gitk' does a better job of
595 visualizing their history.  For example,
596
597 -------------------------------------
598 $ gitk --since="2 weeks ago" drivers/
599 -------------------------------------
600
601 allows you to browse any commits from the last 2 weeks of commits
602 that modified files under the "drivers" directory.  (Note: you can
603 adjust gitk's fonts by holding down the control key while pressing
604 "-" or "+".)
605
606 Finally, most commands that take filenames will optionally allow you
607 to precede any filename by a commit, to specify a particular version
608 of the file:
609
610 -------------------------------------
611 $ git diff v2.5:Makefile HEAD:Makefile.in
612 -------------------------------------
613
614 You can also use 'git show' to see any such file:
615
616 -------------------------------------
617 $ git show v2.5:Makefile
618 -------------------------------------
619
620 Next Steps
621 ----------
622
623 This tutorial should be enough to perform basic distributed revision
624 control for your projects.  However, to fully understand the depth
625 and power of Git you need to understand two simple ideas on which it
626 is based:
627
628   * The object database is the rather elegant system used to
629     store the history of your project--files, directories, and
630     commits.
631
632   * The index file is a cache of the state of a directory tree,
633     used to create commits, check out working directories, and
634     hold the various trees involved in a merge.
635
636 Part two of this tutorial explains the object
637 database, the index file, and a few other odds and ends that you'll
638 need to make the most of Git. You can find it at linkgit:gittutorial-2[7].
639
640 If you don't want to continue with that right away, a few other
641 digressions that may be interesting at this point are:
642
643   * linkgit:git-format-patch[1], linkgit:git-am[1]: These convert
644     series of git commits into emailed patches, and vice versa,
645     useful for projects such as the Linux kernel which rely heavily
646     on emailed patches.
647
648   * linkgit:git-bisect[1]: When there is a regression in your
649     project, one way to track down the bug is by searching through
650     the history to find the exact commit that's to blame.  Git bisect
651     can help you perform a binary search for that commit.  It is
652     smart enough to perform a close-to-optimal search even in the
653     case of complex non-linear history with lots of merged branches.
654
655   * linkgit:gitworkflows[7]: Gives an overview of recommended
656     workflows.
657
658   * linkgit:giteveryday[7]: Everyday Git with 20 Commands Or So.
659
660   * linkgit:gitcvs-migration[7]: Git for CVS users.
661
662 SEE ALSO
663 --------
664 linkgit:gittutorial-2[7],
665 linkgit:gitcvs-migration[7],
666 linkgit:gitcore-tutorial[7],
667 linkgit:gitglossary[7],
668 linkgit:git-help[1],
669 linkgit:gitworkflows[7],
670 linkgit:giteveryday[7],
671 link:user-manual.html[The Git User's Manual]
672
673 GIT
674 ---
675 Part of the linkgit:git[1] suite