String Replacement With Git Grep
Jason Meridth published a wonderful one-liner back in 2013, for replacing a string that occurs anywhere within a repo. I find myself using it often, but not often enough to memorize it, so I’m shamelessly duplicating it here for posterity.
TL;DR
On Linux:
git grep -l 'original_text' | xargs sed -i 's/original_text/new_text/g'
On Mac:
git grep -l 'original_text' | xargs sed -i '' -e 's/original_text/new_text/g'
The
-l
argument togit grep
only returns the file names and not the location in the file like it usually does.The
-e
argument is needed with thesed
portion on OSX.
❖