在 Git 中,查看提交历史是了解项目变更和演进过程的重要手段

Git Basics - Viewing the Commit History

在 Git 中,查看提交历史是了解项目变更和演进过程的重要手段。以下是一些关于如何查看提交历史的基本知识和方法:

一、使用 git log 命令

git log 是 Git 中最常用的命令之一,用于显示仓库的提交历史。默认情况下,它会按照时间顺序列出所有的提交记录,最新的提交记录在最上面。

基本用法

git log

执行该命令后,你将看到每个提交的以下信息:

  • SHA-1 哈希值:提交的唯一标识符。
  • 作者:提交者的姓名和邮箱地址。
  • 日期:提交的日期和时间。
  • 提交信息:提交时输入的简短描述。

常用选项

  • --oneline:以一行格式显示提交信息,只显示提交的哈希值和提交消息。

    git log --oneline
    
  • -n:限制显示的提交记录数量。例如,显示最近的 5 条提交记录:

    git log -n 5
    
  • -p--patch:显示每次提交的差异(diff),即具体更改的内容。

    git log -p
    
  • --stat:显示每次提交的文件修改统计信息,包括新增、删除和修改的行数。

    git log --stat
    
  • --author=<作者>:只显示指定作者的提交历史。

    git log --author="John Doe"
    
  • --since=<日期>--until=<日期>:只显示指定日期范围内的提交历史。日期格式可以是具体的日期(如 2023-01-01),也可以是相对时间(如 1 week ago)。

    git log --since="2023-01-01" --until="2023-01-31"
    
  • --grep=<模式>:只显示提交信息中包含指定模式的提交记录。

    git log --grep="bug fix"
    
  • --graph:以图形化的方式显示提交历史,展示分支和合并情况。

    git log --graph
    

二、使用图形化工具

除了命令行工具,Git 还提供了图形化的工具来查看提交历史,如 gitkgitg。这些工具提供了更直观、交互式的界面,方便用户浏览提交记录。

gitk

  • 用法

    gitk
    
  • 说明

    • gitk 是 Git 自带的图形化界面工具。
    • 执行该命令后,将打开一个窗口,显示提交历史的树形结构。
    • 用户可以通过鼠标点击和拖动来导航和查看历史记录,还可以使用搜索功能过滤特定的提交。

gitg

  • 安装(以 Ubuntu 为例)

    sudo apt-get install gitg
    
  • 用法

    gitg
    
  • 说明

    • gitg 是一个开源的 Git 图形界面工具,提供了更丰富的功能和交互界面。
    • 执行该命令后,将打开一个窗口,显示提交历史的图形展示。
    • 用户可以通过点击提交节点来查看详细信息,使用搜索功能查找特定的提交记录。

三、在线代码托管平台

如果你使用的是在线代码托管平台(如 GitHub、GitLab 或 Bitbucket),通常也可以在项目页面上找到提交历史。这些平台提供了直观的界面,方便用户查看提交历史、文件差异和代码审阅等信息。

四、其他命令

除了 git log,Git 还提供了其他命令来查看提交历史或相关信息,如 git showgit blame

  • git show <提交哈希>:查看指定提交的详细信息和更改内容。

    git show abc1234
    
  • git blame <文件路径>:显示指定文件每一行的最后一次修改信息,包括提交者和提交时间。

    git blame main.c
    

总结

查看提交历史是 Git 中非常重要的功能,通过 git log 命令和图形化工具,你可以方便地了解项目的演进和变更情况。根据实际需求选择合适的工具和方法,可以帮助你更有效地管理和跟踪代码变更。
After you have created several commits, or if you have cloned a repository with an existing commit history, you’ll probably want to look back to see what has happened. The most basic and powerful tool to do this is the git log command.

These examples use a very simple project called “simplegit”. To get the project, run

$ git clone https://github.com/schacon/simplegit-progit

When you run git log in this project, you should get output that looks something like this:

$ git log
commit ca82a6dff817ec66f44342007202690a93763949
Author: Scott Chacon schacon@gee-mail.com
Date: Mon Mar 17 21:52:11 2008 -0700

Change version number

commit 085bb3bcb608e1e8451d4b2432f8ecbe6306e7e7
Author: Scott Chacon schacon@gee-mail.com
Date: Sat Mar 15 16:40:33 2008 -0700

Remove unnecessary test

commit a11bef06a3f659402fe7563abf99ad00de2209e6
Author: Scott Chacon schacon@gee-mail.com
Date: Sat Mar 15 10:31:28 2008 -0700

Initial commit

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.

A huge number and variety of options to the git log command are available to show you exactly what you’re looking for. Here, we’ll show you some of the most popular.

One of the more helpful options is -p or --patch, which shows the difference (the patch output) introduced in each commit. You can also limit the number of log entries displayed, such as using -2 to show only the last two entries.

