此页面记录了 TheFuck 中的 Git 特定修正规则。这些规则旨在检测和修复常见的 Git 命令错误,通过自动建议和应用修正来使您的 Git 工作流程更顺畅。有关通用规则系统架构的信息,请参阅 规则系统。
TheFuck 包含用于处理 Git 命令错误的专用规则。这些规则识别常见的 Git 错误模式并生成适当的修正。Git 特定规则使用一个通用装饰器(git_support),以确保它们仅在 Git 命令上激活。
来源: thefuck/rules/git_push.py6-7 thefuck/rules/git_checkout.py9-10 thefuck/specific/git.py
TheFuck 通过单独的规则模块实现 Git 特定错误修正。每个 Git 规则都遵循一个标准模式:
这两个函数都使用 `@git_support` 装饰器,以确保它们仅在 Git 命令上运行。
来源: thefuck/rules/git_push.py6-9 thefuck/rules/git_push.py21-22
TheFuck 包含多项 Git 特定规则,用于处理常见的错误场景:
当当前分支未设置上游分支时,修正 Git push 命令。
错误模式: 在未先设置上游分支的情况下 push 分支。
修正: 向命令添加 `--set-upstream origin <branch>` 标志。
示例
$ git push
fatal: The current branch master has no upstream branch.
To push the current branch and set the remote as upstream, use
git push --set-upstream origin master
$ fuck
git push --set-upstream origin master
来源: thefuck/rules/git_push.py6-9 thefuck/rules/git_push.py21-44
当分支名称拼写错误或不存在时,为 Git checkout 错误提供修正。
错误模式: 尝试 checkout 一个不存在的分支。
修正
来源: thefuck/rules/git_checkout.py9-12 thefuck/rules/git_checkout.py30-49
修正 Git 命令的拼写错误。
错误模式: 使用与有效命令相似但不存在的 Git 命令。
修正: 建议最相似的 Git 命令。
示例
$ git brnch
git: 'brnch' is not a git command. See 'git --help'.
The most similar command is
branch
$ fuck
git branch
来源: thefuck/rules/git_not_command.py6-10 thefuck/rules/git_not_command.py13-18
当一个文件被引用但尚未被跟踪时,自动将其添加到 Git 索引。
错误模式: 当尝试操作一个未被 Git 跟踪的文件时。
修正: 先使用 `git add` 添加文件,然后运行原始命令。
来源: thefuck/rules/git_add.py17-20 thefuck/rules/git_add.py23-27
当 Git 操作因未提交的更改而被阻止时,暂存更改。
错误模式: 当 Git 提示“请提交或暂存它们”。
修正: 运行 `git stash`,然后是原始命令。
来源: thefuck/rules/git_stash.py5-9 thefuck/rules/git_stash.py12-15
在需要时为 pull 命令设置上游分支。
错误模式: 在未设置上游分支的情况下 pull。
修正: 设置上游分支,然后 pull。
来源: thefuck/rules/git_pull.py5-7 thefuck/rules/git_pull.py10-16
修正错误的 `git branch list` 命令。
错误模式: 使用 `git branch list`(这是其他 VCS 用户常见的错误)。
修正: 运行 `git branch --delete list`,然后是 `git branch`。
来源: thefuck/rules/git_branch_list.py5-9 thefuck/rules/git_branch_list.py12-14
每个 Git 规则都有特定的匹配逻辑,用于确定何时应用该规则。
来源: thefuck/rules/git_push.py thefuck/rules/git_checkout.py thefuck/rules/git_not_command.py thefuck/rules/git_add.py thefuck/rules/git_stash.py thefuck/rules/git_pull.py thefuck/rules/git_branch_list.py
| 原始命令 | 错误 | 更正后的命令 | 规则 |
|---|---|---|---|
git push | 无上游分支 | git push --set-upstream origin master | git_push |
git checkout feature | 分支不存在 | git checkout develop (相似分支) | git_checkout |
git checkout feature | 分支不存在 | git checkout -b feature (新分支) | git_checkout |
git brnch | 命令未找到 | git branch | git_not_command |
git status newfile.txt | 文件未跟踪 | git add -- newfile.txt && git status newfile.txt | git_add |
git checkout master | 未提交的更改 | git stash && git checkout master | git_stash |
git pull | 无上游 | git branch --set-upstream-to=origin/master master && git pull | git_pull |
git branch list | 命令错误 | git branch --delete list && git branch | git_branch_list |
来源: tests/rules/test_git_push.py49-75 tests/rules/test_git_not_command.py49-57
git_support 装饰器有两个主要作用:
Git 特定规则使用多种技术来匹配命令:
例如,git_push.py 检查命令部分是否包含“push”,并且输出是否包含特定的错误消息。
来源: thefuck/rules/git_push.py6-9
Git 特定规则使用几种方法来生成更正:
例如,git_push.py 解析 Git 错误消息以提取建议的参数。
来源: thefuck/rules/git_push.py21-44
有关创建自定义 Git 规则的信息,请参阅添加新规则。Git 特定规则应
match 和 get_new_command 函数使用 @git_support 装饰器。