Code

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