0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

プロジェクトを tar や zip で固めてAIに渡すと .env も .git の履歴も入る:git archive なら追跡ファイルだけ(実機で比較)

0
Posted at

この記事の要点

  • AIチャットやレビュー相手にプロジェクトを丸ごと渡すとき、フォルダをそのまま tar czf や Python の shutil.make_archive で固めると、.gitignore に書いた .env・*.pem・node_modules/ も、.git フォルダも全部入りました(GNU tar 1.35・Python 3.13.5 で確認)。
  • .git が入っていると、過去にコミットして消した .env も、受け取った側が git show で読めました。
  • git archive HEAD で作ると、中身はコミット済みの追跡ファイルだけ(今回は .gitignore と app.js の2つ)になりました。
  • 渡す前に、入るファイルを次の1行で確かめられます。
git ls-files --cached --others --exclude-standard

.env や鍵ファイルが手元のどこにあるかをファイル名だけで点検する CLI も作っています。今回の検証用フォルダでは .env と server.pem が表示されました。

npx github:supportapp3-lab/ai-secret-check scan

試した環境

  • git 2.47.3、GNU tar 1.35、Python 3.13.5(Linux)
  • 使い捨てのリポジトリ demo/ を作り、値はすべてダミーです。
git init -b main demo && cd demo
printf 'node_modules/\n.env\n*.pem\n' > .gitignore
echo 'console.log("hi")' > app.js
echo 'API_KEY=dummy-old-key-0000' > .env
git add .gitignore app.js && git add -f .env
git commit -m "first (誤って .env を含む)"
git rm --cached .env && git commit -m "remove .env from index"
echo 'API_KEY=dummy-new-key-1111' > .env
echo 'DUMMY PEM' > server.pem
mkdir -p node_modules/x && echo x > node_modules/x/i.js

git status --short --ignored では .env・node_modules/・server.pem が !!(無視)と表示される状態です。

フォルダを tar や zip で固めた場合

$ tar czf project.tgz demo && tar tzf project.tgz | grep -E "\.env|\.pem|node_modules/x|\.git/HEAD"
demo/.git/HEAD
demo/node_modules/x/
demo/node_modules/x/i.js
demo/server.pem
demo/.env

Python で zip にした場合も同じでした。

$ python3 -c 'import shutil; shutil.make_archive("project","zip","demo")'
$ unzip -l project.zip | grep -E '\.env|\.pem|node_modules/x|\.git/HEAD'
       10  2026-11-02 05:18   server.pem
       27  2026-11-02 05:18   .env
       21  2026-11-02 05:18   .git/HEAD
        0  2026-11-02 05:18   node_modules/x/
        2  2026-11-02 05:18   node_modules/x/i.js

.gitignore は Git が「追跡しない」ファイルを決めるためのもので、tar や zip はそれを見ずにフォルダの中身をそのまま入れました。

.git が入ると、消したはずの古い .env も読める

受け取った側の立場で project.tgz を展開して、履歴を見てみました。

$ mkdir x && tar xzf project.tgz -C x && cd x/demo
$ git log --oneline
d37d955 remove .env from index
0bef694 first (誤って .env を含む)
$ git show HEAD~1:.env
API_KEY=dummy-old-key-0000

今の .env(new-key)だけでなく、一度コミットしてから外した古い値(old-key)も読めました。履歴に残った .env の探し方と消し方は、過去にコミットした.envはgitの履歴から読めるにまとめています。

git archive で作った場合

$ git archive --format=zip -o ../archive.zip HEAD && unzip -l ../archive.zip
  Length      Date    Time    Name
---------  ---------- -----   ----
       25  2026-11-02 05:18   .gitignore
       18  2026-11-02 05:18   app.js
---------                     -------
       43                     2 files

公式ドキュメントの説明は「指定したツリーの構造を含むアーカイブを作る(Create an archive of files from a named tree)」です。今回は HEAD(最新のコミット)を指定したので、コミットに入っていない .env・server.pem・node_modules/ と .git フォルダは入りませんでした。

git archive で気をつけること(実機で確認した2点)

  1. コミットしていない変更は入りません。 app.js を書き換えてコミットせずに git archive HEAD すると、中身はコミット時点の console.log("hi") のままでした。渡す前にコミットするか、渡したいのが作業中の状態なのかを確かめてください。
  2. 追跡中のファイルは、秘密でも入ります。 .env を git add してしまっている場合は git archive にも入ります(.gitignore に書いても追跡は外れません。追跡中の .env に .gitignore は効かない話)。追跡したまま配布物からだけ外したいファイルは、公式の ATTRIBUTES 節にある export-ignore を .gitattributes に書くと外れました。
$ printf 'secrets.sample.json export-ignore\n' > .gitattributes
$ git add .gitattributes secrets.sample.json && git commit -m attrs
$ git archive --format=zip -o ../a2.zip HEAD && unzip -Z1 ../a2.zip
.gitattributes
.gitignore
app.js

tar の除外オプションも試した

GNU tar には --exclude-vcs(.git などを外す)と --exclude-vcs-ignores(.gitignore などの除外パターンを読む)があります。

$ tar czf clean.tgz --exclude-vcs --exclude-vcs-ignores demo && tar tzf clean.tgz
demo/
demo/app.js
demo/node_modules/
demo/node_modules/x/
demo/node_modules/x/i.js

.git・.env・server.pem は外れましたが、node_modules/(末尾にスラッシュのある書き方)は外れずに入りました(GNU tar 1.35 で確認)。Git と同じ解釈になるとは限らないので、Git 管理のプロジェクトなら git archive を使い、どうしても tar を使うなら tar tzf で中身を見てから渡すのが確実です。

渡す前の確認手順

  1. 入るファイルを一覧する(追跡中+無視されていない未追跡)
git ls-files --cached --others --exclude-standard
  1. 一覧に .env や鍵があれば、渡す前に追跡を外すか export-ignore を付ける
  2. git archive -o project.zip HEAD で作り、unzip -Z1 project.zip で中身を確かめてから渡す

まとめ

作り方 .env・*.pem(無視指定) node_modules/ .git(履歴)
tar czf 入る 入る 入る
shutil.make_archive 入る 入る 入る
tar --exclude-vcs --exclude-vcs-ignores 外れる 入る 外れる
git archive HEAD 外れる(未追跡なので) 外れる 外れる

参考

※ 仕様は 2026-11-02 時点の公式ドキュメントに基づきます。動作は git 2.47.3・GNU tar 1.35・Python 3.13.5 の使い捨てリポジトリで、ダミー値だけを使って試しました。macOS の Finder の「圧縮」など、ほかのツールは試していません。

更新情報やちょっとしたTipsは X(@supportapp3_lab)で発信しています。

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?