重写git历史,替换每个文件中的单词 [英] Rewrite git history replacing a word in every single file
问题描述
我想用git-filter-branch重写我的仓库的git历史记录,以便在所有文件中用bar.foo替换所有出现的foo.bar。
我该如何做到这一点?
更新:
我一直在玩 - tree-filter参数,我已经能够用这个替换指定文件中的单词:
git filter-branch - f --tree-filter'
if [-f testfile]
then
sed -is / foo.bar / bar.foo / g testfile
fi' - - 所有
这似乎是我能达到我想要的最接近的水平。
就其效果而言,您可以将它想象为您已经完成:
git checkout< rev>
,以便工作目录现在包含与给定修订相关的树。 (事实上,它几乎检查每一个版本,进入一个临时目录,这就是为什么它很慢。另见 -d
标志。)如果你想要一个改变树中的每个文件,你可能会这样做:
find。 -type f -print | xargs sed -i''-e's / foo \..bar / bar.foo / g'
<关于 - tree-filter
的特殊之处在于它自动执行任何必需的 git add
和/或 git rm
(因此 -i''
; -i .bak
会 git add
所有 .bak
文件)。
< (请记住,如果您有标签,请使用 - tag-name-filter
,并且注意在注释标签上删除签名。)
I want to rewrite the git history of my repository with git-filter-branch to replace all occurrences of "foo.bar" with "bar.foo" in all files.
How can I achieve this?
Update:
I've being playing with the --tree-filter parameter and I've been able to replace the word in a specified file with this:
git filter-branch -f --tree-filter '
if [ -f testfile ]
then
sed -i s/foo.bar/bar.foo/g testfile
fi ' -- --all
This seems to be the closest I can get to achieve what I wanted.
The tree filter is run on every tree being filtered, one tree at a time.
In terms of its effect, you can imagine it as if you had done:
git checkout <rev>
so that the work directory now contains the tree associated with the given revision. (In fact, it does pretty much check out every revision, into a temporary directory, which is why it's so slow. See also the -d
flag.) If you want a change made to every file in the tree, you might do:
find . -type f -print | xargs sed -i '' -e 's/foo\.bar/bar.foo/g'
The special thing about --tree-filter
is that it automatically does any required git add
and/or git rm
(hence -i ''
; -i .bak
would git add
all the .bak
files).
(Remember to use --tag-name-filter
if you have tags, and beware of signature removal on annotated tags.)
这篇关于重写git历史,替换每个文件中的单词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!