Git:Log
lastmod 2021-04-20created 2014-02-2714 edits
git-log: Show commit logs.
Frequently used Git Log command options
-
git log -- path1 path2 - Uses a directory or file name to search the log for changes to that file. This option takes path names with
--and is written at the end of the command.
-
git log --stat - Prints the basic LOG output + the list of modified files. Shows statistics (number of modified lines) for the files modified in each commit.
-
git log --name-only - Shows only the list of modified files from the commit information. Somewhere between the basic LOG output and
git log --stat.
-
git log --name-status - Shows not only the list of modified files but also whether each file was added, modified, or deleted.
-
git log --shortstat - Shows only the modified files, added lines, and deleted lines from the output of the
git log --statcommand.
-
git log --pretty=oneline - Shows each Commit on one line. Prints only the Commit number and the Commit log.
-
git log --author-date-order - Sorts by author timestamp instead of commit timestamp.
- Show no parents before all of its children are shown, but otherwise show commits in the author timestamp order.
-
git log --oneline - Shows each Commit on one line. The difference from
git log --pretty=onelineis that it prints the Commit SHA1 in abbreviated form (7 characters). - Same as using the
git log --pretty=oneline --abbrev-commitoptions together.
-
git log -p - Shows the patch applied in each commit. Prints the diff output showing which files were modified and how.
-
git log --pretty=? - Shows output in the specified format. The values for this option are oneline, short, full, fuller, and format.
- Note that format is used when you want to print in a custom format.
-
git log --graph - This command prints an ASCII graph showing the Branch and Merge history.
- Works even better together with the
onelineandformatoptions.
-
git log --word-diff - Shows the diff output word by word.
-
git log --abbrev-commit - Shows only the first few characters of the 40-character SHA-1 checksum instead of all of it.
-
git log --relative-date - Shows dates in a relative format like
2 weeks agoinstead of the exact time.
-
git log --decorate - Adds ASCII decorations such as HEAD, Origin, and Branch information.
-
git log --all - Prints all logs (including Branches).
-
git log [REMOTE]/[BRANCH] - Lets you view the log of a remote repository. To view the Master Branch of the default remote repository (Origin), enter
git log origin/master.
List of Pretty Format Option
Useful options for --pretty=format are listed below.
| Option | Description of Output |
| %H | Commit hash |
| %h | Abbreviated commit hash |
| %T | Tree hash |
| %t | Abbreviated tree hash |
| %P | Parent hashes |
| %p | Abbreviated parent hashes |
| %an | Author name |
| %ae | Author e-mail |
| %ad | Author date (format respects the --date= option) |
| %ar | Author date, relative |
| %cn | Committer name |
| %ce | Committer email |
| %cd | Committer date |
| %cr | Committer date, relative |
| %s | Subject |
Query restrictions
We have looked at options related to the output format, but the git log command also has options that limit the query range. They query only part of the history instead of all of it.
| Option | Description of Output |
| | Queries only the most recent n commits. |
| | Searches only commits after the specified date. |
| | Queries only commits before the specified date. |
| | Shows only commits by the given author. |
| | Shows only commits by the given committer. |
Example
Short output
To print a short output limited to the last 4 (including Branches):
Entering the above prints the following.
* 177a8fc (HEAD) Addin RenderScript v8 JAR.
| * f3eae1b (origin/master, origin/HEAD, master) Remove RenderScript.
|/
* 0dd2f1e Update Balrocon2 logo icon.
* 55c63c5 Logging class refactoring.
Searching for removed files
Search the entire history for removed files and restore them.
After searching with a tool like grep, find the SHA and restore as follows.