Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Git View Commit History

Git View Commit History

Viewing Git commit history can help you understand code changes and development progress.

Git provides a variety of commands and options for viewing commit history, from simple logs to detailed diff comparisons.

Two commands are commonly used for Git commit history:


git log

After committing several updates with Git, or after cloning a project, if you want to review the commit history, you can use the git log command to view it.

git log The command is used to view the commit history records in a Git repository.

git log It displays all commit information from the latest commit to the earliest, including the commit hash, author, commit date, and commit message.

git log The basic syntax of the command:

git log [选项] [分支名/提交哈希]

Common options include:

For the operations in our previous chapter, use the git log command to list the historical commit records as follows:



$ git log

commit d5e9fc2c811e0ca2b2d28506ef7dc14171a207d9 (HEAD -> master)

Merge: c68142b 7774248

Author: runoob 

Date:   Fri May 3 15:55:58 2019 +0800



    Merge branch 'change_site'



commit c68142b562c260c3071754623b08e2657b4c6d5b

Author: runoob 

Date:   Fri May 3 15:52:12 2019 +0800



    修改代码



commit 777424832e714cf65d3be79b50a4717aea51ab69 (change_site)

Author: runoob 

Date:   Fri May 3 15:49:26 2019 +0800



    changed the runoob.php



commit c1501a244676ff55e7cccac1ecac0e18cbf6cb00

Author: runoob 

Date:   Fri May 3 15:35:32 2019 +0800

We can use the --oneline option to view a concise version of the history records.



$ git log --oneline

$ git log --oneline

d5e9fc2 (HEAD -> master) Merge branch 'change_site'

c68142b 修改代码

7774248 (change_site) changed the runoob.php

c1501a2 removed test.txt、add runoob.php

3e92c19 add test.txt

3b58100 第一次版本提交

This tells us the development history of this project.

We can also use the --graph option to see when branches and merges occurred in the history. The following is the same command with the topology graph option enabled:



*   d5e9fc2 (HEAD -> master) Merge branch 'change_site'

|\  

| * 7774248 (change_site) changed the runoob.php

* | c68142b Modify code

|/  

* c1501a2 removed test.txt、add runoob.php

* 3e92c19 add test.txt

* 3b58100 First version submission

Now we can see more clearly when the work forked and when it merged.

You can also use the --reverse parameter to display all logs in reverse.



$ git log --reverse --oneline

3b58100 第一次版本提交

3e92c19 add test.txt

c1501a2 removed test.txt、add runoob.php

7774248 (change_site) changed the runoob.php

c68142b 修改代码

d5e9fc2 (HEAD -> master) Merge branch 'change_site'

If you only want to find the commit log of a specified user, you can use the command: git log --author, for example, suppose we want to find the part committed by Linus in the Git source code:



$ git log --author=Linus --oneline -5

81b50f3 Move 'builtin-*' into a 'builtin/' subdirectory

3bb7256 make "index-pack" a built-in

377d027 make "git pack-redundant" a built-in

b532581 make "git unpack-file" a built-in

112dd51 make "mktag" a built-in

If you want to specify a date, you can use several options: --since and --before, but you can also use --until and --after.

For example, if I want to see all commits in the Git project from three weeks ago and after April 18, I can execute this (I also used the --no-merges option 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 the number of commits displayed:



git log -n 

For example, display the most recent 5 commits:



git log -n 5

Display commits after the specified date:



git log --since="2024-01-01"

Display commits before the specified date:



git log --until="2024-07-01"

Only display commits by a certain author:



git log --author="Author Name"

More git log The command can view http://git-scm.com/docs/git-log or use the git log --help command to view help information.


git blame

git blame The command is used to display line by line who introduced or modified each line of code in a specified file and when.

git blame It can track the change history of each line in a file, including information such as author, commit hash, commit date, and commit message.

To view the modification records of a specified file, you can use the git blame command, in the following format:

git blame [选项] <文件路径>

Common options include:

Display the code annotations and related information for each line of the file:



git blame <文件路径>

Only display code annotations within the specified line number range:



git blame -L <起始行号>,<结束行号> <文件路径>

Trace the origin of renamed or copied code lines:



git blame -C <文件路径>

Trace the origin of moved code lines:



git blame -M <文件路径>

Display line count statistics:



git blame --show-stats <文件路径>

The git blame command displays modification records in list form, as in the following example:

$ git blame README 

^d2097aa (tianqixin 2020-08-25 14:59:25 +0800 1) # Runoob Git 测试

db9315b0 (runoob    2020-08-25 16:00:23 +0800 2) # Runoob Tutorial 

For more content, you can use git blame --help to view the complete help documentation and learn more options and usage.


Recovery and Revert

Git provides multiple ways to recover and revert to previous versions; different commands are suitable for different scenarios and needs.

The following are several common methods:

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 files in the working directory to a certain commit:



git checkout  -- 

For example, restore file.txt to the version 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 in this way, you are in a detached HEAD state.

2. git reset: Reset the current branch to a specific commit

The git reset command can change the commit history of the current branch; it has three main modes: --soft, --mixed, and --hard.

--soft: Only reset HEAD to the specified commit; the staging area and working directory remain unchanged.



git reset --soft 

--mixed (default): Reset HEAD to the specified commit and reset the staging area, but the working directory remains unchanged.



git reset --mixed 

--hard: Reset HEAD to the specified commit; both the staging area and working directory are reset.



git reset --hard 

For example, reset the current branch to commit abc123:



git reset --hard abc123

3. git revert: Undo a commit

The git revert command creates a new commit to undo the specified commit; it does not change the 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 a commit is deleted or reset, it can be recovered through reflog.



git reflog

Using reflog you can find the previous commit hash and thus restore to a specific state. For example:



git reset --hard HEAD@{3}

Example

The following is a comprehensive example demonstrating how to use these commands to restore a historical version:

View the commit history:



git log --oneline

Suppose the output is as follows:



abc1234 Commit 1

def5678 Commit 2

ghi9012 Commit 3

Switch to Commit 2 (in a detached HEAD state):



git checkout def5678

Reset to Commit 2, keeping changes in the staging area:



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 Commit 2:



git revert def5678

View reflog to recover the lost commit:



git reflog

Find the previous commit hash and restore:



git reset --hard HEAD@{3}