Course Tonight

Course Tonight

Did You Know?

You can create any type of product documentation with Docy

Chapter 2: Browsing the history

Estimated reading: 9 minutes 9 views
  1. -q, --quiet: This parameter suppresses the diff output and makes the command run quietly without displaying excessive information.
  2. --source: This parameter shows the source of the commit, providing information about where the commit originated from.
  3. --use-mailmap: When this parameter is used, Git will apply a mail map file to change the user information for the committing user. A mail map file is used to map different email addresses to a single name and email combination.
  4. --decorate[=...]: This parameter enables the decoration options for displaying additional information about commits. The exact options that follow --decorate can vary, but they typically include information such as branch or tag names associated with each commit.
  5. --L <n,m:file>: This parameter is used to show the log for a specific range of lines in a file. The range is specified as n,m, where n is the starting line number and m is the ending line number. It also displays the diff for the specified range.
  6. --show-signature: When this parameter is used, Git will display the signatures of signed commits. This can be useful for verifying the authenticity and integrity of commits.
  7. -i, --regexp-ignore-case: This parameter allows the regular expression limiting patterns to be matched without regard to letter case. In other words, it performs a case-insensitive match when using regular expressions to specify patterns.

Section 2.1: “Regular” Git Log

<code class="language-git">git log

The following command will display all your commits with the author and hash. This will be shown over multiple lines per commit. (If you wish to show a single line per commit, look at onelineing). Use the q key to exit the log.

By default, with no arguments, git log lists the commits made in that repository in reverse chronological order – that is, the most recent commits show up first. As you can see, this command lists each commit with its SHA-1 checksum, the author’s name and email, the date written, and the commit message. – source

Example (from Free Code Camp repository):

commit 87ef97f59e2a2f4dc425982f76f14a57d0900bcf
Merge: e50ff0d eb8b729
Author: Brian
Date: Thu Mar 24 15:52:07 2016 -0700

Merge pull request #7724 from BKinahan/fix/where-art-thou

Fix ‘its’ typo in Where Art Thou description

commit eb8b7298d516ea20a4aadb9797c7b6fd5af27ea5
Author: BKinahan
Date: Thu Mar 24 21:11:36 2016 +0000

Fix ‘its’ typo in Where Art Thou description

commit e50ff0d249705f41f55cd435f317dcfd02590ee7
Merge: 6b01875 2652d04
Author: Mrugesh Mohapatra
Date: Thu Mar 24 14:26:04 2016 +0530

Merge pull request #7718 from deathsythe47/fix/unnecessary-comma

Remove unnecessary comma from CONTRIBUTING.md

If you wish to limit your command to the last n commits log, you can simply pass a parameter. For example, if you wish to list the last 2 commits logs.

Section 2.2: Prettier log

To see the log in a prettier graph-like structure, use the following command:

<code class="language-git">git log --decorate --oneline --graph

Sample output:

<code class="language-git">* e0c1cea (HEAD -> maint, tag: v2.9.3, origin/maint) Git 2.9.3
* 9b601ea Merge branch 'jk/difftool-in-subdir' into maint
|\
| * 32b8c58 difftool: use Git::* functions instead of passing around state
| * 98f917e difftool: avoid $GIT_DIR and $GIT_WORK_TREE
| * 9ec26e7 difftool: fix argument handling in subdirs
* | f4fd627 Merge branch 'jk/reset-ident-time-per-commit' into maint
...

To assign an alias for this command, you can use the following:

<code class="language-git">git config --global alias.lol "log --decorate --oneline --graph"

To use the alias version:

  • To see the history of the current branch:
git lol
  • To see the combined history of the active branch (HEAD), develop, and origin/master branches:
git lol HEAD develop origin/master
  • To see the combined history of everything in your repo:
git lol --all

Section 2.3: Colorize Logs

To colorize logs, you can use the following command:

git log --graph --pretty=format:'%C(red)%h%Creset -%C(yellow)%d%Creset %s %C(green)(%cr) %C(yellow)<%an>%Creset'

The format option allows you to specify your own log output format. Here are the parameters and their details:

  • %C(color_name): This option colors the output that comes after it.
  • %h or %H: This abbreviates the commit hash (use %H for the complete hash).
  • %Creset: This resets the color to the default terminal color.
  • %d: This represents ref names.
  • %s: This represents the subject (commit message).
  • %cr: This represents the committer date relative to the current date.
  • %an: This represents the author name.

Section 2.4: Oneline log

To display the log in a single line format, you can use the following command:

git log --oneline

To show all of your commits with only the first part of the hash and the commit message, you can use the --oneline flag with the git log command:

