56

Let's say I want to rename a method in source code contained in a git repository. I could do this by hand, but the method name might be in multiple places (e.g., unit test, documentation, actual method). To check where the method is used, I use 'git grep'. I get 'git grep' to show only lines that I want to change, and then I don't have a workflow to automatically change those lines.

I'm looking for an automated way (hopefully using git tools) to do this last step. I was hoping there was some sort of 'git sed' or equivalent, but I can't find any.

The interface I'm thinking would be nice: git sed 's/old-method-name/new-method-name/g'

10 Answers 10

64

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'
9
  • 4
    Yes. If you have whitespace in filenames, use git ls-files -z | xargs -0 sed .... Commented Mar 11, 2012 at 2:38
  • 2
    Probably but I think you can pass -z to ls-files and -0 to xargs to make it work. Commented Mar 11, 2012 at 2:39
  • 4
    This technique kept failing for me, b/c I have submodules in the repo that cause sed to exit before getting to all of the files listed in ls-files. At least I think that's what's going on. I'm still troubleshooting it, as I favor this approach over @NoufalIbrahim, because the method-name is only in one place. Commented Mar 11, 2012 at 3:12
  • 1
    Note that you need GNU sed for this (e.g. it won’t work on macOS with its default sed, because of how its -i option works).
    – kirelagin
    Commented May 28, 2020 at 15:45
  • 1
    git ls-files will include submodules which are not files. Instead of git ls-files use: git ls-files | grep -Fxvf <(grep "^\s*\[submodule " .gitmodules | cut -d '"' -f2)
    – Liakos
    Commented Jun 1, 2021 at 13:43
25

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'
2
  • 2
    This works in Linux, but on OS X it copies all the files in the repository and adds -e to the end of them. This seems to work better: git grep -z --full-name -l '.' | xargs -0 sed -i ''. Commented Nov 29, 2016 at 4:46
  • 1
    Add the -I flag to git grep to stop it also listing binary files
    – minseong
    Commented Aug 28 at 0:14
14

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.

2
  • 6
    I suspect you need a $i there at the end of the command. Also, you don’t have to launch Perl repeatedly. It will process all the files you give it as argument. So perl -i.orig -pe 's/\bold_method_name\b/new_method_name/g' $(git grep --full-name -l old_method_name) should suffice.
    – tchrist
    Commented Mar 11, 2012 at 3:53
  • Much better. I've fixed the $i in the snippet. Commented Mar 11, 2012 at 4:32
9

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
2
  • can I also use this for general substitutions, e.g. substitutions that include spaces? Commented Dec 20, 2016 at 21:37
  • Yes, but depending on your shell you may have to adjust how many times you quote the arguments.
    – Mr Fooz
    Commented Jan 5, 2017 at 16:53
7

Yes, there's. In Ubuntu the package git-extras provides the command. Install it:

$ sudo apt-get install git-extras

Use it like bellow e.g. to correct a spelling issue quickly:

$ git sed 'qoute' 'quote'

Unfortunately it doesn't support file filters like what git grep does:

$ git grep -e 'class' -- '*.py'

The same functionality also exists on Mac and other operating systems. Checkout its installation page.

1
3

Unhappy with most other solutions provided (which is basically just a string-replace on git tracked files) I wrote my own script: git-sed.

  • It supports any expression sed supports (e.g git sed '1{/^$/d}')
  • Can run on a subset of paths in the repo (git sed 's/foo/bar' src tests)
  • Multiple expressions (git sed -e 's/foo/bar' -e '/bar/d').
  • etc...

Just drop it anywhere in PATH to use it or add an alias pointing to the full path.

1

Note that starting git 2.1 (Q3 2014), you can set "full-name" by default for git grep.
(See commit 6453f7b by Andreas Schwab)

"git grep" learned grep.fullname configuration variable to force "--full-name" to be default.
This may cause regressions on scripted users that do not expect this new behaviour.

That means the previous solutions can benefit from:

git config grep.full-name true

And use:

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

An option that will work on both macOS (BSD sed) and Linux (GNU sed) is to create an executable git-sed file somewhere in your $PATH containing the following code:

if sed --version >/dev/null 2>&1
then
    # GNU sed
    git grep -lz "$1" | xargs -0 sed -i "s/$1/$2/g"
else
    # BSD sed
    git grep -lz "$1" | xargs -0 sed -i '' "s/$1/$2/g"
fi

This can be used like:

> git sed before after

to replace all occurrences of "before" with "after".

0

See git-search-replace on github - it's designed for this exactly.

0

I have written a git sed which supports file filtering:

#!/bin/bash

split=$(($# + 1))

for i in $(seq 1 $#); do
   if [[ "${!i}" = "--" ]]; then
       split=$i
   fi
done

git ls-files -z "${@:$split:$#}" | xargs -0 sed -b -i "${@:1:$(($split - 1))}"

(You probably don't want the -b parameter on non-Windows platforms; it's necessary on Windows to preserve Windows-style newlines.)

You can then add an alias in your .gitconfig:

[alias]
    sed = ! <path to git-sed>

so that you can use it like git sed -e <your expression> -- <path filter>.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.