gittutorial.txt 22.4 KB
Newer Older
1 2 3 4 5
gittutorial(7)
==============

NAME
----
6
gittutorial - A tutorial introduction to Git (for version 1.5.1 or newer)
7 8 9

SYNOPSIS
--------
10
[verse]
11 12 13 14
git *

DESCRIPTION
-----------
15

16
This tutorial explains how to import a new project into Git, make
J. Bruce Fields's avatar
J. Bruce Fields committed
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 21 22
for example, to test the latest version, you may prefer to start with
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:
25

J. Bruce Fields's avatar
J. Bruce Fields committed
26
------------------------------------------------
27
$ man git-log
J. Bruce Fields's avatar
J. Bruce Fields committed
28
------------------------------------------------
29

30 31 32 33 34 35 36 37 38
or:

------------------------------------------------
$ git help log
------------------------------------------------

With the latter, you can use the manual viewer of your choice; see
linkgit:git-help[1] for more information.

39
It is a good idea to introduce yourself to Git with your name and
Nicolas Pitre's avatar
Nicolas Pitre committed
40 41
public email address before doing any operation.  The easiest
way to do so is:
42 43

------------------------------------------------
44 45
$ git config --global user.name "Your Name Comes Here"
$ git config --global user.email you@yourdomain.example.com
46 47 48
------------------------------------------------


J. Bruce Fields's avatar
J. Bruce Fields committed
49 50
Importing a new project
-----------------------
51

J. Bruce Fields's avatar
J. Bruce Fields committed
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

Junio C Hamano's avatar
Junio C Hamano committed
55
------------------------------------------------
56
$ tar xzf project.tar.gz
J. Bruce Fields's avatar
J. Bruce Fields committed
57
$ cd project
58
$ git init
Junio C Hamano's avatar
Junio C Hamano committed
59
------------------------------------------------
60

J. Bruce Fields's avatar
J. Bruce Fields committed
61
Git will reply
62

J. Bruce Fields's avatar
J. Bruce Fields committed
63
------------------------------------------------
64
Initialized empty Git repository in .git/
J. Bruce Fields's avatar
J. Bruce Fields committed
65
------------------------------------------------
66

J. Bruce Fields's avatar
J. Bruce Fields committed
67
You've now initialized the working directory--you may notice a new
68 69
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':
72

Junio C Hamano's avatar
Junio C Hamano committed
73
------------------------------------------------
J. Bruce Fields's avatar
J. Bruce Fields committed
74
$ git add .
Junio C Hamano's avatar
Junio C Hamano committed
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

J. Bruce Fields's avatar
J. Bruce Fields committed
81
------------------------------------------------
82
$ git commit
J. Bruce Fields's avatar
J. Bruce Fields committed
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 89 90
Making changes
--------------

91
Modify some files, then add their updated contents to the index:
92

Junio C Hamano's avatar
Junio C Hamano committed
93
------------------------------------------------
94
$ git add file1 file2 file3
Junio C Hamano's avatar
Junio C Hamano committed
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 102 103

------------------------------------------------
$ git diff --cached
------------------------------------------------

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 110 111 112 113 114 115 116 117 118 119 120 121

------------------------------------------------
$ git status
# On branch master
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#	modified:   file1
#	modified:   file2
#	modified:   file3
#
------------------------------------------------

If you need to make any further adjustments, do so now, and then add any
newly modified content to the index.  Finally, commit your changes with:
122

J. Bruce Fields's avatar
J. Bruce Fields committed
123
------------------------------------------------
Junio C Hamano's avatar
Junio C Hamano committed
124
$ git commit
J. Bruce Fields's avatar
J. Bruce Fields committed
125
------------------------------------------------
126

Fred Maranhão's avatar
Fred Maranhão committed
127
This will again prompt you for a message describing the change, and then
128
record a new version of the project.
129

130
Alternatively, instead of running 'git add' beforehand, you can use
131 132 133 134

------------------------------------------------
$ git commit -a
------------------------------------------------
135

136 137
which will automatically notice any modified (but not new) files, add
them to the index, and commit, all in one step.
138