The oneline option prints each commit on a single line, which is useful if you’re looking at a lot of commits. – source

Example (from Free Code Camp repository):

87ef97f Merge pull request #7724 from BKinahan/fix/where-art-thou
eb8b729 Fix 'its' typo in Where Art Thou description
e50ff0d Merge pull request #7718 from deathsythe47/fix/unnecessary-comma
2652d04 Remove unnecessary comma from CONTRIBUTING.md
6b01875 Merge pull request #7667 from zerkms/patch-1
766f088 Fixed assignment operator terminology
d1e2468 Merge pull request #7690 from BKinahan/fix/unsubscribe-crash
bed9de2 Merge pull request #7657 from Rafase282/fix/

If you wish to limit your command to the last n commits log, you can pass a parameter. For example, to list the last 2 commit logs, you can use:

git log -2 --oneline

Section 2.6: List all contributions grouped by author name

You can use the git shortlog command to list all contributions grouped by author name. If no parameters are given, a list of all commits made per committer will be shown in chronological order:

git shortlog

To simply see the number of commits and suppress the commit description, you can use the -s or --summary option:

git shortlog -s

To sort the output by the number of commits instead of alphabetically by committer name, you can use the -n or --numbered option:

git shortlog -n

To add the email of a committer, you can use the -e or --email option:

git shortlog -e

You can also provide a custom format option using the --format option to display information other than the commit subject. This format can be any string accepted by the --format option of git log. Refer to the “Colorizing Logs” section above for more information on custom formatting.

Section 2.7: Searching commit string in git log

To search for a specific string in the git log, you can use the --grep option:

git log [options] --grep "search_string"

For example:

git log --all --grep "removed file"

This command will search for the string “removed file” in all logs across all branches.

Starting from git 2.4+, the search can be inverted using the --invert-grep option. For example:

git log --grep="add file" --invert-grep

This command will show all commits that do not contain the string “add file”.

Section 2.8: Log for a range of lines within a file

To view the log for a range of lines within a specific file, you can use the -L option with the line range and file name:

git log -L 1,20:index.html

This command will display the log for lines 1 to 20 in the file index.html.

Section 2.9: Filter logs

You can filter logs based on various criteria. For example, you can use the --after option to show logs after a specific date:

git log --after '3 days ago'

You can also use specific dates:

git log --after 2016-05-01

Similarly, you can use the --before or --until options to filter logs by date.

To filter logs by author, you can use the --author option:

git log --author=author

Section 2.10: Log with changes inline

To see the log with changes inline, you can use the -p or --patch options:

git log --patch

This command will show the log with the changes inline.

Section 2.11: Log showing committed files

To view the log showing the committed files, you can use the --stat option:

git log --stat

This command will display the commit information along with the files that were changed in each commit.

Section 2.12: Show the contents of a single commit

To view the contents of a single commit, you can use the git show command followed by the commit hash:

git show 48c83b3

For example:

git show 48c83b3690dfc7b0e622fd220f8f37c26a77c934

This command will display the commit information, including the author, date, and commit message. It will also show the file differences introduced in that commit.

Section 2.13: Git Log Between Two Branches

To see the commits that are on a specific branch but not on another branch, you can use the following command:

git log master..foo

This command will show the commits that are on the branch foo but not on the branch master. It is useful for seeing the commits you have added since branching.

Section 2.14: One line showing committer name and time since commit

To display a concise one-line format of the commit information, including the committer name and the time since the commit, you can use the git log command with the appropriate format options:

git log --oneline --decorate --source --pretty=format:'"%Cblue %h %Cgreen %ar %Cblue %an %C(yellow) %d %Creset %s"' --all --graph

For example:

* 40554ac 3 months ago Alexander Zolotov Merge pull request #95 from gmandnepr/external_plugins
|\
| * e509f61 3 months ago Ievgen Degtiarenko Documenting new property
| * 46d4cb6 3 months ago Ievgen Degtiarenko Running idea with external plugins
| * 6253da4 3 months ago Ievgen Degtiarenko Resolve external plugin classes
| * 9fdb4e7 3 months ago Ievgen Degtiarenko Keep original artifact name as this may be important for intellij
| * 22e82e4 3 months ago Ievgen Degtiarenko Declaring external plugin in intellij section
|/
* bc3d2cb 3 months ago Alexander Zolotov Ignore DTD in plugin.xml

This command will show a concise representation of the commits, including the commit hash, relative time since the commit, committer name, branch information, and commit message. The --all --graph options add a graphical representation of the commit history.

Leave a Comment

Share this Doc

Chapter 2: Browsing the history

Or copy link

CONTENTS