Is there a 'git sed' or equivalent?

Here's a solution that combines those of of Noufal and claytontstanley and avoids touching files that won't change.

In the [alias] block of my ~/.gitconfig file:

psed = !sh -c 'git grep --null --full-name --name-only --perl-regexp -e \"$1\" | xargs -0 perl -i -p -e \"s/$1/$2/g\"' -

To use:

git psed old_method_name new_method_name

Thanks to both Noufal and Greg for their posts. I combined their solutions, and found one that uses git grep (more robust than git ls-files for my repo, as it seems to list only the files that have actual src code in them - not submodule folders for example), and also has the old method name and new method name in only one place:

In the [alias] block of my ~/.gitconfig file:

sed = ! git grep -z --full-name -l '.' | xargs -0 sed -i -e

To use:

git sed 's/old-method-name/new-method-name/ig'

You could use git ls-files in combination with xargs and sed:

git ls-files -z | xargs -0 sed -i -e 's/old-method-name/new-method-name/g'

You could do a

for i in $(git grep --full-name -l old_method_name)
do
 perl -p -i -e 's/old_method_name/new_method_name/g' $i
done

stick that in a file somewhere and then alias it as git sed in your config.

Update: The comment by tchrist below is a much better solution since it prevents perl from spawning repeatedly.

Tags:

Linux

Git

Bash

Sed