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