Git Commit History π
2026-06-14 | π Git
Viewing Git commit history helps you understand code changes and development progress.
Git provides multiple commands and options for viewing commit history, ranging from simple logs to detailed diff comparisons.
Two commonly used commands for viewing Git commit history are:
* **git log** β View historical commit records.
* **git blame ** β View the historical modification records of a specified file in list form.
* * *
## git log
After making several updates with Gitβor after cloning a projectβyou may want to review the commit history. You can use the `git log` command for this purpose.
The **git log** command is used to view commit history records in a Git repository.
**git log** displays all commit information from the most recent commit to the earliest one, including the commit hash, author, commit date, and commit message.
Basic syntax of the **git log** command:
`git log [branch-name/commit-hash]`
Commonly used options include:
* `-p`: Show patches (specific changes) for each commit.
* `--oneline`: Display commit information in a concise single-line format.
* `--graph`: Display branch and merge history in graphical form.
* `--decorate`: Show branches and tags pointing to commits.
* `--author=`: Show only commits by a specific author.
* `--since=`: Show only commits after a specified time.
* `--until=`: Show only commits before a specified time.
* `--grep=`: Show only commits whose messages contain the specified pattern.
* `--no-merges`: Do not show merge commits.
* `--stat`: Show brief statistics, including modified files and line counts.
* `--abbrev-commit`: Use abbreviated commit hashes.
* `--pretty=`: Use a custom format for displaying commit information.
Based on the operations from our previous chapter, using the `git log` command to list historical commit records yields the following output:
$ git log commit d5e9fc2c811e0ca2b2d28506ef7dc14171a207d9 (HEAD -> master)Merge: c68142b 7774248Author: Date: Fri May 3 15:55:58 2019 +0800 Merge branch 'change_site' commit c68142b562c260c3071754623b08e2657b4c6d5b Author: Date: Fri May 3 15:52:12 2019 +0800 Modify Code commit 777424832e714cf65d3be79b50a4717aea51ab69 (change_site)Author: Date: Fri May 3 15:49:26 2019 +0800 changed the .php commit c1501a244676ff55e7cccac1ecac0e18cbf6cb00 Author: Date: Fri May 3 15:35:32 2019 +0800
We can use the `--oneline` option to view a concise version of the history.
$ git log --oneline $ git log --oneline d5e9fc2 (HEAD -> master) Merge branch 'change_site' c68142b Modify Code7774248 (change_site) changed the .php c1501a2 removed test.txtγadd .php 3e92c19 add test.txt 3b58100 First Version Commit
This tells us about the development history of this project.
We can also use the `--graph` option to see when branches and merges occurred in the history. Below is the same command with the topology graph option enabled:
* d5e9fc2 (HEAD -> master) Merge branch 'change_site'| | * 7774248 (change_site) changed the .php * | c68142b Modify Code|/ * c1501a2 removed test.txtγadd .php * 3e92c19 add test.txt * 3b58100 First Version Commit
Now we can more clearly see when work diverged and when it was merged back together.
You can also use the **--reverse** parameter to display all logs in reverse order.
$ git log --reverse --oneline 3b58100 First Version Commit3e92c19 add test.txt c1501a2 removed test.txtγadd .php 7774248 (change_site) changed the .php c68142b Modify Code d5e9fc2 (HEAD -> master) Merge branch 'change_site'
To search for commit logs by a specific user, use the command: `git log --author`, for example, suppose we want to find Linusβs commits in the Git source code:
$ git log --author=Linus --oneline -581b50f3 Move 'builtin-*' into a 'builtin/' subdirectory 3bb7256 make "index-pack" a built-in377d027 make "git pack-redundant" a built-in b532581 make "git unpack-file" a built-in112dd51 make "mktag" a built-in
If you need to specify dates, you can use several options: `--since` and `--before`, or alternatively `--until` and `--after`.
For example, if I want to view all commits in the Git project from three weeks ago until after April 18th, 2010, I could execute the following (Iβve also used `--no-merges` to hide merge commits):
$ git log --oneline --before={3.weeks.ago} --after={2010-04-18} --no-merges 5469e2d Git 1.7.1-rc2 d43427d Documentation/remote-helpers: Fix typos and improve language 272a36b Fixup: Second argument may be any arbitrary string b6c8d2d Documentation/remote-helpers: Add invocation section 5ce4f4e Documentation/urls: Rewrite to accomodate transport::address 00b84e9 Documentation/remote-helpers: Rewrite description 03aa87e Documentation: Describe other situations where -z affects git diff 77bc694 rebase-interactive: silence warning when no commits rewritten 636db2c t3301: add tests to use --format="%N"
### Common Options
Limit number of commits displayed:
`git log -n `
For example, show the most recent 5 commits:
`git log -n 5`
Show commits since a specified date:
`git log --since="2024-01-01"`
Show commits before a specified date:
`git log --until="2024-07-01"`
Show commits by a specific author:
`git log --author="Author Name"`
For more **git log** commands, refer to [http://git-scm.com/docs/git-log](http://git-scm.com/docs/git-log) or use the `git log --help` command to view help information.
* * *
## git blame
The **git blame** command displays, line-by-line, who introduced or modified each line of code in a specified file and when.
**git blame** tracks the change history of every line in a file, including author, commit hash, commit date, and commit message.
To view the modification history of a specified file, use the `git blame` command, in the following format:
`git blame `
Commonly used options include:
* `-L ,`: Show annotations only for lines within the specified range.
* `-C`: Trace lines that were copied or renamed.
* `-M`: Trace lines that were moved.
* `-C -C` or `-M -M`: Perform deeper tracing for lines with substantial modifications.
* `--show-stats`: Show statistics showing how many lines each author contributed.
Display annotations and related information for each line in a file:
`git blame `
Display annotations only for a specified line range:
`git blame -L , `
Trace lines that were copied or renamed:
`git blame -C `
Trace lines that were moved:
`git blame -M `
Display line-count statistics:
`git blame --show-stats `
The `git blame` command displays modification records in list form, as shown in the following example:
$ git blame README ^d2097aa (tianqixin 2020-08-25 14:59:25 +0800 1) # Git test db9315b0 ( 2020-08-25 16:00:23 +0800 2) #
For more details, use `git blame --help` to view the complete help documentation and learn about additional options and usage methods.
* * *
## Recovery and Reverting
Git provides multiple ways to recover and revert to previous versions; different commands suit different scenarios and requirements.
Below are several common methods:
* **`git checkout`**: Switch branches or restore files to a specified commit.
* **`git reset`**: Reset the current branch to a specified commit (soft reset, mixed reset, hard reset).
* **`git revert`**: Create a new commit to undo a specified commit, without altering commit history.
* **`git reflog`**: View historical operation records to recover lost commits.
### 1. `git checkout`: Check out a specific version of a file
The `git checkout` command is used to switch branches or restore files in the working directory to a specified commit.
Restore a file in the working directory to a specific commit:
`git checkout -- `
For example, restore `file.txt` to its state at commit `abc123`:
`git checkout abc123 -- file.txt`
Switch to a specific commit:
`git checkout `
For example:
`git checkout abc123`
When switching to a specific commit this way, you enter a detached HEAD state.
### 2. `git reset`: Reset the current branch to a specific commit
The `git reset` command modifies the commit history of the current branch. It has three main modes: `--soft`, `--mixed`, and `--hard`.
`--soft`: Only resets HEAD to the specified commit; the staging area and working directory remain unchanged.
`git reset --soft `
`--mixed` (default): Resets HEAD to the specified commit; resets the staging area but leaves the working directory unchanged.
`git reset --mixed `
`--hard`: Resets HEAD to the specified commit; resets both the staging area and the working directory.
`git reset --hard `
For example, reset the current branch to commit `abc123`:
`git reset --hard abc123`
### 3. `git revert`: Undo a specific commit
The `git revert` command creates a new commit to undo a specified commit. It does not alter commit history and is suitable for commits already pushed to a remote repository.
`git revert `
For example, undo commit `abc123`:
`git revert abc123`
### 4. `git reflog`: View historical operation records
The `git reflog` command records all movements of HEAD. Even if commits are deleted or reset, they can still be recovered via reflog.
`git reflog`
Using reflog, you can locate prior commit hashes and thus restore to a specific state. For example:
`git reset --hard HEAD@{3}`
### Example
Below is a comprehensive example demonstrating how to use these commands to recover a historical version:
View commit history:
`git log --oneline`
Suppose the output is:
abc1234 Commit 1 def5678 Commit 2 ghi9012 Commit 3
Switch to Commit 2 (entering detached HEAD state):
`git checkout def5678`
Reset to Commit 2, keeping changes staged:
`git reset --soft def5678`
Reset to Commit 2, unstaging changes:
`git reset --mixed def5678`
Reset to Commit 2, discarding all changes:
`git reset --hard def5678`
Undo Comm