当前位置: 首页 > news >正文

成为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
http://www.lryc.cn/news/409561.html

相关文章:

  • Win10出现错误代码0x80004005 一键修复指南
  • C++ 基础(类和对象下)
  • java RestClientBuilder es 集群 鉴权
  • 【OpenCV】中saturate_cast<uchar>的含义和用法是什么?
  • 【数据结构】哈希表二叉搜索树详解
  • 【SpringBoot】参数传递之@ModelAttribute
  • frp搭建ssh内网穿透
  • OpenCV库学习之cv2.normalize函数
  • LINUX操作系统安全
  • vue3.0学习笔记(三)——计算属性、监听器、ref属性、组件通信
  • Elasticsearch面试三道题
  • 大厂面经:大疆嵌入式面试题及参考答案(4万字长文:持续更新)
  • 数据结构【有头双向链表】
  • docker 安装jenkins详细步骤教程
  • C++模板函数
  • c#中的正则表达式和日期的使用(超全)
  • 论文阅读【检测】:商汤 ICLR2021 | Deformable DETR
  • dpdk发送udp报文
  • 网站后端管理和构建java项目的工具-Maven
  • 深入理解计算机系统 CSAPP 家庭作业11.10
  • Unity3D 二进制序列化器详解
  • js_拳皇(上)
  • TCP请求如何获取客户端真实源IP地址
  • 【b站-湖科大教书匠】6 应用层 - 计算机网络微课堂
  • QT串口和数据库通信
  • WebKitWebKit简介及工作流程
  • 架构分析(CPU:ARM vs RISC-V)
  • 使用 Docker Compose 部署 RabbitMQ 的一些经验与踩坑记录
  • 前端八股速通(持续更新中...)
  • 【语音识别和生成】语音识别和语音合成技术