Git 将所有已完成的替换更改为其他内容
在重写遗留应用程序时,我用Git 将所有已完成的替换更改为其他内容,git,diff,Git,Diff,在重写遗留应用程序时,我用bar大量替换了foo,并混合了许多手动更改。一些替换必须手动撤消,并且原始代码中已经有许多其他的bars 现在,我看到每个foo被bar替换,实际上应该是baz 例如: 旧文件: 保留的“foo”和替换的“foo”以及保留的“bar” 新文件 保留的“foo”和替换的“bar”和保留的“bar” 通缉 一个保留的“foo”和一个替换的“baz”和一个保留的“bar” 所需的操作很简单:通过bar将foo的每个替换项修复为baz。我想知道是否有一种简单的方法可以使用
bar
大量替换了foo
,并混合了许多手动更改。一些替换必须手动撤消,并且原始代码中已经有许多其他的bar
s
现在,我看到每个foo
被bar
替换,实际上应该是baz
例如:
- 旧文件:
保留的“foo”和替换的“foo”以及保留的“bar”
- 新文件
保留的“foo”和替换的“bar”和保留的“bar”
- 通缉
一个保留的“foo”和一个替换的“baz”和一个保留的“bar”
bar
将foo
的每个替换项修复为baz
。我想知道是否有一种简单的方法可以使用git
或任何Linux工具
重新制定
也许这句话更清楚:
给定一个文件的两个版本,将baz
放在旧版本包含foo
和新版本包含bar
的每个地方
更多细节
实际上有三个完整的单词被不同长度的单词替换,比如
替换foo:grep-rl'foo'xargs sed-i's/foo/bar/g' 替换酒吧:grep-rl'bar'xargs sed-i's/bar/baz/g'您可以使用git diff的模式(以及传递给
-U
选项的足够大的值,以保留更改之间的所有上下文),并使用足够简单的脚本处理其输出,以纠正错误的替换
--word diff[=]
显示一个单词diff,使用
分隔已更改的单词。默认情况下,单词由空格分隔;
请参见下面的--word diff regex
。
默认为普通型
,并且
必须是以下各项之一:
:使用专用于脚本使用的基于行的格式。添加/删除/未更改的运行以通常的方式打印 统一的差异格式,以+/-/``字符开头 线的起点和延伸到线的终点。新行 在输入中,在其自身的一行上用波浪线陶瓷
表示~
下面,您将看到基于原型的上述方法的实现 用法:
# In last two commits (and, maybe, in the working copy) some "int"s
# were incorrectly changed to "unsigned", now change those to "long"
$myname main.c HEAD~2 /int/unsigned/long/
# In the working copy of somefile.txt all "abc" case-insensitive words
# were changed to "pqrs", now change them to "xyz"
$myname somefile.txt HEAD '/[aA][bB][cC]/pqrs/xyz/'
修复错误的替换
路径
修订
修复替换
在哪里
是工作树中文件的(相对)路径path
是必须修复的错误替换的版本revision
是一个字符串形式replacement\u fix
/orig\u模式/不正确的\u替换\u str/正确的\u替换\u str/
# In last two commits (and, maybe, in the working copy) some "int"s
# were incorrectly changed to "unsigned", now change those to "long"
$myname main.c HEAD~2 /int/unsigned/long/
# In the working copy of somefile.txt all "abc" case-insensitive words
# were changed to "pqrs", now change them to "xyz"
$myname somefile.txt HEAD '/[aA][bB][cC]/pqrs/xyz/'
假设比较时文件的工作副本位于路径
其提交的修订版修订版
包含替换的结果
原始模式的某些实例具有不正确的替换\u str
,
识别这些替换并将其更改为正确的\u替换\u str
示例:
# In last two commits (and, maybe, in the working copy) some "int"s
# were incorrectly changed to "unsigned", now change those to "long"
$myname main.c HEAD~2 /int/unsigned/long/
# In the working copy of somefile.txt all "abc" case-insensitive words
# were changed to "pqrs", now change them to "xyz"
$myname somefile.txt HEAD '/[aA][bB][cC]/pqrs/xyz/'
已知的限制/问题:
# In last two commits (and, maybe, in the working copy) some "int"s
# were incorrectly changed to "unsigned", now change those to "long"
$myname main.c HEAD~2 /int/unsigned/long/
# In the working copy of somefile.txt all "abc" case-insensitive words
# were changed to "pqrs", now change them to "xyz"
$myname somefile.txt HEAD '/[aA][bB][cC]/pqrs/xyz/'
- 它适用于单个文件。要修复提交、提交范围或本地更改中的所有错误替换,必须标识已更改文件的列表,并在循环中为所有文件调用此脚本
- 如果在原始(错误)替换过程中使用了不区分大小写的模式,则
替换修复
参数的原始模式
部分必须为每个字母使用[aA]
,[bB]
等regex atom
- 不处理与其他更改相邻的替换
- 有时可能会添加多余的空行(因为
git diff--word diff的输出中存在轻微的不一致)
修复错误的替换内容:
# In last two commits (and, maybe, in the working copy) some "int"s
# were incorrectly changed to "unsigned", now change those to "long"
$myname main.c HEAD~2 /int/unsigned/long/
# In the working copy of somefile.txt all "abc" case-insensitive words
# were changed to "pqrs", now change them to "xyz"
$myname somefile.txt HEAD '/[aA][bB][cC]/pqrs/xyz/'
#/usr/bin/env bash
myname=“$(basename“$0”)”
如果[$#-ne 3]
然后
cat“$file”
您只想更改提交消息,而不是实际的文件?@obsimanage否,我想更改文件。没有单一的承诺,我其实不在乎历史。。。在我得到一个编译版本之前,这是一个很大的混乱。比方说,我做了一个错误的大规模替换,我需要在保留所有手动更改的同时修复它。对投票人来说:这个问题有什么不清楚的地方吗?检查一下@dommm这就是我所做的:perl-pi-e的!福!酒吧!g
。这就是我应该做的:perl-pi-e的!福!巴兹!g
。在这期间,我做了许多手动更改。我不能简单地做perl-pi-e!酒吧!巴兹!g
现在,因为不是每次都应该更换。