J. Bruce Fields's avatar
J. Bruce Fields committed
139 140 141
A note on commit messages: Though not required, it's a good idea to
begin the commit message with a single short (less than 50 character)
line summarizing the change, followed by a blank line and then a more
142 143
thorough description. The text up to the first blank line in a commit
message is treated as the commit title, and that title is used
144
throughout Git.  For example, linkgit:git-format-patch[1] turns a
145 146
commit into email, and it uses the title on the Subject line and the
rest of the commit in the body.
147

148 149 150
Git tracks content not files
----------------------------

151 152
Many revision control systems provide an `add` command that tells the
system to start tracking changes to a new file.  Git's `add` command
153
does something simpler and more powerful: 'git add' is used both for new
154 155 156
and newly modified files, and in both cases it takes a snapshot of the
given files and stages that content in the index, ready for inclusion in
the next commit.
157

158 159
Viewing project history
-----------------------
160

J. Bruce Fields's avatar
J. Bruce Fields committed
161
At any point you can view the history of your changes using
162

J. Bruce Fields's avatar
J. Bruce Fields committed
163
------------------------------------------------
164
$ git log
J. Bruce Fields's avatar
J. Bruce Fields committed
165
------------------------------------------------
166

J. Bruce Fields's avatar
J. Bruce Fields committed
167
If you also want to see complete diffs at each step, use
168

J. Bruce Fields's avatar
J. Bruce Fields committed
169
------------------------------------------------
170
$ git log -p
J. Bruce Fields's avatar
J. Bruce Fields committed
171
------------------------------------------------
172

Junio C Hamano's avatar
Junio C Hamano committed
173 174 175 176 177 178 179
Often the overview of the change is useful to get a feel of
each step

------------------------------------------------
$ git log --stat --summary
------------------------------------------------

J. Bruce Fields's avatar
J. Bruce Fields committed
180 181
Managing branches
-----------------
Junio C Hamano's avatar
Junio C Hamano committed
182

183
A single Git repository can maintain multiple branches of
J. Bruce Fields's avatar
J. Bruce Fields committed
184
development.  To create a new branch named "experimental", use
185

J. Bruce Fields's avatar
J. Bruce Fields committed
186 187 188
------------------------------------------------
$ git branch experimental
------------------------------------------------
189

J. Bruce Fields's avatar
J. Bruce Fields committed
190
If you now run
191

J. Bruce Fields's avatar
J. Bruce Fields committed
192 193 194
------------------------------------------------
$ git branch
------------------------------------------------
195

J. Bruce Fields's avatar
J. Bruce Fields committed
196
you'll get a list of all existing branches:
197

Junio C Hamano's avatar
Junio C Hamano committed
198
------------------------------------------------
J. Bruce Fields's avatar
J. Bruce Fields committed
199 200
  experimental
* master
Junio C Hamano's avatar
Junio C Hamano committed
201
------------------------------------------------
202

J. Bruce Fields's avatar
J. Bruce Fields committed
203 204 205 206
The "experimental" branch is the one you just created, and the
"master" branch is a default branch that was created for you
automatically.  The asterisk marks the branch you are currently on;
type
207

J. Bruce Fields's avatar
J. Bruce Fields committed
208 209 210
------------------------------------------------
$ git checkout experimental
------------------------------------------------
211

J. Bruce Fields's avatar
J. Bruce Fields committed
212 213
to switch to the experimental branch.  Now edit a file, commit the
change, and switch back to the master branch:
214

J. Bruce Fields's avatar
J. Bruce Fields committed
215 216 217 218 219
------------------------------------------------
(edit file)
$ git commit -a
$ git checkout master
------------------------------------------------
220

J. Bruce Fields's avatar
J. Bruce Fields committed
221 222
Check that the change you made is no longer visible, since it was
made on the experimental branch and you're back on the master branch.
223

J. Bruce Fields's avatar
J. Bruce Fields committed
224
You can make a different change on the master branch:
225

J. Bruce Fields's avatar
J. Bruce Fields committed
226 227 228 229
------------------------------------------------
(edit file)
$ git commit -a
------------------------------------------------
230

