IT系/リファレンス/Gitコマンド/git_archive

Last-modified: 2020-05-31 (日) 13:52:22

git archive

アーカイブを作成するコマンド。

用例

  • 最新のコミットを「hoge.zip」というファイル名でアーカイブ
    git archive HEAD -o hoge.zip
  • コミットID「82e3157」を「fuga.zip」というファイル名でアーカイブ
    git archive 82e3157 -o fuga.zip
  • アーカイブを特定のフォルダに入れたい場合
    git archive HEAD --prefix=hogedir/ -o=hoge.zip
  • 一部のフォルダだけを取り出す(フォルダ自体を含まない)
    git archive HEAD:hogedir --output=hoge.zip
  • 一部のフォルダだけを取り出す(フォルダ自体を含む)
    git archive HEAD hogedir --output=hoge.zip
  • コミットの差分をアーカイブ(Linux)
    git archive [コミット1] `git diff --name-only [コミット2] [コミット3]` -o [ファイル名]
  • エラーの原因となるファイルを除外してアーカイブ(Linux)
    git archive HEAD `git diff --name-only HEAD HEAD~2| sed '/ /d'` -o archive.zip
  • Git管理下にあるファイルを除外してアーカイブ(設定ファイル「.gitattributes」で除外ファイルを指定)
    git archive HEAD --worktree-attributes -o xxxx.zip

書式

git archive [対象コミット] [オプション]

公式によると以下の通り。

git archive [--format=<fmt>] [--list] [--prefix=<prefix>/] [<extra>]
            [-o <file> | --output=<file>] [--worktree-attributes]
            [--remote=<repo> [--exec=<git-upload-archive>]] <tree-ish>
            [<path>…&#8203;]

options

--format=<fmt>Format of the resulting archive: tar or zip. If this option is not given, and the output file is specified, the format is inferred from the filename if possible (e.g. writing to "foo.zip" makes the output to be in the zip format). Otherwise the output format is tar.
-l | --list対応しているファイル形式の一覧を表示
-v | --verboseReport progress to stderr.
--prefix=<prefix>/Prepend <prefix>/ to each filename in the archive.
-o <file> | --output=<file>標準出力せず、指定したファイル名でアーカイブファイルを作成
--worktree-attributesLook for attributes in .gitattributes files in the working tree as well (see ATTRIBUTES).
<extra>This can be any options that the archiver backend understands.
-0 : Store the files instead of deflating them.
-9 : Highest and slowest compression level. You can specify any number from 1 to 9 to adjust compression speed and ratio.
--remote=<repo>Instead of making a tar archive from the local repository, retrieve a tar archive from a remote repository. Note that the remote repository may place restrictions on which sha1 expressions may be allowed in <tree-ish>. See git-upload-archive for details.
--exec=<git-upload-archive>Used with --remote to specify the path to the git-upload-archive on the remote side.
<tree-ish>The tree or commit to produce an archive for.
<path>…Without an optional path parameter, all files and subdirectories of the current working directory are included in the archive. If one or more paths are specified, only these are included.
-hコマンドのヘルプを表示

説明

アーカイブを作成するコマンド。

  • 指定されたツリーのツリー構造を含む指定フォーマットのアーカイブを作成し、標準出力に書き出す。
    <prefix>が指定されている場合は、アーカイブ内のファイル名の前に追加される。
  • ツリーID指定時と、コミットIDまたはタグID指定時のgit archiveの動作は異なる。
  • 前者の場合、現在時刻がアーカイブ内の各ファイルの修正時刻として使用される。
  • 後者の場合、参照されたコミットオブジェクトに記録されているコミット時間が代わりに使用される。
    さらに、tarフォーマットが使用されている場合、コミットIDはグローバル拡張paxヘッダーに保管される。
    git get-tar-commit-idを使って抽出することができる。
    ZIPファイルでは、ファイルコメントとして保存される。
  • デフォルトではzip形式とtar.gz形式に対応。

関連

参考リンク

その他メモ

なし。