欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 房产 > 家装 > 成为git砖家(8): 使用 git log 查询范围内的 commit

成为git砖家(8): 使用 git log 查询范围内的 commit

2024/10/24 5:17:09 来源:https://blog.csdn.net/baiyu33/article/details/140783064  浏览:    关键词:成为git砖家(8): 使用 git log 查询范围内的 commit

文章目录

    • 1. 查询 git log 的文档
    • 2. 不带任何参数: `git log` 啥意思?
    • 3. `git log` 最主要功能是什么?
    • 4. `git log <commit1>..<commit2>` 什么意思
    • 5. 查看最近n次commit
    • 6. References

1. 查询 git log 的文档

git help log --web

在这里插入图片描述
市面上针对 git logoptions 讲解的文章很多,对于 <revision-range> 讲解的很少,白鱼在这篇带你游览 git log <revision-range> 的用法。

2. 不带任何参数: git log 啥意思?

前一节知道了 git 完整用法

git log [<options>] [<revision-range>] [[--] <path>]

因此,当不带任何参数:

git log

意味着:

  • <options> 为空
  • <revision-range> 为默认范围
  • <path> 没有指定

那么默认的 <revision-range> 是什么?答案是 HEAD:
在这里插入图片描述
也就是说:git log 等价于 git log HEAD. HEAD的含义我们先前有提过,它表示“当前分支”, 或者说当前分支的最新节点。因此 git log 也就是 git log HEAD 意思是:从当前分支最新节点 HEAD往前回溯,所有能被回溯的节点,都列出来。懂了吗?

git log 等价于 git log HEAD, 意思是列出当前分支所有 commit 节点

3. git log 最主要功能是什么?

我们来看 git log 的 description 第一段的解释:

Shows the commit logs.

List commits that are reachable by following the parent links from the given commit(s), but exclude commits that are reachable from the one(s) given with a ^ in front of them. The output is given in reverse chronological order by default.

You can think of this as a set operation. Commits reachable from any of the commits given on the command line form a set, and then commits reachable from any of the ones given with ^ in front are subtracted from that set. The remaining commits are what comes out in the command’s output. Various other options and paths parameters can be used to further limit the result.

说的很直白了:git log 列出给定的 commit 的所有可回溯的节点,并且略掉 ^ 开头的节点及其所有可回溯节点。

因此:

git log foo bar ^baz

意思是列出 foo 的所有可回溯节点、bar 的所有可回溯节点、 但是会去除 baz 及其所有可回溯节点。

换言之, git log 命令最主要的功能,是根据给出的若干 commit, 每个commit对应一个集合,执行集合的合并以及减法操作,最终得到一个 commit 集合

如果还不明白,再解释一下 git log foo bar ^baz:

  • foo: 得到以 foo 为末尾节点的 commit 集合 foo-set
  • bar: 得到以 bar 为末尾节点的 commit 集合 bar-set
  • ^baz: 得到以 baz 为末尾节点的 commit 集合 baz-set
  • 最终计算: foo-set + bar-set - baz-set

4. git log <commit1>..<commit2> 什么意思

A special notation “…” can be used as a short-hand for “^ ”. For example, either of the following may be used interchangeably:

$ git log origin..HEAD
$ git log HEAD ^origin

也就是说, git log <commit1>..<commit2>git log ^<commit1> <commit2> 的简写, 意思是从 commit2 回溯得到的 commit 集合, 减去 commit1 及其回溯得到的集合。

实际上 <commit1><commit2> 也可以换成分支的名字, 例如 git log main..feature 表示的是, feature 分支特有的、 main 分支没有的 commit:

在这里插入图片描述
再举一个极端例子:如果只想查看某个 git repo 的第一个commit: 首先用 gitk 或 git log 等任意工具查询得到第一个 commit 的 sha-1, 然后执行

git log <first-commit-sha>

gitk log <first-commit-sha>

从而只查看第一个commit

在这里插入图片描述
在这里插入图片描述

5. 查看最近n次commit

很多人的直觉是:

git log -n

但其实基于上一节的结论,很容易想到另一种表示:

git log HEAD ^HEAD~n

6. References

  • https://git-scm.com/docs/git-log
  • https://www.atlassian.com/git/tutorials/git-log

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com