$ git log -p -2
commit ca82a6dff817ec66f44342007202690a93763949
Author: Scott Chacon schacon@gee-mail.com
Date: Mon Mar 17 21:52:11 2008 -0700

Change version number

diff --git a/Rakefile b/Rakefile
index a874b73…8f94139 100644
— a/Rakefile
+++ b/Rakefile
@@ -5,7 +5,7 @@ require ‘rake/gempackagetask’
spec = Gem::Specification.new do |s|
s.platform = Gem::Platform::RUBY
s.name = “simplegit”

  • s.version = “0.1.0”
  • s.version = “0.1.1”
    s.author = “Scott Chacon”
    s.email = “schacon@gee-mail.com”
    s.summary = “A simple gem for using Git in Ruby code.”

commit 085bb3bcb608e1e8451d4b2432f8ecbe6306e7e7
Author: Scott Chacon schacon@gee-mail.com
Date: Sat Mar 15 16:40:33 2008 -0700

Remove unnecessary test

diff --git a/lib/simplegit.rb b/lib/simplegit.rb
index a0a60ae…47c6340 100644
— a/lib/simplegit.rb
+++ b/lib/simplegit.rb
@@ -18,8 +18,3 @@ class SimpleGit
end

end

-if $0 == FILE

  • git = SimpleGit.new
  • puts git.show
    -end

This option displays the same information but with a diff directly following each entry. This is very helpful for code review or to quickly browse what happened during a series of commits that a collaborator has added. You can also use a series of summarizing options with git log. For example, if you want to see some abbreviated stats for each commit, you can use the --stat option:

$ git log --stat
commit ca82a6dff817ec66f44342007202690a93763949
Author: Scott Chacon schacon@gee-mail.com
Date: Mon Mar 17 21:52:11 2008 -0700

Change version number

Rakefile | 2 ±
1 file changed, 1 insertion(+), 1 deletion(-)

commit 085bb3bcb608e1e8451d4b2432f8ecbe6306e7e7
Author: Scott Chacon schacon@gee-mail.com
Date: Sat Mar 15 16:40:33 2008 -0700

Remove unnecessary test

lib/simplegit.rb | 5 -----
1 file changed, 5 deletions(-)

commit a11bef06a3f659402fe7563abf99ad00de2209e6
Author: Scott Chacon schacon@gee-mail.com
Date: Sat Mar 15 10:31:28 2008 -0700

Initial commit

README | 6 ++++++
Rakefile | 23 +++++++++++++++++++++++
lib/simplegit.rb | 25 +++++++++++++++++++++++++
3 files changed, 54 insertions(+)

As you can see, the --stat option prints below each commit entry a list of modified files, how many files were changed, and how many lines in those files were added and removed. It also puts a summary of the information at the end.

Another really useful option is --pretty. This option changes the log output to formats other than the default. A few prebuilt options are available for you to use. The oneline option prints each commit on a single line, which is useful if you’re looking at a lot of commits. In addition, the short, full, and fuller options show the output in roughly the same format but with less or more information, respectively:

$ git log --pretty=oneline
ca82a6dff817ec66f44342007202690a93763949 Change version number
085bb3bcb608e1e8451d4b2432f8ecbe6306e7e7 Remove unnecessary test
a11bef06a3f659402fe7563abf99ad00de2209e6 Initial commit

The most interesting option is format, which allows you to specify your own log output format. This is especially useful when you’re generating output for machine parsing — because you specify the format explicitly, you know it won’t change with updates to Git:

$ git log --pretty=format:“%h - %an, %ar : %s”
ca82a6d - Scott Chacon, 6 years ago : Change version number
085bb3b - Scott Chacon, 6 years ago : Remove unnecessary test
a11bef0 - Scott Chacon, 6 years ago : Initial commit

Useful options for git log --pretty=format lists some of the more useful options that format takes.
Table 1. Useful options for git log --pretty=format 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 email

%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

You may be wondering what the difference is between author and committer. The author is the person who originally wrote the work, whereas the committer is the person who last applied the work. So, if you send in a patch to a project and one of the core members applies the patch, both of you get credit — you as the author, and the core member as the committer. We’ll cover this distinction a bit more in Distributed Git.

The oneline and format options are particularly useful with another log option called --graph. This option adds a nice little ASCII graph showing your branch and merge history:

$ git log --pretty=format:“%h %s” --graph

  • 2d3acf9 Ignore errors from SIGCHLD on trap
  • 5e3ee11 Merge branch ‘master’ of git://github.com/dustin/grit
    |
    | * 420eac9 Add method for getting the current branch
  • | 30e367c Timeout code and tests
  • | 5a09431 Add timeout protection to grit
  • | e1193f8 Support for heads with slashes in them
    |/
  • d6016bc Require time for xmlschema
  • 11d191e Merge branch ‘defunkt’ into local

This type of output will become more interesting as we go through branching and merging in the next chapter.