J. Bruce Fields's avatar
J. Bruce Fields committed
231
at this point the two branches have diverged, with different changes
232
made in each.  To merge the changes made in experimental into master, run
Linus Torvalds's avatar
Linus Torvalds committed
233

J. Bruce Fields's avatar
J. Bruce Fields committed
234
------------------------------------------------
Nicolas Pitre's avatar
Nicolas Pitre committed
235
$ git merge experimental
J. Bruce Fields's avatar
J. Bruce Fields committed
236 237 238 239
------------------------------------------------

If the changes don't conflict, you're done.  If there are conflicts,
markers will be left in the problematic files showing the conflict;
240

Junio C Hamano's avatar
Junio C Hamano committed
241
------------------------------------------------
J. Bruce Fields's avatar
J. Bruce Fields committed
242
$ git diff
Junio C Hamano's avatar
Junio C Hamano committed
243
------------------------------------------------
244

J. Bruce Fields's avatar
J. Bruce Fields committed
245 246
will show this.  Once you've edited the files to resolve the
conflicts,
247

Junio C Hamano's avatar
Junio C Hamano committed
248
------------------------------------------------
J. Bruce Fields's avatar
J. Bruce Fields committed
249
$ git commit -a
Junio C Hamano's avatar
Junio C Hamano committed
250
------------------------------------------------
251

J. Bruce Fields's avatar
J. Bruce Fields committed
252
will commit the result of the merge. Finally,
253

Junio C Hamano's avatar
Junio C Hamano committed
254
------------------------------------------------
J. Bruce Fields's avatar
J. Bruce Fields committed
255
$ gitk
Junio C Hamano's avatar
Junio C Hamano committed
256
------------------------------------------------
257

J. Bruce Fields's avatar
J. Bruce Fields committed
258
will show a nice graphical representation of the resulting history.
259

260 261 262 263 264 265 266 267 268
At this point you could delete the experimental branch with

------------------------------------------------
$ git branch -d experimental
------------------------------------------------

This command ensures that the changes in the experimental branch are
already in the current branch.

J. Bruce Fields's avatar
J. Bruce Fields committed
269 270
If you develop on a branch crazy-idea, then regret it, you can always
delete the branch with
271

J. Bruce Fields's avatar
J. Bruce Fields committed
272 273 274
-------------------------------------
$ git branch -D crazy-idea
-------------------------------------
275

J. Bruce Fields's avatar
J. Bruce Fields committed
276 277
Branches are cheap and easy, so this is a good way to try something
out.
278

279
Using Git for collaboration
J. Bruce Fields's avatar
J. Bruce Fields committed
280
---------------------------
281

282
Suppose that Alice has started a new project with a Git repository in
J. Bruce Fields's avatar
J. Bruce Fields committed
283 284
/home/alice/project, and that Bob, who has a home directory on the
same machine, wants to contribute.
285

J. Bruce Fields's avatar
J. Bruce Fields committed
286
Bob begins with:
287

Junio C Hamano's avatar
Junio C Hamano committed
288
------------------------------------------------
289
bob$ git clone /home/alice/project myrepo
Junio C Hamano's avatar
Junio C Hamano committed
290
------------------------------------------------
291

J. Bruce Fields's avatar
J. Bruce Fields committed
292 293
This creates a new directory "myrepo" containing a clone of Alice's
repository.  The clone is on an equal footing with the original
294
project, possessing its own copy of the original project's history.
J. Bruce Fields's avatar
J. Bruce Fields committed
295 296

Bob then makes some changes and commits them:
Linus Torvalds's avatar
Linus Torvalds committed
297

J. Bruce Fields's avatar
J. Bruce Fields committed
298 299
------------------------------------------------
(edit files)
300
bob$ git commit -a
J. Bruce Fields's avatar
J. Bruce Fields committed
301 302
(repeat as necessary)
------------------------------------------------
Linus Torvalds's avatar
Linus Torvalds committed
303

J. Bruce Fields's avatar
J. Bruce Fields committed
304 305
When he's ready, he tells Alice to pull changes from the repository
at /home/bob/myrepo.  She does this with:
Linus Torvalds's avatar
Linus Torvalds committed
306

