Thanks to visit codestin.com
Credit goes to www.tutorialspoint.com

Git Log



The command git log displays commit logs in reverse chronological order by default. This command is useful as it helps in tracking and understanding the history of a project, who made a change and when.

Usage

1. View Commit History − The command git log displays commit logs in reverse chronological order, which means, the latest commits are listed first.

Each commit entry contains:

  • A unique identifier for the commit Commit hash.

  • Author of the commit.

  • Date and time of the commit.

  • The commit message.

2. Filter Commits − Commit history can be filtered using different options, such as:

  • --author=<name> − Commits that are done by the given specific author only, is shown.

  • --since or --until − Commits made before or after a certain date, is shown.

3. Compact Output − Often the default output of git log is long and in detail, but their are various ways to make them compact.

  • --oneline − Each commit is shown in a single line with commit hash and the commit message.

  • --graph − Shows the detailed and graphical representation of the commit history. This is useful in visualizing the branches and merges.

4. Show Commit Differences − The difference (diffs) for each commit can also be seen, using the option --patch / -p

5. Number of Commits Limited − Number of commits can be limited using the -n option. A number can be specified for the value n.

6. Search Commits by Messages − Commits can be searched using the --grep option. This searches for commits that contain specific words or phrases in the commit messages.

7. View Commits for a Specific File − Commit history for a specific file can also be viewed. You just need to specify the <file-path> at the end of the git log command.

Syntax

The command git log has the following syntax:

git log

It displays commits that may be accessed through parent links from the given commit(s), but not from commits that are prefixed with the symbol ^.

git log feature-branch master ^bugfix-branch

The command means list all the commits which are reachable from feature-branch or master, but not from bugfix-branch.

By removing any commits that are accessible from bugfix-branch, it effectively displays the commits that are unique to feature-branch and master.

In the Git language, <commit1>..<commit2> is a shortcut for ^<commit1> <commit2>.

It means that commits that may be accessed through <commit2> but not <commit1> will be included in the list.

These commands, for example, are interchangeable:

git log feature-branch..master

git log master ^feature-branch

List commits reachable from master but not from feature-branch is what both commands really mean.

Git computes the symmetric difference between two sets of commits, thus this format <commit1>..<commit2> is especially helpful for merges.

It is similar to employing a more direct set difference operation using this notation.

It can be used, for instance, to identify differences between two divergent branches.

git log branch1 branch2 --not $(git merge-base --all branch1 branch2)
git log branch1...branch2

The symmetric difference between commits accessible from branches 1 and 2 is produced by both commands.

Using parameters from the git-rev-list and git-diff commands, this command allows options that modify what is shown and how changes introduced by each commit are displayed.

Options

The following options are available with git log:

Formatting Options

Following options control how commit log will be displayed:

1. --oneline − Each commit is displayed in a single line, containing the commit hash and message.

git log --oneline

2. --pretty=<format> − Output format can be chosen, which may include, oneline, short, medium, full, fuller, and format:<custom>.

There are some placeholders for custom format, which are as follows:

  • %H − Commit hash

  • %h − Abbreviated commit hash

  • %an − Author name

  • %ae − Author email

  • %ar − Author date, relative

  • %s − Commit message

git log --pretty=short
git log --pretty=format:"%h - %an, %ar : %s"

3. --graph − An ASCII graph of branch and merge history can be seen.

git log --graph

4. --decorate[=short|full|auto|no] − Reference names can be seen next to commit hashes, using this option.

It defaults to auto based on terminal output, determines how ref names like refs/heads/, refs/tags/, and refs/remotes/ are shown alongside commit information.

git log --decorate

5. --no-decorate − Reference names can not be seen next to commit hashes, using this option.

git log --no-decorate

6. --stat − Summary of changes introduced by each commit.

git log --stat

Filtering Options

Following options can be used to filter the commit log:

1. --author=<pattern> − Commits that are done by the given author only, is shown.

git log --author="John Doe"

2. --grp=<pattern> − Searches commit messages for a particular pattern.

git log --grep="fixed"

3. --since=<date> and --until=<date> − Commits that are most recent or older than the specified date, can be shown.

git log --since="2015-01-01" --until="2015-01-02"

4. --after=<date> and --before=<date> − Similar to the previuos one, but the syntax is used differently.

git log --after="2015-01-01" --before="2015-01-05"

5. --all − Commit history of all the branches can be seen.

git log --all

Limiting Options

Following options can be used to limit the commit log:

1. -n <n> − Number of commits can be limited using the -n option. A number can be specified for the value n.

git log -n=5

2. -p − The difference (diffs) for each commit can also be seen, using the option --patch / -p.

git log -p

3. --max-count=<number> − This is another way to limit the number of commits.

git log --max-count=5

4. --skip=<number> − Skips the specified number of commits.

git log --skip=5

5. --reverse − Reverses the order of the commits.

git log --reverse

Miscellaneous Options

Some other miscellaneous options are as follows:

1. --follow − Even if a file is renamed, the --follow option in git log keeps track of its history, but it is only useful for following a single file.

git log --follow

2. --decorate-refs=<pattern> − Decorate references by matching them with the given pattern.

git log --decorate-refs="refs/heads/*"

3. --decorate-refs-exclude=<pattern> − Exclude references by matching them with the given pattern.

git log --decorate-refs-exclude="refs/heads/*"

4. --name-only − Only show the names of the files that are changed in each commit.

git log --name-only

5. --name-status − Only show the status of the files that are changed in each commit, such as added, modified, or deleted.

git log --name-status

6. -S<string> − Searches for such commits where the given string was either added or removed.

git log -S"fixed"

7. --clear-decorations − The --clear-decorations option in the Git log command expands decoration to include all references by default and resets any prior --decorate-refs or --decorate-refs-exclude settings.

git log --clear-decorations

8. --source − The ref name used on the command line to reach each commit is printed by the --source option in the git log command.

git log --source

Combination of Options

The above mentioned options can be combined with git log command, such as git log --all --name-only, git log --oneline --max-count=5, etc.

The options given above allow you to customize git log to suit your specific needs, whether you are searching for specific changes, analyzing the history of a branch, or tracking down the issues and fixes.

Advertisements