git-hunk

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

/git-hunk - split changes into focused commits

/git-hunk - 将改动拆分为聚焦的提交

Requires:
uv tool install git-hunk
(or
pip install git-hunk
)
依赖要求:
uv tool install git-hunk
(或
pip install git-hunk

Workflow

工作流程

  1. git-hunk list
    — see all hunks (file, id, +/- stats). No diffs.
  2. git-hunk show <id> [<id>...]
    or
    git-hunk show --all
    when headers aren't clear enough.
  3. Plan commits before staging. For each planned commit, list the hunk IDs (and
    -l
    line ranges for partial hunks). A single hunk may need to be split across commits. Ask the user if grouping is ambiguous.
  4. Stage and commit each group:
    bash
    git-hunk stage <id1> <id2> ...
    git commit -m "<type>: <description>"
  5. git-hunk list
    again to check nothing got left behind.
  1. git-hunk list
    —— 查看所有hunk(文件、ID、增减量统计),不展示diff内容。
  2. 当标题信息不够清晰时,使用
    git-hunk show <id> [<id>...]
    git-hunk show --all
  3. 暂存前先规划提交内容。针对每个规划的提交,列出对应的hunk ID(部分hunk可使用
    -l
    指定行范围)。单个hunk可能需要拆分到多个提交中,如果分组存在歧义请询问用户。
  4. 暂存并提交每个分组:
    bash
    git-hunk stage <id1> <id2> ...
    git commit -m "<type>: <description>"
  5. 再次执行
    git-hunk list
    检查是否有遗漏的改动。

Partial hunks

部分hunk操作

Line selection (
-l
) works with
stage
,
unstage
, and
discard
(requires single id):
  • Include lines:
    git-hunk stage <id> -l 3,5-7
  • Exclude lines:
    git-hunk stage <id> -l ^3,^5-7
行选择参数(
-l
)可用于
stage
unstage
discard
命令(需要指定单个ID):
  • 包含指定行:
    git-hunk stage <id> -l 3,5-7
  • 排除指定行:
    git-hunk stage <id> -l ^3,^5-7

Fixing mistakes

错误修正

  • git-hunk unstage <id1> <id2> ...
    — move staged hunks back to working tree.
  • git-hunk unstage <id> -l 3,5-7
    — partially unstage specific lines.
  • git-hunk discard <id1> <id2> ...
    — permanently discard unstaged hunks (restore from HEAD).
  • git-hunk discard <id> -l 3,5-7
    — partially discard specific lines.
  • git-hunk unstage <id1> <id2> ...
    —— 将已暂存的hunk移回工作区。
  • git-hunk unstage <id> -l 3,5-7
    —— 部分取消暂存指定行的改动。
  • git-hunk discard <id1> <id2> ...
    —— 永久丢弃未暂存的hunk(从HEAD恢复内容)。
  • git-hunk discard <id> -l 3,5-7
    —— 部分丢弃指定行的改动。

Example
git-hunk list
output

git-hunk list
输出示例

unstaged:
labelme/app.py
  c43213b  @@ -78,6 +78,7 @@ _AI_CREATE_MODES  +1
  4da0d77  @@ -1364,6 +1365,19 @@ class MainWindow  +13
labelme/translate/de_DE.qm
  7a3befc  Binary file
unstaged:
labelme/app.py
  c43213b  @@ -78,6 +78,7 @@ _AI_CREATE_MODES  +1
  4da0d77  @@ -1364,6 +1365,19 @@ class MainWindow  +13
labelme/translate/de_DE.qm
  7a3befc  Binary file

Notes

注意事项

  • IDs are content-based hashes, stable across partial staging, and support prefix matching
  • git-hunk list [<file>...]
    — filter hunks by file path
  • --staged
    /
    --unstaged
    to filter
    list
    and
    show
    (both search staged+unstaged by default)
  • --json
    exists but plain output is usually enough
  • One logical change per commit, conventional commit messages
  • ID是基于内容生成的哈希值,在部分暂存操作中保持稳定,支持前缀匹配
  • git-hunk list [<file>...]
    —— 按文件路径过滤hunk
  • 可使用
    --staged
    /
    --unstaged
    过滤
    list
    show
    命令的结果(默认同时搜索暂存和未暂存内容)
  • 支持
    --json
    参数,但普通文本输出通常已满足需求
  • 每个提交对应一项逻辑改动,使用约定式提交信息