J. Bruce Fields's avatar
J. Bruce Fields committed
307
------------------------------------------------
308 309
alice$ cd /home/alice/project
alice$ git pull /home/bob/myrepo master
J. Bruce Fields's avatar
J. Bruce Fields committed
310
------------------------------------------------
Linus Torvalds's avatar
Linus Torvalds committed
311

312
This merges the changes from Bob's "master" branch into Alice's
313
current branch.  If Alice has made her own changes in the meantime,
314
then she may need to manually fix any conflicts.
315

316 317
The "pull" command thus performs two operations: it fetches changes
from a remote branch, then merges them into the current branch.
318

319 320 321 322
Note that in general, Alice would want her local changes committed before
initiating this "pull".  If Bob's work conflicts with what Alice did since
their histories forked, Alice will use her working tree and the index to
resolve conflicts, and existing local changes will interfere with the
323
conflict resolution process (Git will still perform the fetch but will
324 325 326 327 328 329 330 331 332 333
refuse to merge --- Alice will have to get rid of her local changes in
some way and pull again when this happens).

Alice can peek at what Bob did without merging first, using the "fetch"
command; this allows Alice to inspect what Bob did, using a special
symbol "FETCH_HEAD", in order to determine if he has anything worth
pulling, like this:

------------------------------------------------
alice$ git fetch /home/bob/myrepo master
334
alice$ git log -p HEAD..FETCH_HEAD
335 336 337
------------------------------------------------

This operation is safe even if Alice has uncommitted local changes.
338 339
The range notation "HEAD..FETCH_HEAD" means "show everything that is reachable
from the FETCH_HEAD but exclude anything that is reachable from HEAD".
340
Alice already knows everything that leads to her current state (HEAD),
341 342
and reviews what Bob has in his state (FETCH_HEAD) that she has not
seen with this command.
343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364

If Alice wants to visualize what Bob did since their histories forked
she can issue the following command:

------------------------------------------------
$ gitk HEAD..FETCH_HEAD
------------------------------------------------

This uses the same two-dot range notation we saw earlier with 'git log'.

Alice may want to view what both of them did since they forked.
She can use three-dot form instead of the two-dot form:

------------------------------------------------
$ gitk HEAD...FETCH_HEAD
------------------------------------------------

This means "show everything that is reachable from either one, but
exclude anything that is reachable from both of them".

Please note that these range notation can be used with both gitk
and "git log".
365 366 367 368 369 370 371

After inspecting what Bob did, if there is nothing urgent, Alice may
decide to continue working without pulling from Bob.  If Bob's history
does have something Alice would immediately need, Alice may choose to
stash her work-in-progress first, do a "pull", and then finally unstash
her work-in-progress on top of the resulting history.

372 373 374 375 376 377
When you are working in a small closely knit group, it is not
unusual to interact with the same repository over and over
again.  By defining 'remote' repository shorthand, you can make
it easier:

------------------------------------------------
378
alice$ git remote add bob /home/bob/myrepo
379 380
------------------------------------------------

381
With this, Alice can perform the first part of the "pull" operation
382
alone using the 'git fetch' command without merging them with her own
383
branch, using:
Junio C Hamano's avatar
Junio C Hamano committed
384

J. Bruce Fields's avatar
J. Bruce Fields committed
385
-------------------------------------
386
alice$ git fetch bob
J. Bruce Fields's avatar
J. Bruce Fields committed
387
-------------------------------------
Junio C Hamano's avatar
Junio C Hamano committed
388

389
Unlike the longhand form, when Alice fetches from Bob 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
`bob/master`.  So after this:
393

J. Bruce Fields's avatar
J. Bruce Fields committed
394
-------------------------------------
395
alice$ git log -p master..bob/master
J. Bruce Fields's avatar
J. Bruce Fields committed
396
-------------------------------------
397

J. Bruce Fields's avatar
J. Bruce Fields committed
398 399
shows a list of all the changes that Bob made since he branched from
Alice's master branch.
400

401
After examining those changes, Alice
Nicolas Pitre's avatar
Nicolas Pitre committed
402
could merge the changes into her master branch:
Linus Torvalds's avatar
Linus Torvalds committed
403

