shell 通过bash命令修改当前分支中的每个提交

ih99xse1  于 2023-08-07  发布在  Shell
关注(0)|答案(1)|浏览(109)
  • 有三个提交的分支
  • 我做了一些rebasing-amendment后,忘记运行Rubocop“linter”了
  • CI构建不顺利
  • 现在我想弥补过去

我可以为每个提交使用edit命令来重定分支的基,然后手动完成所有操作,但是其中的冒险在哪里呢?如果我遇到这样一个分支,有20多个提交,我肯定不想手动修复它。

通过shell命令修改现有提交的正确方法是什么?

想法/我尝试过的

思路一

我知道在rebase中有 exec 命令,但是我可能需要在命令中包含git-add和git-commit

$ git rebase --exec "rubocop --autocorrect && git commit --amend" <parent branch>
...
error: cannot rebase: You have unstaged changes.
warning: execution succeeded: rubocop --autocorrect && git commit --amend
but left changes to the index and/or the working tree.
Commit or stash your changes, and then run

  git rebase --continue

字符串
如果没有它是可能的,请让我知道。

思路二

我有一个很久以前的剧本,不知怎么的,我把它贴在一起了。此脚本正在重置提交的作者。它是通过git filter-branch --env-filter来实现的,所以 filter-branch 看起来就是我要找的。

$ git filter-branch --commit-filter 'rubocop --autocorrect ' HEAD~3..HEAD


这个命令完成了,但不幸的是没有改变历史记录。在手册页的所有地方,当我运行该命令时,它都说filter-branch是一个危险的命令,任何人都不应该使用它:

p1tboqfb

p1tboqfb1#

我认为使用git rebase是最简单的选择。类似这样的东西应该可以工作:

git rebase --exec "rubocop --autocorrect; git add -u; git commit --amend --no-edit" ...

字符串
git add -u将暂存所有已修改的文件。

相关问题