Those are only some simple output-formatting options to git log — there are many more. Common options to git log lists the options we’ve covered so far, as well as some other common formatting options that may be useful, along with how they change the output of the log command.
Table 2. Common options to git log Option Description

-p

Show the patch introduced with each commit.

–stat

Show statistics for files modified in each commit.

–shortstat

Display only the changed/insertions/deletions line from the --stat command.

–name-only

Show the list of files modified after the commit information.

–name-status

Show the list of files affected with added/modified/deleted information as well.

–abbrev-commit

Show only the first few characters of the SHA-1 checksum instead of all 40.

–relative-date

Display the date in a relative format (for example, “2 weeks ago”) instead of using the full date format.

–graph

Display an ASCII graph of the branch and merge history beside the log output.

–pretty

Show commits in an alternate format. Options include oneline, short, full, fuller, and format (where you specify your own format).

–oneline

Shorthand for --pretty=oneline --abbrev-commit used together.
Limiting Log Output

In addition to output-formatting options, git log takes a number of useful limiting options; that is, options that let you show only a subset of commits. You’ve seen one such option already — the -2 option, which displays only the last two commits. In fact, you can do -, where n is any integer to show the last n commits. In reality, you’re unlikely to use that often, because Git by default pipes all output through a pager so you see only one page of log output at a time.

However, the time-limiting options such as --since and --until are very useful. For example, this command gets the list of commits made in the last two weeks:

$ git log --since=2.weeks

This command works with lots of formats — you can specify a specific date like “2008-01-15”, or a relative date such as “2 years 1 day 3 minutes ago”.

You can also filter the list to commits that match some search criteria. The --author option allows you to filter on a specific author, and the --grep option lets you search for keywords in the commit messages.
Note

You can specify more than one instance of both the --author and --grep search criteria, which will limit the commit output to commits that match any of the --author patterns and any of the --grep patterns; however, adding the --all-match option further limits the output to just those commits that match all --grep patterns.

Another really helpful filter is the -S option (colloquially referred to as Git’s “pickaxe” option), which takes a string and shows only those commits that changed the number of occurrences of that string. For instance, if you wanted to find the last commit that added or removed a reference to a specific function, you could call:

$ git log -S function_name

The last really useful option to pass to git log as a filter is a path. If you specify a directory or file name, you can limit the log output to commits that introduced a change to those files. This is always the last option and is generally preceded by double dashes (–) to separate the paths from the options.

In Options to limit the output of git log we’ll list these and a few other common options for your reference.
Table 3. Options to limit the output of git log Option Description

-

Show only the last n commits

–since, --after

Limit the commits to those made after the specified date.

–until, --before

Limit the commits to those made before the specified date.

–author

Only show commits in which the author entry matches the specified string.

–committer

Only show commits in which the committer entry matches the specified string.

–grep

Only show commits with a commit message containing the string

-S

Only show commits adding or removing code matching the string

For example, if you want to see which commits modifying test files in the Git source code history were committed by Junio Hamano in the month of October 2008 and are not merge commits, you can run something like this:

$ git log --pretty=“%h - %s” --author=‘Junio C Hamano’ --since=“2008-10-01”
–before=“2008-11-01” --no-merges – t/
5610e3b - Fix testcase failure when extended attributes are in use
acd3b9e - Enhance hold_lock_file_for_{update,append}() API
f563754 - demonstrate breakage of detached checkout with symbolic link HEAD
d1a43f2 - reset --hard/read-tree --reset -u: remove unmerged new paths
51a94af - Fix “checkout --track -b newbranch” on detached HEAD
b0ad11e - pull: allow “git pull origin s o m e t h i n g : something: something:current_branch” into an unborn branch

Of the nearly 40,000 commits in the Git source code history, this command shows the 6 that match those criteria.
Tip

Preventing the display of merge commits

Depending on the workflow used in your repository, it’s possible that a sizable percentage of the commits in your log history are just merge commits, which typically aren’t very informative. To prevent the display of merge commits cluttering up your log history, simply add the log option --no-merges.

Git --distributed-even-if-your-workflow-isnt

About
Documentation
    Reference
    Book
    Videos
    External Links
Downloads
Community

This book is available in English.

Full translation available in
български език,
Deutsch,
Español,
Français,
Ελληνικά,
日本語,
한국어,
Nederlands,
Русский,
Slovenščina,
Tagalog,
Українська
简体中文,

Partial translations available in
Čeština,
Македонски,
Polski,
Српски,
Ўзбекча,
繁體中文,

Translations started for
azərbaycan dili,
Беларуская,
فارسی,
Indonesian,
Italiano,
Bahasa Melayu,
Português (Brasil),
Português (Portugal),
Svenska,
Türkçe.

The source of this book is hosted on GitHub.
Patches, suggestions and comments are welcome.
Chapters ▾ 2nd Edition
2.3 Git Basics - Viewing the Commit History
Viewing the Commit History

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Bol5261

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值