J. Bruce Fields's avatar
J. Bruce Fields committed
404
-------------------------------------
405
alice$ git merge bob/master
J. Bruce Fields's avatar
J. Bruce Fields committed
406
-------------------------------------
Linus Torvalds's avatar
Linus Torvalds committed
407

408 409
This `merge` can also be done by 'pulling from her own remote-tracking
branch', like this:
410 411

-------------------------------------
412
alice$ git pull . remotes/bob/master
413 414
-------------------------------------

415
Note that git pull always merges into the current branch,
416
regardless of what else is given on the command line.
417

J. Bruce Fields's avatar
J. Bruce Fields committed
418
Later, Bob can update his repo with Alice's latest changes using
Linus Torvalds's avatar
Linus Torvalds committed
419

J. Bruce Fields's avatar
J. Bruce Fields committed
420
-------------------------------------
421
bob$ git pull
J. Bruce Fields's avatar
J. Bruce Fields committed
422
-------------------------------------
Linus Torvalds's avatar
Linus Torvalds committed
423

J. Bruce Fields's avatar
J. Bruce Fields committed
424
Note that he doesn't need to give the path to Alice's repository;
425
when Bob cloned Alice's repository, Git stored the location of her
426 427
repository in the repository configuration, and that location is
used for pulls:
Junio C Hamano's avatar
Junio C Hamano committed
428

J. Bruce Fields's avatar
J. Bruce Fields committed
429
-------------------------------------
430
bob$ git config --get remote.origin.url
431
/home/alice/project
J. Bruce Fields's avatar
J. Bruce Fields committed
432
-------------------------------------
Junio C Hamano's avatar
Junio C Hamano committed
433

434
(The complete configuration created by 'git clone' is visible using
435
`git config -l`, and the linkgit:git-config[1] man page
436 437 438 439 440 441
explains the meaning of each option.)

Git also keeps a pristine copy of Alice's master branch under the
name "origin/master":

-------------------------------------
442
bob$ git branch -r
443 444
  origin/master
-------------------------------------
Junio C Hamano's avatar
Junio C Hamano committed
445

J. Bruce Fields's avatar
J. Bruce Fields committed
446 447
If Bob later decides to work from a different host, he can still
perform clones and pulls using the ssh protocol:
Junio C Hamano's avatar
Junio C Hamano committed
448

J. Bruce Fields's avatar
J. Bruce Fields committed
449
-------------------------------------
450
bob$ git clone alice.org:/home/alice/project myrepo
J. Bruce Fields's avatar
J. Bruce Fields committed
451
-------------------------------------
Junio C Hamano's avatar
Junio C Hamano committed
452

453
Alternatively, Git has a native protocol, or can use rsync or http;
454
see linkgit:git-pull[1] for details.
455

J. Bruce Fields's avatar
J. Bruce Fields committed
456
Git can also be used in a CVS-like mode, with a central repository
457
that various users push changes to; see linkgit:git-push[1] and
458
linkgit:gitcvs-migration[7].
459

460 461
Exploring history
-----------------
462

463
Git history is represented as a series of interrelated commits.  We
464
have already seen that the 'git log' command can list those commits.
465 466
Note that first line of each git log entry also gives a name for the
commit:
467

J. Bruce Fields's avatar
J. Bruce Fields committed
468
-------------------------------------
469 470 471 472 473 474
$ git log
commit c82a22c39cbc32576f64f5c6b3f24b99ea8149c7
Author: Junio C Hamano <junkio@cox.net>
Date:   Tue May 16 17:18:22 2006 -0700

    merge-base: Clarify the comments on post processing.
J. Bruce Fields's avatar
J. Bruce Fields committed
475
-------------------------------------
476

477
We can give this name to 'git show' to see the details about this
478
commit.
479

J. Bruce Fields's avatar
J. Bruce Fields committed
480
-------------------------------------
481
$ git show c82a22c39cbc32576f64f5c6b3f24b99ea8149c7
J. Bruce Fields's avatar
J. Bruce Fields committed
482
-------------------------------------
483

