LoginSignup
2
0

More than 3 years have passed since last update.

複数人開発時に git log の 特定 author を一括変更する

Last updated at Posted at 2019-04-30

はじめに

github はgit configに記載されたメールアドレスを照合して草を生やします。

そのため、git config に誤ったメールアドレスを記載をした場合や、そもそもメールアドレスを記載していないと草が生えません。

$ git config --list # 設定の確認

# 名前の設定
$ git config --global user.name "Foo Bar"
# メールアドレスの設定
$ git config --global user.email "sample@example.com"

本題

複数人で開発を行っている中、上記に後から気付いた時、あなたは草が生えないことにショックを受けるでしょう。

git filter-branchを利用すれば、全コミットの author を書き換えられますが、それでは、他のユーザーの author 情報も書き換えてしまう可能性があります。

update-all-git-author.sh
git filter-branch -f --env-filter "GIT_AUTHOR_NAME='Foo Bar'; GIT_AUTHOR_EMAIL='sample@example.com'; GIT_COMMITTER_NAME='Foo Bar'; GIT_COMMITTER_EMAIL='sample@example.com';" HEAD

しかし、シェルスクリプトを利用することでそのようなニーズを満たすことが可能です。

update-git-author.sh
#!/bin/sh

git filter-branch --env-filter '
OLD_EMAIL="your-old-email@example.com"
CORRECT_NAME="Your Correct Name"
CORRECT_EMAIL="your-correct-email@example.com"
if [ "$GIT_COMMITTER_EMAIL" = "$OLD_EMAIL" ]
then
    export GIT_COMMITTER_NAME="$CORRECT_NAME"
    export GIT_COMMITTER_EMAIL="$CORRECT_EMAIL"
fi
if [ "$GIT_AUTHOR_EMAIL" = "$OLD_EMAIL" ]
then
    export GIT_AUTHOR_NAME="$CORRECT_NAME"
    export GIT_AUTHOR_EMAIL="$CORRECT_EMAIL"
fi
' --tag-name-filter cat -- --branches --tags
$ chmod 755 update-git-author.sh # 実行権限さえ与えればいいので 111 でも構いません

# 実行
$ ./update-git-author.sh

# 変更を github 上へ反映
$ git push origin master --force

終わりに

私がまさに上記の問題に直面していたので本当に助かりました。

シェルスクリプト様には頭が上がりません。

参考

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