我做了5次提交,其中引用了一些变量FooObj。然后我意识到那FooObj是错误的选择并创造了BarObj。现在,我想进行一次提交,FooObj以BarObj 仅在这5次提交中添加的行中替换字符串的所有实例。
我无法在整个代码库中进行搜索和替换,因为FooObj仍然可以在许多地方正确使用它;必须将其本地化为这5个提交。
我可以手动替换,但是有自动方法吗?
您可以通过git diff --name-only HEAD~5. 然后,您可以将其通过管道传输到 sed 命令中,以将您的变量名替换为xargs.
所以整个命令最终会看起来像这样:
git diff --name-only HEAD~5 | xargs sed -i 's/FooObj/BarObj/g'
Run Code Online (Sandbox Code Playgroud)
这应该会为您更新正确的文件。验证更改是否正确,您可以像往常一样提交它们。
编辑
由于您只希望在提交中更改行,因此该方法不会那么自动化,但您可以创建补丁修复这些行并应用它,这应该会导致您想要的更改。
http://www.thegeekstuff.com/2014/03/git-patch-create-and-apply/
您将创建一个补丁文件并通过以下方式应用它:
git diff HEAD~5 HEAD >> BarObjPatch.diff
//Edit the patch file to correct the changes.
//As noted in the comments, be careful as this will modify context lines
sed -i 's/FooObj/BarObj/g' BarObjPatch.diff //Can also edit file manually
git apply BarObjPatch.diff
Run Code Online (Sandbox Code Playgroud)
这应该会导致您想要的更改。