[Git] gitの歴史に残っているユーザー名やメールアドレスを書き換える
commit内に含まれる情報を書き換えます。
[markdown]
## 識別情報を書き換える
まず既存の情報を書き換えます。
“`
% git config –global user.name “your_name”
% git config –global user.email your_email@gmail.com
% git config –list
user.name=your_name
user.email=your_email@gmail.com
“`
> * [Git – 最初のGitの構成](http://git-scm.com/book/ja/%E4%BD%BF%E3%81%84%E5%A7%8B%E3%82%81%E3%82%8B-%E6%9C%80%E5%88%9D%E3%81%AEGit%E3%81%AE%E6%A7%8B%E6%88%90)
GitHub.app や SourceTree.app の情報も忘れずに書き換えましょう。
## git filter-branch
現状はこのようになっています。
“`
% git show-ref
cc34ba95390fc5efca16b50dd75ade62f52652c1 refs/heads/master
cc34ba95390fc5efca16b50dd75ade62f52652c1 refs/remotes/origin/master
“`
下記のようなコマンドとシェルスクリプトを実行します。
“`
git filter-branch -f –commit-filter ‘
GIT_AUTHOR_NAME=”your_name”
GIT_AUTHOR_EMAIL=”your_email@gmail.com”
GIT_COMMITTER_NAME=”your_name”
GIT_COMMITTER_EMAIL=”your_email@gmail.com”
git commit-tree “$@”
‘ HEAD
“`
実行結果。
“`
% git filter-branch -f –commit-filter ‘
quote> GIT_AUTHOR_NAME=”your_name”
quote> GIT_AUTHOR_EMAIL=”your_email@gmail.com”
quote> GIT_COMMITTER_NAME=”your_name”
quote> GIT_COMMITTER_EMAIL=”your_email@gmail.com”
quote> git commit-tree “$@”
quote> ‘ HEAD
Rewrite c812ba092756390b292089aa2c975dda37dd2e0e (1/1)
Ref ‘refs/heads/master’ was rewritten
“`
## git push -f
git push するも reject されます。
“`
% git push origin master
To git@github.com:your_name/zfSkelton.git
! [rejected] master -> master (fetch first)
error: failed to push some refs to ‘git@github.com:your_name/zfSkelton.git’
hint: Updates were rejected because the remote contains work that you do
hint: not have locally. This is usually caused by another repository pushing
hint: to the same ref. You may want to first merge the remote changes (e.g.,
hint: ‘git pull’) before pushing again.
hint: See the ‘Note about fast-forwards’ in ‘git push –help’ for details.
“`
git push -f します。
この他に方法はないのだろうか?
“`
% git push -f origin master
Counting objects: 27, done.
Delta compression using up to 2 threads.
Compressing objects: 100% (20/20), done.
Writing objects: 100% (27/27), 5.51 KiB | 0 bytes/s, done.
Total 27 (delta 1), reused 27 (delta 1)
To git@github.com:your_name/zfSkelton.git
+ c29e059…dfe2749 master -> master (forced update)
“`
確認してみると、確かにコミットのチェックサムが変わっています。
“`
% git show-ref
2bc872815b6f65d39970497263e1bbac89252ea2 refs/heads/master
c812ba092756390b292089aa2c975dda37dd2e0e refs/original/refs/heads/master
2bc872815b6f65d39970497263e1bbac89252ea2 refs/remotes/origin/master
“`
remotes も新しい設定に書き換わっています。
“`
% git show 2bc872
commit 2bc872815b6f65d39970497263e1bbac89252ea2
Author: your_name
Date: Thu Aug 29 17:12:24 2013 +0900
“`
## バックアップを削除する
(おそらく必要があれば)
git filter-branch で作成されたローカルの refs/original を削除します。
現状、確認すると古い情報が見えました。
“`
% git show c812
commit c812ba092756390b292089aa2c975dda37dd2e0e
Author: your_name_old
Date: Wed Jul 17 11:20:31 2013 +0900
zf create skelton-project (Version: 1.12.3)
“`
“`
% git update-ref -d refs/original/refs/heads/master
% git show-ref
2bc872815b6f65d39970497263e1bbac89252ea2 refs/heads/master
2bc872815b6f65d39970497263e1bbac89252ea2 refs/remotes/origin/master
“`
> * [gitのユーザ名やメールアドレスをコミット後に書き換える。 – こせきの技術日記](http://koseki.hatenablog.com/entry/20081115/gitFilterBranch)
> * [Git – 歴史の書き換え](http://git-scm.com/book/ja/Git-%E3%81%AE%E3%81%95%E3%81%BE%E3%81%96%E3%81%BE%E3%81%AA%E3%83%84%E3%83%BC%E3%83%AB-%E6%AD%B4%E5%8F%B2%E3%81%AE%E6%9B%B8%E3%81%8D%E6%8F%9B%E3%81%88)
> * [GitのCommit中のAuthor名およびCommitter名を変える – idesaku blog](http://d.hatena.ne.jp/idesaku/20090908/1252419890)
> * [git filter-branchで過去commitのAuthorとメールアドレスを一括変更 – Glide Note – グライドノート](http://blog.glidenote.com/blog/2012/07/25/git-filter-branch/)
## 補遺
> * [Githubのメールアドレスを非公開にしておく方法 | IDEA*IDEA](http://www.ideaxidea.com/archives/2013/08/keepyouremailprivate.html)
あわせて、メールアドレスを非公開にしておく。
> * [gitの歴史上からpasswordを完全に削除したい – (゚∀゚)o彡 sasata299’s blog](http://blog.livedoor.jp/sasata299/archives/52053534.html)
“`
% git filter-branch –tree-filter “find . -type f -exec sed -i ” -e ‘/PASSWORD/d’ {} \;”
“`
[/markdown]