简体   繁体   English

Git将修正提交提交到原始提交

[英]Git distribute fixup commit to original commits

Having the following trivial commits 具有以下琐碎的提交

# define function in one file
echo "function foo () {}" > a
git add a
git commit -m "1st commit"

# call the function in different file
echo "foo()" > b
git add b
git commit -m "2nd commit"

# rename foo to bar in both files
echo "function bar () {}" > a
echo "bar()" > b
git commit -am "fixup"

is it possible to automatically split the fixup commit to update both previous commits? 是否可以自动拆分修订提交以更新两个先前的提交?

I'm thinking that if the fixup commit only changes lines that were changed in the original commits, it should indeed be possible to precisely algorithmically split the fixup commit. 我在想,如果修订提交仅更改原始提交中已更改的行,则确实应该可以通过算法精确地拆分修订提交。 Is that the case? 是这样吗 If so, does git support it? 如果是这样,git是否支持它?

I know this can be done with multiple rebases and manually splitting the fixup commit, but that is not what I'm after. 我知道可以使用多个基准库并手动拆分修订提交来完成,但这不是我要的。

Clarification : Imagine working on a feature branch. 澄清 :想象一下在功能分支上工作。 You have numerous commits. 您有很多提交。 Upon code review it was found that for example method foo should rather be named method bar . 通过代码审查,发现例如方法foo应该更名为方法bar Now before pushing to the upstream I'd rather not just change all instances of it add a new commit, but split the last commit and apply it's parts to the appropriate prior commits. 现在,在推向上游之前,我不只是更改其所有实例以添加一个新的提交,而是拆分最后一个提交并将其部分应用于适当的先前提交。

If I interpret your question to instead be "How can I rename a function in all commits it appears in" then the following works (tested against the two sample commits you provided, but not intended to be fully functional). 如果我将您的问题解释为“如何在出现的所有提交中重命名功能”,那么以下工作(针对您提供的两个示例提交进行了测试,但并不旨在发挥全部功能)。

When working on feature branch you'd need to pass that in to limit how many commits filter-branch tried to process. 在功能分支上工作时,您需要传递该值以限制filter-branch尝试处理的提交数量。

git filter-branch --tree-filter 'git grep --name-only foo | xargs sed -i "s/foo/bar/g"'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM