LoginSignup
2
1

More than 5 years have passed since last update.

interactive-rebaseのexecを使ってコミット毎に単語を置換する

Last updated at Posted at 2015-07-02

まず事前準備として、HEADで変更したファイルに対して置換処理を実行したあと、その変更をHEADに追加するスクリプトを用意する

bin/substitute-head.sh
git diff-tree --no-commit-id --name-only -r HEAD \
  | xargs git grep -l $1 \
  | xargs sed -i '' -e "s/${1}/${2}/g"
git commit -a --amend -C HEAD

次に変更したいコミットを含む形でinteractive-rebaseを実行する。そのときexecオプションを指定する

git rebase -i -x "bash substitute-head.sh foo bar" $(git merge-base master HEAD)

するとこんな感じの画面が表示される

pick 6ae3155 first
exec bash tmp-replace.sh foo bar
pick fb5cca5 second
exec bash tmp-replace.sh foo bar
pick 311b1ec third
exec bash tmp-replace.sh foo bar

エディタを閉じるとコミット単位でsubstitute-head.shが実行され、その結果、fooがbarに変更される。

もしおかしなことになったら、git-reflogを使って巻き戻しましょう。

参考

2
1
0

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
2
1