Junio C Hamano's avatar
Junio C Hamano committed
484
But there are other ways to refer to commits.  You can use any initial
485
part of the name that is long enough to uniquely identify the commit:
486

487 488 489 490 491 492 493
-------------------------------------
$ git show c82a22c39c	# the first few characters of the name are
			# usually enough
$ git show HEAD		# the tip of the current branch
$ git show experimental	# the tip of the "experimental" branch
-------------------------------------

494 495
Every commit usually has one "parent" commit
which points to the previous state of the project:
496

J. Bruce Fields's avatar
J. Bruce Fields committed
497
-------------------------------------
498 499 500
$ git show HEAD^  # to see the parent of HEAD
$ git show HEAD^^ # to see the grandparent of HEAD
$ git show HEAD~4 # to see the great-great grandparent of HEAD
J. Bruce Fields's avatar
J. Bruce Fields committed
501
-------------------------------------
502

503 504 505 506 507 508
Note that merge commits may have more than one parent:

-------------------------------------
$ git show HEAD^1 # show the first parent of HEAD (same as HEAD^)
$ git show HEAD^2 # show the second parent of HEAD
-------------------------------------
509

510
You can also give commits names of your own; after running
511

J. Bruce Fields's avatar
J. Bruce Fields committed
512
-------------------------------------
513
$ git tag v2.5 1b2e1d63ff
J. Bruce Fields's avatar
J. Bruce Fields committed
514
-------------------------------------
515

516 517 518
you can refer to 1b2e1d63ff by the name "v2.5".  If you intend to
share this name with other people (for example, to identify a release
version), you should create a "tag" object, and perhaps sign it; see
519
linkgit:git-tag[1] for details.
520

521
Any Git command that needs to know a commit can take any of these
522
names.  For example:
523

J. Bruce Fields's avatar
J. Bruce Fields committed
524
-------------------------------------
525 526 527 528
$ git diff v2.5 HEAD	 # compare the current HEAD to v2.5
$ git branch stable v2.5 # start a new branch named "stable" based
			 # at v2.5
$ git reset --hard HEAD^ # reset your current branch and working
Francis Daly's avatar
Francis Daly committed
529
			 # directory to its state at HEAD^
J. Bruce Fields's avatar
J. Bruce Fields committed
530
-------------------------------------
531

532 533 534
Be careful with that last command: in addition to losing any changes
in the working directory, it will also remove all later commits from
this branch.  If this branch is the only branch containing those
535
commits, they will be lost.  Also, don't use 'git reset' on a
536 537
publicly-visible branch that other developers pull from, as it will
force needless merges on other developers to clean up the history.
538
If you need to undo changes that you have pushed, use 'git revert'
539
instead.
540

541
The 'git grep' command can search for strings in any version of your
542
project, so
543

J. Bruce Fields's avatar
J. Bruce Fields committed
544
-------------------------------------
545
$ git grep "hello" v2.5
J. Bruce Fields's avatar
J. Bruce Fields committed
546
-------------------------------------
547

548
searches for all occurrences of "hello" in v2.5.
Junio C Hamano's avatar
Junio C Hamano committed
549

550
If you leave out the commit name, 'git grep' will search any of the
551
files it manages in your current directory.  So
Junio C Hamano's avatar
Junio C Hamano committed
552

J. Bruce Fields's avatar
J. Bruce Fields committed
553
-------------------------------------
554
$ git grep "hello"
555 556
-------------------------------------

557
is a quick way to search just the files that are tracked by Git.
J. Bruce Fields's avatar
J. Bruce Fields committed
558

559
Many Git commands also take sets of commits, which can be specified
560
in a number of ways.  Here are some examples with 'git log':
J. Bruce Fields's avatar
J. Bruce Fields committed
561 562

-------------------------------------
563 564 565 566 567
$ git log v2.5..v2.6            # commits between v2.5 and v2.6
$ git log v2.5..                # commits since v2.5
$ git log --since="2 weeks ago" # commits from the last 2 weeks
$ git log v2.5.. Makefile       # commits since v2.5 which modify
				# Makefile
J. Bruce Fields's avatar
J. Bruce Fields committed
568
-------------------------------------
569

