miviclin
miviclin

Reputation: 507

Rewrite git history replacing a word in every single file

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.

Upvotes: 3

Views: 1415

Answers (1)

torek
torek

Reputation: 488243

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

Upvotes: 2

Related Questions