You are previewing Git Pocket Guide.

Git Pocket Guide

Cover of Git Pocket Guide by Richard E. Silverman Published by O'Reilly Media, Inc.
  1. Special Upgrade Offer
  2. Preface
    1. What Is Git?
    2. Goals of This Book
    3. Conventions Used in This Book
      1. Unix
      2. Shell
      3. Command Syntax
      4. Typography
    4. Using Code Examples
    5. Safari® Books Online
    6. How to Contact Us
    7. Acknowledgments
  3. 1. Understanding Git
    1. Overview
      1. Terminology
      2. Branches
      3. Sharing Work
    2. The Object Store
      1. Blob
      2. Tree
      3. Commit
      4. Tag
    3. Object IDs and SHA-1
      1. Security
    4. Where Objects Live
    5. The Commit Graph
    6. Refs
      1. Related Commands
    7. Branches
    8. The Index
    9. Merging
      1. Merging Content
      2. Merging History
    10. Push and Pull
      1. Notes
  4. 2. Getting Started
    1. Basic Configuration
      1. Personal Identification
      2. Text Editor
      3. Commit ID Abbreviation
      4. Pagination
      5. Color
      6. Cryptographic Keys
      7. Command Aliases
      8. Getting Help
      9. References
    2. Creating a New, Empty Repository
      1. Selected Options
    3. Importing an Existing Project
    4. Ignoring Files
      1. Syntax of “Ignore Patterns”
  5. 3. Making Commits
    1. Changing the Index
      1. Adding a New File
      2. Adding the Changes to an Existing File
      3. Adding Partial Changes
      4. Shortcuts
      5. Removing a File
      6. Renaming a File
      7. Unstaging Changes
    2. Making a Commit
      1. Commit Messages
      2. What Makes a Good Commit?
      3. Shortcuts
      4. Empty Directories
      5. A Commit Workflow
  6. 4. Undoing and Editing Commits
    1. Changing the Last Commit
      1. Double Oops!
    2. Discarding the Last Commit
      1. Discarding Any Number of Commits
    3. Undoing a Commit
      1. Partial Undo
    4. Editing a Series of Commits
      1. Conflicts
      2. The exec Action
  7. 5. Branching
    1. The Default Branch, master
    2. Making a New Branch
    3. Switching Branches
      1. Uncommitted Changes
      2. Untracked Files
      3. Losing Your Head
    4. Deleting a Branch
    5. Renaming a Branch
  8. 6. Tracking Other Repositories
    1. Cloning a Repository
      1. Clones and Hard Links
      2. Bare Repositories
      3. Reference Repositories
    2. Local, Remote, and Tracking Branches
    3. Synchronization: Push and Pull
      1. Pulling
      2. Pushing
      3. Push Defaults
      4. Pull with Rebase
      5. Notes
    4. Access Control
  9. 7. Merging
    1. Merge Conflicts
      1. Resolving Merge Conflicts
      2. Notes
    2. Details on Merging
    3. Merge Tools
      1. Notes
    4. Custom Merge Tools
    5. Merge Strategies
    6. Why the Octopus?
    7. Reusing Previous Merge Decisions
  10. 8. Naming Commits
    1. Naming Individual Commits
      1. Commit ID
      2. Ref Name
      3. Names Relative to a Given Commit
      4. Names Relative to the Reflog
      5. The Upstream Branch
      6. Matching a Commit Message
      7. Following Chains
      8. Addressing Pathnames
    2. Naming Sets of Commits
  11. 9. Viewing History
    1. Command Format
    2. Output Formats
    3. Defining Your Own Formats
      1. Notes
    4. Limiting Commits to Be Shown
    5. Regular Expressions
    6. Reflog
    7. Decoration
    8. Date Style
    9. Listing Changed Files
    10. Showing and Following Renames or Copies
      1. Detecting Copies
    11. Rewriting Names and Addresses: The “mailmap”
      1. Shortening Names
    12. Searching for Changes: The “pickaxe”
    13. Showing Diffs
      1. Color
      2. Word Diff
    14. Comparing Branches
      1. Displaying Sides
    15. Showing Notes
    16. Commit Ordering
    17. History Simplification
    18. Related Commands
      1. git cherry
      2. git shortlog
  12. 10. Editing History
    1. Rebasing
      1. Undoing a Rebase
    2. Importing from One Repository to Another
      1. Importing Disconnected History
      2. Importing Linear History
      3. Importing Nonlinear History
    3. Commit Surgery: git replace
      1. Keeping It Real
    4. The Big Hammer: git filter-branch
      1. Examples
    5. Notes
  13. 11. Understanding Patches
    1. Applying Plain Diffs
    2. Patches with Commit Information
  14. 12. Remote Access
    1. SSH
    2. HTTP
    3. Storing Your Username
    4. Storing Your Password
    5. References
  15. 13. Miscellaneous
    1. git cherry-pick
    2. git notes
      1. git notes Subcommands
    3. git grep
      1. Combining Regular Expressions
      2. What to Search
      3. What to Show
      4. How to Match
    4. git rev-parse
    5. git clean
    6. git stash
      1. Subcommands
    7. git show
    8. git tag
      1. Deleting a Tag from a Remote
      2. Following Tags
      3. Backdating Tags
    9. git diff
      1. git diff
      2. git diff --staged
      3. git diff <commit>
      4. git diff <A> <B>
      5. Options and Arguments
    10. git instaweb
    11. Git Hooks
    12. Visual Tools
    13. Submodules
  16. 14. How Do I…?
    1. …Make and Use a Central Repository?
    2. …Fix the Last Commit I Made?
    3. …Edit the Previous n Commits?
    4. …Undo My Last n Commits?
    5. …Reuse the Message from an Existing Commit?
    6. …Reapply an Existing Commit from Another Branch?
    7. …List Files with Conflicts when Merging?
    8. …Get a Summary of My Branches?
    9. …Get a Summary of My Working Tree and Index State?
    10. …Stage All the Current Changes to My Working Files?
    11. …Show the Changes to My Working Files?
    12. …Save and Restore My Working Tree and Index Changes?
    13. …Add a Downstream Branch Without Checking It Out?
    14. …List the Files in a Specific Commit?
    15. …Show the Changes Made by a Commit?
    16. …Get Tab Completion of Branch Names, Tags, and So On?
    17. …List All Remotes?
    18. …Change the URL for a Remote?
    19. …Remove Old Remote-Tracking Branches?
    20. …Have git log:
      1. Find Commits I Made but Lost?
      2. Not Show the diffs for Root Commits?
      3. Show the Changes for Each Commit?
      4. Show the Committer as well as the Author?
  17. Index
  18. About the Author
  19. Special Upgrade Offer
  20. Copyright

