git-revisions-syntax

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Git Revision Syntax

Git 修订版本引用语法

Single Commits

单个提交

SyntaxMeaning
abc123
SHA prefix (≥4, unambiguous)
HEAD
/
@
current commit
HEAD^
first parent
HEAD^2
second parent (merge's other side)
HEAD~N
walk N steps along first parent
HEAD~3^2
chained: grandparent's merge sibling
HEAD@{2}
2 reflog entries ago
HEAD@{yesterday}
/
@{2.weeks.ago}
reflog by time
@{-1}
previously checked-out branch
@{u}
/
@{push}
upstream / push target
tag^{}
deref annotated tag to commit
:/fix typo
newest commit whose msg matches regex
^N
picks parent N of one commit (merge-aware).
~N
walks N first-parent steps. Different.
语法含义
abc123
SHA前缀(≥4位,无歧义)
HEAD
/
@
当前提交
HEAD^
第一个父提交
HEAD^2
第二个父提交(合并的另一侧)
HEAD~N
沿第一个父提交回溯N步
HEAD~3^2
链式引用:祖父提交的合并兄弟提交
HEAD@{2}
回溯2条引用日志记录
HEAD@{yesterday}
/
@{2.weeks.ago}
按时间筛选引用日志
@{-1}
之前检出的分支
@{u}
/
@{push}
上游分支 / 推送目标
tag^{}
将带注释的标签解引用到提交
:/fix typo
提交消息匹配正则表达式的最新提交
^N
选择一个提交的第N个父提交(支持合并场景)。
~N
沿第一个父提交回溯N步。二者有所不同。

Ranges

范围选择

SyntaxMeaning
A..B
in B, not A
A...B
symmetric diff (either but not both)
^A B
/
B --not A
same as
A..B
A B ^C
in A or B, not C
bash
git log origin/main..HEAD              # unpushed
git log --left-right main...feature    # side-marked
git log HEAD --not release/*
语法含义
A..B
属于B但不属于A的提交
A...B
对称差集(仅属于A或仅属于B的提交)
^A B
/
B --not A
A..B
含义相同
A B ^C
属于A或B但不属于C的提交
bash
git log origin/main..HEAD              # 查看未推送的提交
git log --left-right main...feature    # 查看分支间对称差异并标记所属分支
git log HEAD --not release/*           # 查看HEAD提交,排除所有release/*分支的提交

Content-Based Selectors

基于内容的选择器

bash
git log -S"token"                  # commits where literal token count changed
git log -S"pat" --pickaxe-regex
git log -G"regex"                  # commits touching any matching line
git log -L :funcName:file.js       # evolution of a function
git log -L 10,20:file.js           # evolution of line range
-S
= added/removed detection;
-G
= any line touching pattern.
bash
git log -S"token"                  # 查找字面量token出现次数变化的提交
git log -S"pat" --pickaxe-regex   # 使用正则表达式匹配pickaxe模式
git log -G"regex"                  # 查找触及任何匹配正则表达式行的提交
git log -L :funcName:file.js       # 查看函数funcName的演变历史
git log -L 10,20:file.js           # 查看文件第10-20行的演变历史
-S
= 检测内容的添加/移除;
-G
= 检测任何触及匹配模式的行。

Plumbing

底层命令

bash
git rev-parse HEAD~3               # resolve to full SHA
git merge-base A B                 # nearest common ancestor
git name-rev <sha>                 # which branch/tag contains it
bash
git rev-parse HEAD~3               # 解析为完整SHA值
git merge-base A B                 # 查找最近的共同祖先
git name-rev <sha>                 # 查找包含该SHA的分支/标签

Pitfalls

注意事项

  • Reflog selectors (
    @{N}
    ,
    @{time}
    ) are local, expire (~90 days).
  • HEAD^
    in zsh/PowerShell needs quoting.
  • A..B
    is empty if B is an ancestor of A — probably want
    B..A
    .
  • 引用日志选择器(
    @{N}
    @{time}
    )是本地的,会过期(约90天)。
  • 在zsh/PowerShell中使用
    HEAD^
    需要加引号。
  • 如果B是A的祖先,
    A..B
    的结果为空——你可能需要
    B..A