4
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Gitのコミット差分から必要なファイルだけを抜き出してZIP化する方法

Posted at

はじめに

新しく追加したファイルや、更新したファイルだけをまとめて渡したいときはありませんか?
すべてのファイルを送るのは容量が大きくなってしまいますし、手間もかかりますよね。
そんなときは、Gitのコマンドを使うことで、コミット間の差分ファイルだけを自動で抜き出し、ZIPファイルにまとめることができます。

こんな場面で便利です

  • チームメンバーに「この変更分だけ送ってほしい」と頼まれたとき
  • サーバーに差分だけアップロードしたいとき
  • バックアップやレビュー用に、変更ファイルだけをまとめたいとき

手順

1. コマンド一つで差分ファイルをZIP化できます

以下のコマンドを使用します。

$ git archive --prefix=archive/ <ブランチ名> `git diff --name-only <前のコミット> <後のコミット> --diff-filter=d` -o archive.zip

コマンドの解説

  • git diff --name-only <前のコミット> <後のコミット> --diff-filter=d
    追加・更新されたファイル名だけをリストアップします(削除されたファイルは除外されます)。

  • git archive --prefix=archive/ <ブランチ名> ... -o archive.zip
    指定したファイルだけを、archive/フォルダにまとめてZIP化します。

2. 実行例

例えば、abc1234 から def5678 までの差分を抜き出したい場合は、次のように入力します。

$ git archive --prefix=archive/ main `git diff --name-only abc1234 def5678 --diff-filter=d` -o archive.zip
  • main はブランチ名です。必要に応じて変更してください。
  • archive.zip という名前でZIPファイルが作成されます。

まとめ

この方法を使うことで、余計なファイルを含めず、必要な差分だけを簡単にまとめて渡すことができます。
「すべて送るにはデータ量が多過ぎ!」と感じたときは、ぜひこの方法をお試しください。

4
3
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
4
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?