Chapter 9. Viewing History

The primary command for examining the commit history of your repository is git log. The documentation for this command, git-log(1), is about 30 pages long, and we shall not repeat all that detail here. We will cover its main modes of operation, as well as a selection of the most common and useful options and techniques.

Command Format

The format of the command is:

$ git log [options] [commits] [[--] path ...]

The commits parameter specifies the commits Git should list for you, using the notation discussed in Naming Sets of Commits; for example:

git log
The default for commits is HEAD, so this lists the commits reachable from the current HEAD commit. This is generally a branch, but may not be if you have checked out an arbitrary commit and are in “detached HEAD” mode (see Branches).
git log topic
Lists the commits in the topic branch, even if you are on another branch.
git log alvin simon
Lists all commits on either of the branches alvin or simon.
git log alvin..simon
Lists all commits in simon that are not in alvin; this is often those commits on simon that have occurred since you last merged it with alvin.

Here, the names topic, alvin, and simon could also be tags, or expressions such as master~3 or 780ae563. You can also use patterns to indicate sets of refs instead of listing them all individually, with these options:


Behave as if all branches, tags, or remotes were given on the command line as the commits argument, optionally limiting the refs matched with a glob pattern. Match refs directly with --glob=pattern; a leading refs/ is implied if not given. A trailing /* is implied if the pattern does not contain * or ?.

Synonyms: --all = --glob='*'

Thus, git log --branches='foo*' lists all branches whose names begin with “foo”: refs/heads/foobar, refs/heads/foodie, etc.

The optional list of file pathnames or glob patterns further limits the commits listed, to those that touched matching paths by either adding, deleting, or modifying the named files. Use the -- separator in case there’s some ambiguity with the preceding options or commit names.

Output Formats

The default output format is fairly detailed, including the author timestamp and commit message:

$ git log
commit 86815742
Author: Richard E. Silverman <>
Date:   Tue Sep 18 14:36:00 2012 -0700

   reduce annoyance

   Fix this software so that it is slightly less
   annoying than it was before, though less annoyance
   would still be good.

commit 72e4d8e8
Merge: 5ac81f5f af771c39
Author: Witch King of Angmar <>
Date:   Tue Sep 18 14:35:54 2012 -0700

   Merge branch 'hobbits'

   Some scholars are of the opinion that "nazgûl" is
   exclusively plural, so that one does not speak of
   "a Nazgûl." Of course, it's best not to speak of
   them at all, regardless.

git log --oneline gives more compact output, including just the ID and message subject for each commit:

$ git log --oneline
86815742 reduce annoyance
72e4d8e8 Merge branch 'hobbits'

Note that this is one reason to format your commit messages in the conventional way, with a subject line: it makes this sort of summary readable, as opposed to just showing the beginning of a sentence trailing off to the right (see Commit Messages).

The --oneline option is actually short for --format=oneline --abbrev-commit, and the default is --format=medium. There are a number of predefined formats; the following table shows the full list, along with some commit elements they contain (they all show the commit ID).

format author author date committer commit date subject message








The email format produces output in traditional Unix “mbox” style, with one email message per commit (and here’s yet another reason for the standard commit message format: commit subject lines become the email subject headers of each message). You can use it to prepare a set of email messages describing some commits, which can be easily read, manipulated, and sent with most Unix-based mail programs.

The raw format shows all the information in the commit in full detail and uninterpreted format, including the full 40-digit IDs of the parent commits and content tree object.

Defining Your Own Formats

You can also customize the display format, with git log --format ="format:string". You can give a format using a set of substitutions similar in usage to the printf function in the C standard library (and widely copied in other languages). The full set of substitutions is in the PRETTY FORMATS section of git-log(1); here are some examples:

# committer, commit ID, relative timestamp, subject
$ git log --date=relative --format='%an, %h, %ar, "%s"
Richard E. Silverman, 86815742, 6 hours ago, "reduce …
Witch King of Angmar, 72e4d8e8, 7 hours ago, "Merge b…

This example uses color and underlining to distinguish the different fields on the line. The colors may not show here depending on the medium in which you’re reading this text, but give it a try (it assumes your terminal is set up to handle color, of course):

# commit ID, subject, committer, date
$ git log --date=short --format=\
"%C(blue)%h %C(reset)%s %C(magenta)%aN %C(green ul)\
86815742 reduce annoyance Richard E. Silverman 2012-1…
72e4d8e8 Merge branch 'hobbits' Witch King of Angmar …

Make sure to use %Creset at the end of such a format; otherwise, if the output is going directly to a terminal rather than through a pager, you’ll leave the terminal stuck in whatever color or mode you last used. You can add a format you use frequently to your configuration in ~/.gitconfig or elsewhere:

 colorful = "%C(blue)%h %C(reset)%s %C(magenta)%aN
 %C(green ul)%ad%C(reset)"

and then refer to it by name:

$ git log --format=colorful


  • To use a double quote in a format string given in a Git configuration file, escape it with backslash.
  • --pretty is a synonym for --format (from the term “pretty printing”).
  • A format given as format:template places a newline between each log item; there is no newline after the final item. Use tformat:template instead to get a final newline (“t” for “terminator”).
  • If the --format argument contains a percent sign (%), then Git assumes tformat:, as in the previous example.
  • You can change the default format for git log by setting format.pretty; this affects git show as well.

Limiting Commits to Be Shown

There are many options for further limiting the commits to be shown beyond the commits expression given as an argument to git log; here is a selection of common ones:

-n (-n n, --max-count=n)
Only show the first n commits.
Skip n leading commits before starting output.
Show commits made before or after a specific date (synonyms: --{until,since}). Note that this refers to the commit timestamp; there is no analogous simple way to refer to the author timestamp.
Show only commits whose author or committer header (name <email>) matches the given regular expression. Multiple instances of a given constraint are combined with logical “or,” but (as usual) use of both types counts as logical “and”; thus, git log --author=Richard --author=Booboo --committer=Felix shows commits made by Felix, whose author is either Richard or Booboo.
Show only commits whose log messages match the given regular expression. Multiple instances are combined with logical “or”; change this to “and” with --all-match. Use --grep-reflog to match reflog entries instead, when using git log -g to examine the reflog instead of the commit graph (--grep still matches the commit message, even though the commits examined are found via the reflog; it does not match the reflog comment instead).

Show only commits with a matching number of parent commits. Synonyms:

  • --merges = --min-parents=2
  • --no-merges = --max-parents=1
Follow only the first parent of a merge commit, rather than all of them. This can give a more useful history of a topic branch into which you periodically merge from a more central branch, keeping it up to date with the main development. This shows only the activity on the topic branch itself, rather than commits brought in from the main branch by merging.

Show commits containing files with any of the statuses given by the following one-letter codes. The “copied” and “renamed” statuses will only be effective if copy and rename detection are enabled as described:

  • A: Added
  • C: Copied
  • D: Deleted
  • M: Modified
  • R: Renamed
  • T: Type change (e.g., a file replaced by a symbolic link)

Regular Expressions

A number of options affect the interpretation of regular expressions:

-i (--regexp-ignore-case)
Ignore case differences (e.g., hello and HELLO will both match “Hello”).
-E (--extended-regexp)
Use extended regular expressions; the default type is basic.
-F (--fixed-strings)
Consider the limiting patterns as literal strings to be matched; that is, don’t interpret them as regular expressions at all.
Use Perl-style regular expressions. This will not be available if Git is not built with the --with-libpcre option, which is not on by default.


git log --walk-reflog (-g) shows a completely different log: the reflog. This is a log of actions you’ve taken in your repository, and it can be very helpful in recovering from mistakes; see Double Oops!.


git log --decorate={no,short,full} shows refs pointing to the listed commits:

$ git log --decorate
commit feca033e (HEAD, master)
Author: Richard E. Silverman <>
Date:   Thu Dec 20 00:38:51 2012 -0500


commit 6faac5df (u/master, origin/master, origin/HEAD)
Author: Richard E. Silverman <>
Date:   Mon Dec 3 03:18:43 2012 -0500

    working on ch09

commit 110dac65
Author: Richard E. Silverman <>
Date:   Mon Dec 3 03:18:09 2012 -0500

    minor editing on earlier chapters

Note the inclusion in parentheses of various local and remote branch names. The default is short; full uses the full ref name (e.g., refs/heads/master instead of just master).

Date Style

git log --date= {local,relative,default,iso,rfc,short,raw}
This option affects how dates are rendered in formatted log output, as long as the format has not explicitly given a date style. For example, using this format:
    compact = %h %ad, \"%s\"

$ git log -1 --format=compact --date=local
6faac5df Mon Dec 3 03:18:43 2012, "working on ch09"

$ git log -1 --format=compact --date=relative
6faac5df 2 weeks ago, "working on ch09"

$ git log -1 --format=compact --date=iso
6faac5df 2012-12-03 03:18:43 -0500, "working on ch09"

$ git log -1 --format=compact --date=rfc
6faac5df Mon, 3 Dec 2012 03:18:43 -0500, "working on …

$ git log -1 --format=compact --date=short
6faac5df 2012-12-03, "working on ch09"

$ git log -1 --format=compact --date=raw
6faac5df 1354522723 -0500, "working on ch09"
Original time zone of author or committer
Local time zone
How far in the past
ISO 8601 format
RFC 2822 format (as found in email)
Internal Git format

Listing Changed Files

git log --name-status summarizes which files changed in a given commit (relative to its predecessor), and the nature of the changes:

$ git log --name-status
commit bc0ba0f7
Author: Richard E. Silverman <>
Date:   Wed Dec 19 23:31:49 2012 -0500

    fix directory; misc diffs with older ghc

M       keepmeta.hs

commit f6a96775
Author: Richard E. Silverman <>
Date:   Wed Dec 19 21:48:26 2012 -0500

    rename keepmeta

D       .gitfoo
A       Makefile
D       commit.hs
A       keepmeta.hs

The single-letter codes to the left of the filenames, indicating the change status of that file in the commit, are the same as listed for the --diff-filter option for added, deleted, modified, and so on.

git log --name-only lists only filenames without the status codes, and --stat gives an ASCII-art graph (“diffstat”) representing the amount and kind of change in each file:

$ git log --stat
commit ddcd718b
Author: Richard E. Silverman <>
Date:   Sun Dec 9 23:47:50 2012 -0500

   add KDC default referral feature

   Two new realm configuration parameters:

   * default_referral_realm (string, none)
   * cross_realm_default_referral (boolean, false)

   If default_referral_realm is set, then the KDC
   will issue referrals to the specified realm for
   TGS requests otherwise qualifying for a referral
   but lacking a static realm mapping, as long as the
   presented TGT is not cross-realm (setting
   cross_realm_default_referral omits that check).

src/config-files/kdc.conf.M  | 12 +
src/include/adm.h           |  4 +
src/include/k5-int.h        |  2 
src/kdc/do_tgs_req.c         | 52 +----------
src/kdc/extern.h            |  4 +
src/kdc/main.c              | 12 
src/lib/kadm5/admin.h        |  5 -
src/lib/kadm5/alt_prof.c    | 15 +
8 files changed, 95 insertions(+), 11 deletions(-)

git log --dirstat summarizes the amount of change in subdirectories (it can take a number of parameters controlling how the summarization is done):

$ git log --dirstat
commit 4dd1530f (tag: mit-krb5-1.10.3, origin/MIT)
Author: Richard E. Silverman <>
Date:   Mon Jan 9 15:03:23 2012 -0500

    import MIT Kerberos 1.10.3

  52.1% doc/
   6.0% src/lib/
  12.4% src/windows/identity/doc/
   3.7% src/windows/identity/ui/
   8.8% src/windows/identity/
   3.5% src/windows/leash/htmlhelp/
   3.4% src/windows/leash/
   9.5% src/

Showing and Following Renames or Copies

Ordinary git log does not show file renaming, because it takes longer to do this and often you’re not interested. To enable renaming detection, use --find-renames[=n] (-M[n]). The optional integer n is an index of similarity: consider a delete/add pair to be a rename if the before/after files are at least n% identical (the default is 100%):

$ git log --name-status
commit 4a933304 (HEAD, master)
Author: Richard E. Silverman <>
Date:   Thu Dec 20 01:08:14 2012 -0500

    Rename foo; wouldn’t bar be better?

D       foo
A       bar

$ git log --name-status -M
commit 4a933304 (HEAD, master)
Author: Richard E. Silverman <>
Date:   Thu Dec 20 01:08:14 2012 -0500

    Rename foo; wouldn’t bar be better?

R100    foo     bar

To have Git follow a file past a rename, use git log --follow; this only works when you give a single file to follow:

$ git log bar
commit 4a933304 (HEAD, master)
Author: Richard E. Silverman <>
Date:   Thu Dec 20 01:08:14 2012 -0500

    Rename foo; wouldn’t bar be better?

$ git log --follow bar
commit 4a933304 (HEAD, master)
Author: Richard E. Silverman <>
Date:   Thu Dec 20 01:08:14 2012 -0500

    Rename foo; wouldn’t bar be better?

commit 4e286d96
Author: Richard E. Silverman <>
Date:   Tue Dec 18 04:57:55 2012 -0500

    Add “foo” in its glorious fooness!

Detecting Copies

A “copied” file is a new path appearing in a commit with identical or similar contents to an existing one (one already in a prior commit). git log --find-copies[=n] (-C[n]) does the same for detecting copies as -M does for renames. -CC (or --find-copies-harder) will consider all files in a commit as potential sources of copying, while plain -C considers only files that changed in that commit.

Rewriting Names and Addresses: The “mailmap”

The same person’s name or email address as embedded in commits may vary in a single repository history, depending on settings she had at various times as she was working. Git has a facility to normalize these for display and collation, called the mailmap. A mailmap file may be named .mailmap at the top of the working tree, or have any name given by the mailmap.file configuration option, and has lines in any of the following formats:

Correct Name <>
This collates by address and rewrites names: entries of this form with the same address identify commits marked with those addresses as being by the same person, with the specified name replacing the names given in those commits.
<desired@email.address> <random@other.address>
This collates by address and rewrites addresses: entries of this form with the same desired address, but differing “random other” addresses, identify commits by the varying addresses as being by the same person, with the specified address replacing those appearing in the commits (but leaving the names alone).
Correct Name <> <>
This collates by address and rewrites both name and address: entries of this form, with the same desired address but differing names and other addresses, identify those commits as being by the same person, with the specified name and desired address replacing those appearing in the commits.
Correct Name <> Other Name <>
This collates by both name and address, and rewrites both as well: entries of this form with the same “correct name” and desired address identify commits marked with the given combinations of “other” name/address pairs as being by the same person, with the specified name and address replacing those appearing in the commits.

For example, this mailmap entry:

Richard E. Silverman <>

coalesces all commits marked with the address and presents my name consistently as “Richard E. Silverman,” even if some say “Richard Silverman” or “Rich S.” These entries:

Richard E. Silverman <> <>
Richard E. Silverman <> <>
Richard E. Silverman <> <>

identify commits marked with the three addresses appearing on the right, and rewrite both name and address to be “Richard E. Silverman” and

The coalescing and rewriting features are used by the command git shortlog, which summarizes history using the commit subjects (or other format given by --format) and grouping by author. The rewriting feature alone is used by git log and git blame if the format specifies it. Particular escapes for committer and author info, given in the PRETTY FORMATS section of git-log(1), take the mailmap (if any) into account; for example, this version of the “compact” format defined earlier:

        compact = %aN (%h) %aD, \"%s\"

(note the capital N and D) shows the author name and address as rewritten by the mailmap.

Shortening Names

Another use for the mailmap is shortening names for compact display. Full names can be truncated and difficult to read in a short format, such as the common git log --oneline. You can maintain a mailmap rewriting full names to your organization’s computer account names, for example, which are typically shorter. You can then define log formats that use them as above; you could place these in the system-level Git configuration used by everyone (usually /etc/gitconfig), or have a smaller group explicitly include a shared file via the include.path variable. This mailmap:

res <>
res <>
john <>
john <>

together with the second “compact” log format above causes the author name and addresses for Richard Silverman and John Preston to appear as “res” and “john” instead, also taking into account two different email address formats.

Searching for Changes: The “pickaxe”

The Git “pickaxe,” git log -S string, lists commits that changed the number of occurrences of string in at least one file. Note that this is slightly different from string appearing in the commit diff at all: if a commit removed one occurrence and added another one elsewhere, the pickaxe will not show it. Nonetheless, this is a useful method of looking for changes. For example, if you want to know when a particular feature was added, using this command with the name of a function or variable specific to the feature will turn it up, as the earliest commit that introduced that term. git log -G pattern does the same with a regular expression.

If you combine the pickaxe with a git log option that lists files, such as --name-status, Git shows only those files that triggered the listing (those in which the number of string or pattern occurrences changed). If you add --pickaxe-all, then Git shows all files touched by the listed commits. This allows you to see the entire changeset associated with any commit that matched the pattern you’re interested in.

Showing Diffs

git log -p shows the “patch” or “diff” associated with each commit (illustrating the actual changes made to the files, only for text files, naturally), after the usual commit information as indicated by the log format in use. Normally, no diff is shown for merge commits, however you can use these options:

Shows each pairwise diff between the merge and its parents.
Shows the differences with all parents simultaneously in a merged format (a generalization of the traditional “unified diff”), rather than serially as with -m, and only for files that were modified in all branches.
Implies -c and further simplifies the diff by showing only conflicts; change regions with only two variants of which the merge picked one unmodified are not shown.


The option --color[={always,auto,never}] uses color to help distinguish difference regions; additions are in green and deletions in red. The default is never, --color means --color=always, and --color=auto means to use color when standard output is a terminal.

Word Diff

The option --word-diff[={plain,color,none}] shows word-level changes within lines, rather than entire changed lines. For example, this:

- I changed a word.
+ I altered a word.

becomes this:

I [-changed-]{+altered+} a word.

with --word-diff=plain. This is often more useful than line diffs if the content is English prose rather than software code. The color option uses color instead of the markers shown earlier to indicate the additions and deletions, again using green and red. It is possible to change the regular expression Git uses to determine word boundaries with --word-diff-regex; see git-log(1) for details.

Comparing Branches

Often we are interested in understanding the relationship between the content of two branches, particularly in how they have diverged. As discussed in Naming Sets of Commits, a basic tool for this is the symmetric difference A...B, which shows those commits in either branches A or B but not in both (i.e., those commits added to either branch since they last diverged). Sometimes this isn’t enough, though. For example, git cherry-pick creates a new commit based on an existing one, by reapplying the changes introduced by the original commit at a different place in the history. It is useful in situations where incorporating changes by merging is inconvenient or impossible due to repository organization. If a commit has been cherry-picked from one branch to another, then it will be included in their symmetric difference anyway, since they are distinct commits that just happen to represent the same changeset. git log --cherry-pick takes this into account by omitting commits that have identical diffs. Consider the commit graph in Figure 9-1, in which commit 2 was produced with git cherry-pick D on the other branch, and so it and D have the same changeset.

git log and cherry-picking
Figure 9-1. git log and cherry-picking

Assuming all the other commits have distinct changesets, we will see something like:

$ git log master...other
e5feb479 E
070e87e5 D
9b0e3dc5 C
6f70a016 3
0badfe94 2
15f47204 1

Whereas this omits the patch-equivalent commits, showing just content differences:

$ git log --cherry-pick master...other
e5feb479 E
9b0e3dc5 C
6f70a016 3
15f47204 1

The variation --cherry-mark will mark duplicate commits with an equal sign, instead of omitting them:

$ git log --cherry-mark master...other
+ e5feb479 E
= 070e87e5 D
+ 9b0e3dc5 C
+ 6f70a016 3
= 0badfe94 2
+ 15f47204 1

Displaying Sides

git log master..other (with just two dots) shows one side of this situation: those commits on other that are not on master. If you want cherry-pick detection, you have to consider both sides as before, but then you are no longer viewing just one side. You can recover this by adding --{left,right}-only:

$ git log master..other
6f70a016 3
0badfe94 2
15f47204 1

$ git log --cherry-pick --right-only master...other
6f70a016 3
15f47204 1

This shows commits on other that are not contained in master or patch-equivalent to another commit in their difference, in this case, omitting commit 2 since it is equivalent to D. And similar to --cherry-mark, the related option --left-right displays the side of a commit with the symbols < and >:

$ git log --cherry-mark --left-right master...other
< e5feb479 E
< 070e87e5 D
= 9b0e3dc5 C
> 6f70a016 3
= 0badfe94 2
> 15f47204 1

The simple option --cherry is a synonym for --right-only --cherry-mark --no-merges, so that this:

$ git log --cherry HEAD@{upstream}...

shows the commits on your side of the current branch (ignoring possible merges with other branches), marking those that duplicate changes made by distinct commits on the other side (probable cherry-picks, either literally or by another means such as applying patches via email with git format-patch and git am).

Showing Notes

git log --notes[=ref] includes any notes on a commit after its message; see git notes for how this command and Git notes work in general.

Commit Ordering

Normally, git log displays commits in reverse chronological order according to the committer (not author) timestamps. You can alter this in three ways:

  • --date-order shows all children before their parents;
  • --topo-order (for “topological”) implies --date-order, and also groups commits from the same branch together; and
  • --reverse reverses the output list.


The --reverse option does not affect the selection of commits to list, but rather the final order in which they are listed; that is, it is applied last. You might expect the following command to show the root commit, by first listing all commits in reverse chronological order and then showing only the first one:

$ git log --reverse -n 1

Instead, however, it shows the latest commit, by picking the first one from the normal output of git log; the reversal applies to a list containing that single commit, and thus has no effect.

History Simplification

Git has a number of options for pruning sections of history according to various notions of equivalence between parent and child commits, documented in the “History Simplification” section of git-log(1). Since these are fairly specialized and abstruse, useful mostly with very large and complex histories, and well documented in the manpage, we do not cover them here.

git cherry

git cherry [-v] [upstream [head [limit]]]

This command is similar to git log --cherry, but more specialized. It shows commits on a branch that are not in the upstream, marking those whose changes are duplicated by distinct upstream commits with a minus sign (while other commits have a plus sign). Using the same example as before, if we’re on the other branch for which master is upstream:

$ git cherry -v --abbrev
+ 6f70a016 3
- 0badfe94 2
+ 15f47204 1

This shows that we have three new commits on our side, but the changes from commit 2 are already applied upstream (the -v option includes the commit message subject line). As shown, you can give specific current (head) and upstream branches for comparison, and also a limit commit so that only commits in limit..head are eligible to be shown. The limit would be an earlier commit on the head branch, past which you are not interested in looking. The default is equivalent to git cherry HEAD@{upstream} HEAD (no limit).

git shortlog

As mentioned earlier, git shortlog summarizes commit history, grouping commits by author with the number of commits and their subjects, and applying a mailmap if available to rewrite author names or email addresses:

$ git shortlog
Ammon Riley (1):
     Make git-svn branch patterns match complete URL

Amos King (2):
     Do not name "repo" struct "remote" in push_http…
     http-push.c: use a faux remote to pass to http_…

Amos Waterland (6):
     tutorial note about git branch
     Explain what went wrong on update-cache of new …
     Do not create bogus branch from flag to git bra…
     git rebase loses author name/email if given bad…
     AIX compile fix for repo-config.c
     wcwidth redeclaration

This can be useful in preparing the release notes for a new version of a project, automatically collating the new features in this release. You could limit it to just the features since the last version by referring to the tags for the previous and current release (e.g., git shortlog v1.0..v1.1).

The best content for your career. Discover unlimited learning on demand for around $1/day.