570
You can also give 'git log' a "range" of commits where the first is not
571
necessarily an ancestor of the second; for example, if the tips of
572
the branches "stable" and "master" diverged from a common
573 574 575
commit some time ago, then

-------------------------------------
576
$ git log stable..master
577 578
-------------------------------------

579
will list commits made in the master branch but not in the
580 581 582
stable branch, while

-------------------------------------
583
$ git log master..stable
584 585 586
-------------------------------------

will show the list of commits made on the stable branch but not
587
the master branch.
588

589
The 'git log' command has a weakness: it must present commits in a
590
list.  When the history has lines of development that diverged and
591
then merged back together, the order in which 'git log' presents
592 593
those commits is meaningless.

594
Most projects with multiple contributors (such as the Linux kernel,
595
or Git itself) have frequent merges, and 'gitk' does a better job of
596 597 598 599 600 601 602
visualizing their history.  For example,

-------------------------------------
$ gitk --since="2 weeks ago" drivers/
-------------------------------------

allows you to browse any commits from the last 2 weeks of commits
603 604 605
that modified files under the "drivers" directory.  (Note: you can
adjust gitk's fonts by holding down the control key while pressing
"-" or "+".)
606 607 608

Finally, most commands that take filenames will optionally allow you
to precede any filename by a commit, to specify a particular version
609
of the file:
610 611 612 613

-------------------------------------
$ git diff v2.5:Makefile HEAD:Makefile.in
-------------------------------------
J. Bruce Fields's avatar
J. Bruce Fields committed
614

615
You can also use 'git show' to see any such file:
616 617

-------------------------------------
618
$ git show v2.5:Makefile
619 620
-------------------------------------

J. Bruce Fields's avatar
J. Bruce Fields committed
621 622 623
Next Steps
----------

624 625
This tutorial should be enough to perform basic distributed revision
control for your projects.  However, to fully understand the depth
626
and power of Git you need to understand two simple ideas on which it
627 628 629 630 631 632 633 634 635 636
is based:

  * The object database is the rather elegant system used to
    store the history of your project--files, directories, and
    commits.

  * The index file is a cache of the state of a directory tree,
    used to create commits, check out working directories, and
    hold the various trees involved in a merge.

637
Part two of this tutorial explains the object
638
database, the index file, and a few other odds and ends that you'll
639
need to make the most of Git. You can find it at linkgit:gittutorial-2[7].
640

641
If you don't want to continue with that right away, a few other
642
digressions that may be interesting at this point are:
J. Bruce Fields's avatar
J. Bruce Fields committed
643

644
  * linkgit:git-format-patch[1], linkgit:git-am[1]: These convert
J. Bruce Fields's avatar
J. Bruce Fields committed
645
    series of git commits into emailed patches, and vice versa,
646
    useful for projects such as the Linux kernel which rely heavily
J. Bruce Fields's avatar
J. Bruce Fields committed
647 648
    on emailed patches.

649
  * linkgit:git-bisect[1]: When there is a regression in your
J. Bruce Fields's avatar
J. Bruce Fields committed
650 651 652 653 654 655
    project, one way to track down the bug is by searching through
    the history to find the exact commit that's to blame.  Git bisect
    can help you perform a binary search for that commit.  It is
    smart enough to perform a close-to-optimal search even in the
    case of complex non-linear history with lots of merged branches.

656 657 658
  * linkgit:gitworkflows[7]: Gives an overview of recommended
    workflows.

659
  * link:everyday.html[Everyday Git with 20 Commands Or So]
660

661
  * linkgit:gitcvs-migration[7]: Git for CVS users.
662 663 664 665 666

SEE ALSO
--------
linkgit:gittutorial-2[7],
linkgit:gitcvs-migration[7],
667 668
linkgit:gitcore-tutorial[7],
linkgit:gitglossary[7],
669
linkgit:git-help[1],
670
linkgit:gitworkflows[7],
671
link:everyday.html[Everyday Git],
672 673 674 675
link:user-manual.html[The Git User's Manual]

GIT
---
676
Part of the linkgit:git[1] suite.