使用bash命令修改当前分支中的每个提交。

huangapple go评论57阅读模式
英文:

Modify each commit in current branch by bash command

问题

  • 有一个包含三个提交的分支
  • 在我做一些变基和修改后,忘记运行Rubocop的“linter”
  • CI构建没有成功
  • 现在我想要修复过去

我可以使用编辑命令为每个提交进行分支变基并手动完成,但这样做有什么乐趣呢🤷🏾而且如果我遇到一个包含20多个提交的分支,我绝对不想手动修复它。

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

想法 / 我尝试过的

想法1

我知道在变基中有一个exec命令,但我可能需要在我的命令中包含git-add和git-commit

 $ git rebase --exec "rubocop --autocorrect && git commit --amend" <父分支>
...
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

如果可以不使用它,请告诉我。

想法2

我有一个很久以前的脚本,我不知怎么就把它拼在一起了。这个脚本通过git filter-branch --env-filter来重置提交的作者。所以_filter-branch_似乎是我正在寻找的东西。

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

这个命令会执行完毕,但不幸的是会保留历史记录。而且在手册的任何地方以及当我运行命令时,都会说filter-branch是一个危险的命令,不应该被使用 :/

英文:
  • have branch with three commits
  • forgot to run Rubocop "linter" after I did some rebasing-amending
  • CI build didn't go well
  • now I want to fix the past

I could rebase the branch with edit command for each commit and do it all manually, but where is the adventure in that 🤷🏾 Also if I'd run into a branch like that, with 20+ commits, I definitely don't want to be fixing it manually.

What is the right way to modify existing commits by shell command?

Ideas / What I've tried

Idea 1

I know there is exec command in rebase, but I'd probably have to include the git-add and git-commit in my command

 $ 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

If it is possible without it, pls let me know.

Idea 2

I have a script from long time ago, which I overflow-pasted together somehow. This script is reseting the author of commits. It does so via git filter-branch --env-filter, so filter-branch seems like what I am looking for.

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

This command finishes, but unfortunately leaves the history untouched. Also everywhere in the man page and when I run the command, it says filter-branch is a dangerous command and nobody should be using it :/

答案1

得分: 1

我认为使用 git rebase 是最简单的选项。类似这样的操作应该有效:

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

git add -u 会暂存所有已修改的文件。

英文:

I think using git rebase is the simplest option. Something like this should work:

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

git add -u will stage all modified files.

huangapple
  • 本文由 发表于 2023年7月7日 01:04:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/76631087.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定