LoginSignup
62
58

More than 5 years have passed since last update.

Gitリポジトリ内の特定の語を全て置換する

Last updated at Posted at 2012-12-26

FooをBarにする場合、先にfoo->barをしてしまうと、Fooまでbarになってしまうので注意(FooはBarになって欲しいはず)

BSD (Mac)

BSDでは--in-placeがバックアップファイルの拡張子を受け取るので、バックアップファイルが要らない場合は''を指定する

git grep -l Foo | xargs sed -i '' -e 's/Foo/Bar/g'
git grep -l foo | xargs sed -i '' -e 's/foo/bar/g'

''を忘れると-eというのが末尾に追加されたバックアップファイルが作成される(置換はちゃんと行われる。この動作はバグなんじゃないかと思うけど、よく分からない)

GNU

GNUのsed若干実装が違うため、''を渡す必要がない。

git grep -l Foo | xargs sed -i -e 's/Foo/Bar/g'
git grep -l foo | xargs sed -i -e 's/foo/bar/g'

逆に、バックアップファイルが欲しい場合(拡張子.bakをつける)

git grep -l Foo | xargs sed -i.bak -e 's/Foo/Bar/g'
git grep -l foo | xargs sed -i.bak -e 's/foo/bar/g'

iと.bakが連続している点に注意

62
58
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
62
58