修复

工程开发 社区

技能解读

当前原文的自动摘译,尚未人工审校。

gfix 的工作原理

来源:~/code/dotfiles/plugins/git.zsh(gfix函数)gfix [-m <message>] <commit-sha>执行以下操作: 防止在正在进行的 git 操作期间运行(rebase、合并、cherry-pick 等) 存储任何未暂存的更改(保留索引) 从当前暂存的任何内容创建修复提交:

何时使用 gfix

当您需要修改非 HEAD 提交的提交时,请使用 gfix。例如: 评论评论要求更改 3 次提交前引入的代码 您在处理其他事情时注意到早期提交中的拼写错误或错误 您希望保持干净、逻辑的提交历史记录,其中每个提交都是独立的 如果您只需要修改最近的提交,请改用 git commit --amend。

步骤

确定安全范围 - 目标提交必须位于当前分支自己的提交内,而不是在基本分支上或之前。使用detect_base_branch找到基础分支。作为最后的手段,请询问用户。目标提交必须出现在git log --oneline <base-branch>..HEAD中。如果没有,请停止并告诉用户 - 重写共享历史记录会导致问题。确定目标提交——使用$ARGUMENTS了解用户想要修复的内容。使用 git log --oneline <base-branch>..HEAD 列出安全提交,并检查它们的差异以查找引入用户想要更改的代码的提交。

流狐档案 运行与权限由流狐静态检查推断,不代表作者声明或实际测试结果。
领域
工程开发
作者声明的智能体
未找到明确声明;不据此推断已兼容或已测试
静态检查
94 / 100 · 启发式扫描,不代表运行安全
运行环境证据
未执行;当前没有隔离运行测试证据
作者 / 版本 / 许可证
@ryanb · MIT
上下文用量评级
低消耗
接入复杂程度
需简单配置
是否需要外部接口密钥
不需要
兼容的系统
未声明(默认跨平台)
运行环境要求
未检出明确要求
检测到的文件与系统行为
  • 只读
  • 允许写入 / 修改
  • 命令执行
网络行为
仅限本地
安装命令数
无(仅作为资料)

需要注意: 未声明工具权限范围;实际权限由运行环境决定。

作者说明文件(原文) SKILL.md

# gfix — Amend a Past Commit

Use the `gfix` shell command to fold staged changes into a commit that is not the most recent. This is the preferred way to amend historical commits in this repo.

## How gfix works

Source: `~/code/dotfiles/plugins/git.zsh` (the `gfix` function)

`gfix [-m <message>] <commit-sha>` does the following:

1. Guards against running during an in-progress git operation (rebase, merge, cherry-pick, etc.)
2. Stashes any unstaged changes (preserving the index)
3. Creates a fixup commit from whatever is currently staged:
   - With no `-m`: `git commit --fixup <commit-sha>` (content-only fixup)
   - With `-m <message>`: creates a manual `amend!` commit with subject `amend! <original-subject>` and body `<message>`, which autosquash then folds into the target (replacing its message, and including any staged changes; `--allow-empty` is used if nothing is staged)
4. Runs `GIT_SEQUENCE_EDITOR=true git rebase -i --autosquash <commit-sha>^` to automatically squash the fixup into the target commit
5. Restores any stashed unstaged changes

## When to use gfix

Use gfix when you need to amend a commit that is **not** the HEAD commit. For example:

- A review comment asks for a change to code introduced 3 commits ago
- You notice a typo or bug in an earlier commit while working on something else
- You want to keep a clean, logical commit history where each commit is self-contained

If you only need to amend the most recent commit, use `git commit --amend` instead.

## Steps

1. **Determine the safe range** — The target commit must be within the current branch's own commits, not on or before the base branch. Use `detect_base_branch` to find the base branch. As a last resort, ask the user.

   The target commit **must** appear in `git log --oneline <base-branch>..HEAD`. If it does not, **stop and tell the user** — rewriting shared history would cause problems.

2. **Identify the target commit** — Use `$ARGUMENTS` to understand what the user wants fixed. List the safe commits with `git log --oneline <base-branch>..HEAD` and examine their diffs to find the commit that introduced the code the user wants changed.

3. **Make and stage the changes** — Edit the files, then `git add` only the files that belong to the target commit.

4. **Run gfix** — Pass the target commit SHA. Add `-m <message>` to also rewrite the target commit's message:

   ```bash
   gfix <commit-sha>
   gfix -m "new commit message" <commit-sha>
   ```

5. **Handle rebase conflicts** — If the rebase hits conflicts:
   - Read the conflicting files and resolve them
   - `git add` the resolved files
   - `git rebase --continue`
   - Repeat until the rebase finishes

6. **Verify** — Run `git log --oneline -10` to confirm the history looks correct. If relevant tests exist, run them.

讨论

基于 GitHub Discussions。登录 GitHub 即可参与讨论、点赞、订阅更新。