3

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.

miviclin
  • 487
  • 1
  • 5
  • 11
  • I don't think that's possible. Why don't you simply replace all occurrences and commit? – nietonfir Nov 11 '13 at 11:40
  • Could you be more specific about what you need help with? You've found the git-filter-branch command -- have you read the documentation for it? Are you confused about the operation of git-filter-branch? What have you tried so far? – John Bartholomew Nov 11 '13 at 11:49
  • I've used git-filter-branch before to rename a directory in the whole history of the repo, but what I'm trying to do now is changing the content of some files in the whole history. I've read the man page of git-filter-branch and I don't see any specific example for this. I've been trying to do this whith the --tree-filter parameter with no luck. – miviclin Nov 11 '13 at 11:57

1 Answers1

2

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

torek
  • 448,244
  • 59
  • 642
  • 775