Git:在变基期间防止提交

Git: Preventing Commit During a Rebase

我知道如何轻松 'fix' 这种状态,当我在交互式变基过程中不小心做了 git commit --amend 时。但我想知道是否有人有任何复杂程度的解决方案,允许我配置 git/terminal/bash 以防止我能够执行此操作。

我正在使用 Mac OSX 终端。如果解决方案需要它们,我愿意接受其他终端程序。

1 @: master$ git pull
2 @: master$ git checkout my/feature/branch
3 @: my/feature/branch$ git rebase -i master
// There are merge conflicts
// I fix them, then
4 @: my/feature/branch$ git add .
// Then what I should do is:
5correct @: my/feature/branch$ git rebase --continue
// But instead, just due to dumb muscle memory, I do:
5incorrect @: my/feature/branch$ git commit --amend

我可以轻松修复损坏的结果状态。我只是想弄清楚是否有一种方法可以配置一些东西来防止我在 rebase 过程中执行 5incorrect 命令。

您可以将以下本地 pre-commit 挂钩添加为 .git/hooks/pre-commit:

#!/bin/bash
set -e
shopt -s extglob

if [[ -d `git rev-parse --git-path rebase-merge` \
   || -d `git rev-parse --git-path rebase-apply` ]]; then
  read -p 'Rebase in progress, continue? '
  [[ ${REPLY,,} == @(y|yes) ]]
fi

然后只需 chmod +x .git/hooks/pre-commit 即可对您进行排序。如果 rebase 正在进行中,那么系统会提示您继续或按 ctrl-c 或只需输入或对您来说更方便的内容,script/commit 将停止。