My repo looks something like this:
(develop) D - E - F - G - H
(master) A - B - C
sed -i 's/foo/bar/g' FILE
You were already quite close with your attempt.
git filter-branch --tree-filter 'sed -i "s/foo/bar/g" $FILES' D^..H HEAD
assuming you are on your
develop branch and you replace D and H with the commit index.
If you mess something up, there is always the reflog. You can also branch before or hand-write the commit reference before as a safeguard. I would recommend performing this in a separate branch, however.
$FILES here is a list of files containing your variable. I guess you could replace it with
$(git grep --name-only foo). I didn't try it wowever, and you might have to escape the
$ symbol. The simplest option is to hand-write a list of files there, of course.
Edit: just tried. it works without escaping.
sed however fails if there is no input file, aborting the process. You also get a copy of your old refs in
.git/refs/original/, so you might need to add
git filter-branch if this is not your first attempt, in order to overwrite the copy.