代码之家  ›  专栏  ›  技术社区  ›  Devin Rhode

git rebase:让大PR堆栈中的坏名声消失

  •  0
  • Devin Rhode  · 技术社区  · 3 年前

    我有一个名为AccountNode的JSX组件,并意外地创建了一个同样名为AccountNode的TS接口,但两者是不同的。一个用于表(ui)中的行,另一个用于树(节点)中的一段数据。

    如何使“AccountNode”名称不出现在历史记录中的任何位置?

    同样,正如评论中提到的,你可能会有一个坏的字符串常量,一个所谓的“魔术串”。不使用这个神奇的字符串可能很难重写代码,但这个git-rebase配方会有所帮助。

    1 回复  |  直到 3 年前
        1
  •  0
  •   Devin Rhode    3 年前

    例如,我不想硬编码字符串 "load_more" 所以我要重做/调整这些提交:

    git rebase --exec 'git difflastcommitadded | ag load_more && { echo "found load_more"; git redo; } || echo "ok"' --no-reschedule-failed-exec -i 315abbd5b

    设置:

    1. 安装 ag ( brew install the_silver_searcher )(grep/ack替代方案)
    2. 添加此数字 difflastcommitadded 别名:
    git config --global --edit
    brew install the_silver_searcher
    

    粘贴:

    [alias]
      # From: https://stackoverflow.com/questions/73981158/git-rebase-make-bad-name-in-large-pr-stack-disappear/73981159#73981159
      difflastcommitadded = !git diff HEAD^..HEAD --no-ext-diff --unified=0 --exit-code -a --no-prefix | egrep '^\\+'
    

    或者,如果你想从回购中的文件夹中完全废除变量名,请继续阅读这个不太灵活的例子:

    git rebase --exec 'ag -0 -l AccountNode ui/app && git redo || echo "ok"' -i 315abbd5b
    

    这将重新设置基准,但只要出现引入的提交,就会停止 AccountNode 在ui/app文件夹中,撤消提交,并将这些更改弹出到您的工作目录中,这样您就可以在ide中修复它。

    一旦修复了的实例 AccountNode 您可以使用以下脚本进行良好的安全检查:

    ag -0 -l AccountNode ui/app/pages/AccountManagement2 && echo "still contains AccountNode" || {
      yarn --cwd ui run jest -i && git add . && git commit && git rebase --continue
    }
    

    在我的例子中, git commit 运行更漂亮的+esint,但不运行测试(现在在ci中运行的测试)

    这取决于 git redo 此处提到的别名: git rebase - pop each commit into working dir to easily edit, re-use commit msg

    推荐文章