【发布时间】:2011-03-07 10:11:52
【问题描述】:
我刚刚阅读了amending a single file in a past commit in git,但不幸的是,接受的解决方案“重新排序”了提交,这不是我想要的。所以这是我的问题:
在处理(不相关的)功能时,我时不时会注意到我的代码中存在错误。快速的git blame 然后显示该错误已在几次提交前被引入(我提交了很多,所以通常不是最近的提交引入了该错误)。此时,我通常会这样做:
git stash # temporarily put my work aside
git rebase -i <bad_commit>~1 # rebase one step before the bad commit
# mark broken commit for editing
vim <affected_sources> # fix the bug
git add <affected_sources> # stage fixes
git commit -C <bad_commit> # commit fixes using same log message as before
git rebase --continue # base all later changes onto this
但是,这种情况经常发生,以至于上面的顺序变得很烦人。特别是“交互式变基”很无聊。上述序列是否有任何捷径,可以让我通过分阶段更改修改过去的任意提交?我非常清楚这会改变历史,但我经常犯错误,以至于我真的很想拥有类似的东西
vim <affected_sources> # fix bug
git add -p <affected_sources> # Mark my 'fixup' hungs for staging
git fixup <bad_commit> # amend the specified commit with staged changes,
# rebase any successors of bad commit on rewritten
# commit.
也许是一个可以使用管道工具重写提交的智能脚本?
【问题讨论】:
-
“重新排序”提交是什么意思?如果您要更改历史记录,那么自更改的提交以来的所有提交必须 不同,但对链接问题的接受答案不会以任何有意义的方式重新排序提交。
-
@Charles:我的意思是重新排序:如果我注意到 HEAD~5 是损坏的提交,则链接问题中接受的答案将使 HEAD(分支的尖端)成为固定的犯罪。但是,我希望 HEAD~5 成为固定提交 - 这是您在使用交互式变基并编辑单个提交进行修复时得到的。
-
是的,但是 rebase 命令将重新签出 master 并将所有后续提交 rebase 到固定提交。这不是你驾驶
rebase -i的方式吗? -
其实这个答案有潜在的问题,我觉得应该是
rebase --onto tmp bad-commit master。如所写,它将尝试将错误提交应用于固定提交状态。 -
这是另一个自动化修复/变基过程的工具:stackoverflow.com/a/24656286/1058622