@matheusbsbs / matheus-bagliano / commits / f1fe3846e4
Fork 0

tutorial: expanded discussion of commit history

Expand the history-browsing section of the tutorial a bit, in part to address Junio's suggestion that we mention "git grep" and Linus's complaint that people are missing the flexibility of the commandline interfaces for selecting commits. This reads a little more like a collection of examples than a "tutorial", but maybe that's what people need at this point. Signed-off-by: J. Bruce Fields <bfields@citi.umich.edu> Signed-off-by: Junio C Hamano <junkio@cox.net>

J. Bruce Fields committed May 21, 2006 at 16:54 UTC f1fe3846e4236a05879d7f84b11ae8ea64dd8d0d
1 file changed +112 -53
Documentation/tutorial.txt
+112 -53
@@ -288,103 +288,162 @@ Git can also be used in a CVS-like mode, with a central repository
288 that various users push changes to; see gitlink:git-push[1] and
289 link:cvs-migration.html[git for CVS users].
290
291 -Keeping track of history
292 -------------------------
291 +Exploring history
292 +-----------------
293
294 -Git history is represented as a series of interrelated commits. The
295 -most recent commit in the currently checked-out branch can always be
296 -referred to as HEAD, and the "parent" of any commit can always be
297 -referred to by appending a caret, "^", to the end of the name of the
298 -commit. So, for example,
294 +Git history is represented as a series of interrelated commits. We
295 +have already seen that the git log command can list those commits.
296 +Note that first line of each git log entry also gives a name for the
297 +commit:
298
299 -------------------------------------
301 -git diff HEAD^ HEAD
300 +$ git log
301 +commit c82a22c39cbc32576f64f5c6b3f24b99ea8149c7
302 +Author: Junio C Hamano <junkio@cox.net>
303 +Date: Tue May 16 17:18:22 2006 -0700
304 +
305 + merge-base: Clarify the comments on post processing.
306 -------------------------------------
307
304 -shows the difference between the most-recently checked-in state of
305 -the tree and the previous state, and
308 +We can give this name to git show to see the details about this
309 +commit.
310
311 -------------------------------------
308 -git diff HEAD^^ HEAD^
312 +$ git show c82a22c39cbc32576f64f5c6b3f24b99ea8149c7
313 -------------------------------------
314
311 -shows the difference between that previous state and the state two
312 -commits ago. Also, HEAD~5 can be used as a shorthand for HEAD{caret}{caret}{caret}{caret}{caret},
313 -and more generally HEAD~n can refer to the nth previous commit.
314 -Commits representing merges have more than one parent, and you can
315 -specify which parent to follow in that case; see
316 -gitlink:git-rev-parse[1].
315 +But there other ways to refer to commits. You can use any initial
316 +part of the name that is long enough to uniquely identify the commit:
317
318 -The name of a branch can also be used to refer to the most recent
319 -commit on that branch; so you can also say things like
318 +-------------------------------------
319 +$ git show c82a22c39c # the first few characters of the name are
320 + # usually enough
321 +$ git show HEAD # the tip of the current branch
322 +$ git show experimental # the tip of the "experimental" branch
323 +-------------------------------------
324 +
325 +Every commit has at least one "parent" commit, which points to the
326 +previous state of the project:
327
328 -------------------------------------
322 -git diff HEAD experimental
329 +$ git show HEAD^ # to see the parent of HEAD
330 +$ git show HEAD^^ # to see the grandparent of HEAD
331 +$ git show HEAD~4 # to see the great-great grandparent of HEAD
332 -------------------------------------
333
325 -to see the difference between the most-recently committed tree in
326 -the current branch and the most-recently committed tree in the
327 -experimental branch.
334 +Note that merge commits may have more than one parent:
335 +
336 +-------------------------------------
337 +$ git show HEAD^1 # show the first parent of HEAD (same as HEAD^)
338 +$ git show HEAD^2 # show the second parent of HEAD
339 +-------------------------------------
340
329 -But you may find it more useful to see the list of commits made in
330 -the experimental branch but not in the current branch, and
341 +You can also give commits names of your own; after running
342
343 -------------------------------------
333 -git log HEAD..experimental
344 +$ git-tag v2.5 1b2e1d63ff
345 -------------------------------------
346
336 -will do that, just as
347 +you can refer to 1b2e1d63ff by the name "v2.5". If you intend to
348 +share this name with other people (for example, to identify a release
349 +version), you should create a "tag" object, and perhaps sign it; see
350 +gitlink:git-tag[1] for details.
351 +
352 +Any git command that needs to know a commit can take any of these
353 +names. For example:
354
355 -------------------------------------
339 -git log experimental..HEAD
356 +$ git diff v2.5 HEAD # compare the current HEAD to v2.5
357 +$ git branch stable v2.5 # start a new branch named "stable" based
358 + # at v2.5
359 +$ git reset --hard HEAD^ # reset your current branch and working
360 + # directory its state at HEAD^
361 -------------------------------------
362
342 -will show the list of commits made on the HEAD but not included in
343 -experimental.
363 +Be careful with that last command: in addition to losing any changes
364 +in the working directory, it will also remove all later commits from
365 +this branch. If this branch is the only branch containing those
366 +commits, they will be lost. (Also, don't use "git reset" on a
367 +publicly-visible branch that other developers pull from, as git will
368 +be confused by history that disappears in this way.)
369
345 -You can also give commits convenient names of your own: after running
370 +The git grep command can search for strings in any version of your
371 +project, so
372
373 -------------------------------------
348 -$ git-tag v2.5 HEAD^^
374 +$ git grep "hello" v2.5
375 -------------------------------------
376
351 -you can refer to HEAD^^ by the name "v2.5". If you intend to share
352 -this name with other people (for example, to identify a release
353 -version), you should create a "tag" object, and perhaps sign it; see
354 -gitlink:git-tag[1] for details.
377 +searches for all occurences of "hello" in v2.5.
378
356 -You can revisit the old state of a tree, and make further
357 -modifications if you wish, using git branch: the command
379 +If you leave out the commit name, git grep will search any of the
380 +files it manages in your current directory. So
381
382 -------------------------------------
360 -$ git branch stable-release v2.5
383 +$ git grep "hello"
384 -------------------------------------
385
363 -will create a new branch named "stable-release" starting from the
364 -commit which you tagged with the name v2.5.
386 +is a quick way to search just the files that are tracked by git.
387
366 -You can reset the state of any branch to an earlier commit at any
367 -time with
388 +Many git commands also take sets of commits, which can be specified
389 +in a number of ways. Here are some examples with git log:
390
391 -------------------------------------
370 -$ git reset --hard v2.5
392 +$ git log v2.5..v2.6 # commits between v2.5 and v2.6
393 +$ git log v2.5.. # commits since v2.5
394 +$ git log --since="2 weeks ago" # commits from the last 2 weeks
395 +$ git log v2.5.. Makefile # commits since v2.5 which modify
396 + # Makefile
397 -------------------------------------
398
373 -This will remove all later commits from this branch and reset the
374 -working tree to the state it had when the given commit was made. If
375 -this branch is the only branch containing the later commits, those
376 -later changes will be lost. Don't use "git reset" on a
377 -publicly-visible branch that other developers pull from, as git will
378 -be confused by history that disappears in this way.
399 +You can also give git log a "range" of commits where the first is not
400 +necessarily an ancestor of the second; for example, if the tips of
401 +the branches "stable-release" and "master" diverged from a common
402 +commit some time ago, then
403 +
404 +-------------------------------------
405 +$ git log stable..experimental
406 +-------------------------------------
407 +
408 +will list commits made in the experimental branch but not in the
409 +stable branch, while
410 +
411 +-------------------------------------
412 +$ git log experimental..stable
413 +-------------------------------------
414 +
415 +will show the list of commits made on the stable branch but not
416 +the experimental branch.
417 +
418 +The "git log" command has a weakness: it must present commits in a
419 +list. When the history has lines of development that diverged and
420 +then merged back together, the order in which "git log" presents
421 +those commits is meaningless.
422 +
423 +Most projects with multiple contributors (such as the linux kernel,
424 +or git itself) have frequent merges, and gitk does a better job of
425 +visualizing their history. For example,
426 +
427 +-------------------------------------
428 +$ gitk --since="2 weeks ago" drivers/
429 +-------------------------------------
430 +
431 +allows you to browse any commits from the last 2 weeks of commits
432 +that modified files under the "drivers" directory.
433 +
434 +Finally, most commands that take filenames will optionally allow you
435 +to precede any filename by a commit, to specify a particular version
436 +fo the file:
437 +
438 +-------------------------------------
439 +$ git diff v2.5:Makefile HEAD:Makefile.in
440 +-------------------------------------
441
442 Next Steps
443 ----------
444
445 Some good commands to explore next:
446
385 - * gitlink:git-diff[1]: This flexible command does much more than
386 - we've seen in the few examples above.
387 -
447 * gitlink:git-format-patch[1], gitlink:git-am[1]: These convert
448 series of git commits into emailed patches, and vice versa,
449 useful for projects such as the linux kernel which rely heavily