Rewrite git history replacing a word in every single file

不打扰是莪最后的温柔 提交于 2019-12-12 15:53:17

问题


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.


回答1:


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.)



来源:https://stackoverflow.com/questions/19905409/rewrite-git-history-replacing-a-word-in-every-single-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!