LoginSignup
11
9

More than 1 year has passed since last update.

暗号化したファイルをgit diffする

Last updated at Posted at 2015-10-03

Gitで管理しているファイルを暗号化しておきたい場合があるが,
git diffしてももちろん以下のように中身は出力されない.

$ git diff
Binary files a/foo.gpg and b/foo.gpg differ

中身を見たいのであれば, 以下のように設定が必要.
例として, ここでは, GnuPGを使う.

まず, 以下のように, 復号化するフィルタを設定する. 1

$ git config --global diff.gpg.textconv 'gpg -o-'

そうすると, ~/.gitconfigに以下のような行が追加されている.

~/.gitconfig
[diff "gpg"]
textconv = gpg -o-

このフィルタを*.gpgに適用したい場合は, .gitattributesに以下の行を追加する.

.gitattributes
*.gpg diff=gpg

以上の設定をした上で, git diffをすると, *.gpgのファイルに変更があるときに限り,
以下のように2回パスフレーズの入力を求められる. 2

$ git diff
Enter passphrase:
Enter passphrase:

パスフレーズを入力すると, 復号化され, git diffで見ることができる.

git grep--textconvオプションを付けると, パスフレーズの入力を求められ, 見ることができる.

$ git grep --textconv WORD
Enter passphrase:

参考


  1. -oは出力ファイルを指定するオプションで, 指定先の-は標準出力という意味なので,
    -o-で復号化内容を標準出力に出力ということになる. 

  2. これはそれぞれ変更前と後のファイルを復号化するパスフレーズ入力に対応している. 

11
9
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
11
9