gitshellsedterminalzsh

Git sed ignore binaries


Similar to this user I'm having a problem running sed over all files in a repo when some of the tracked files are binaries.

Neither git ls-files nor some git-grep solutions from that question like git grep --full-name -l '.' ignore the binaries, so passing them to sed

git ls-files | xargs sed -i '' 's/here/there/g'

causes an error "sed: RE error: illegal byte sequence".

How can I run sed on all the files it can be run on, and just ignore binaries?


Solution

  • Replace git ls-files with git grep -lI .. Actually, it'd probably be better if you used git grep -lI 'here' | ... so you only run sed on files that contain a string that matches the regexp used in your sed command, i.e.:

    git grep -lI 'here' | xargs sed -i '' 's/here/there/g'
    
    -I
    Don’t match the pattern in binary files.