Ten examples of git-archive

hustnzj發表於2022-09-08

Git-archive

This command can create an archive of files from a named tree

Basic usages

usage: git archive [<options>] <tree-ish> [<path>...]
   or: git archive --list
   or: git archive --remote <repo> [--exec <cmd>] [<options>] <tree-ish> [<path>...]
   or: git archive --remote <repo> [--exec <cmd>] --list

    --format <fmt>        archive format
    --prefix <prefix>     prepend prefix to each pathname in the archive
    --add-file <file>     add untracked file to archive
    --add-virtual-file <path:content>
                          add untracked file to archive
    -o, --output <file>   write the archive to this file
    --worktree-attributes
                          read .gitattributes in working directory
    -v, --verbose         report archived files on stderr
    -NUM                  set compression level

    -l, --list            list supported archive formats

    --remote <repo>       retrieve the archive from remote repository <repo>
    --exec <command>      path to the remote git-upload-archive command

Examples

  1. Create a single snapshot of your project so you can send the resulting archive to a client for review or create git-independent backups of important revisions.

    git archive master --format=tar --output=../name_what_you_want.tar
  2. Create a tar archive that contains the contents of the latest commit on the current branch, and extract it in the /var/tmp/junk directory.

    git archive --format=tar --prefix=junk/ HEAD | (cd /var/tmp/ && tar xf -)

    References:
    Piping in Unix or Linux - GeeksforGeeks
    Linux Double Ampersand - javatpoint
    bash - How are parentheses interpreted at the command line? - Unix & Linux Stack Exchange
    tar(1) [osx man page]

  3. Create a compressed tarball for v1.0 release.

    git archive --format=tar --prefix=git-1.0/ v1.0 | gzip >git-1.0.tar.gz

    References:
    gzip(1) [osx man page]

  4. Same as above, but using the builtin tar.gz handling.

    git archive --format=tar.gz --prefix=git-1.0/ v1.0 >git-1.0.tar.gz
  5. Same as above, but the format is inferred from the output file.

    git archive --prefix=git-1.0/ -o git-1.0.tar.gz v1.0
  6. Create a compressed tarball for v1.4.0 release, but without a global extended pax header.

    git archive --format=tar --prefix=git-1.0/ v1.0^{tree} | gzip >git-1.0.tar.gz

    References:
    For v1.0^{tree}, refer to: Git - gitrevisions Documentation
    Git - git-get-tar-commit-id Documentation

  7. Put everything in the current head’s Documentation/ directory into git-about.zip, with the prefix git-about/.

    git archive --format=zip --prefix=git-about/ HEAD:about/ > git-about.zip

    References:
    HEAD:about/
    Git - revisions Documentation

  8. Create a Zip archive that contains the contents of the latest commit on the current branch. Note that the output format is inferred by the extension of the output file.

    git archive -o latest.zip HEAD

    References:
    unzip(1) - Linux man page

  9. Creates a tar archive that contains the contents of the latest commit on the current branch with no prefix and the untracked file configure with the prefix build/.

    git archive -o latest.tar --prefix=build/ --add-file=configure --prefix= HEAD
  10. Configure a “tar.xz” format for making LZMA-compressed tarfiles. You can use it specifying --format=tar.xz, or by creating an output file like -o foo.tar.xz.

    git config tar.tar.xz.command "xz -c"
    git archive -o latest.tar.xz HEAD

    References:
    How to Extract (Unzip) tar.xz File | Linuxize
    Git - git-archive tar.<format>.command Documentation

本作品採用《CC 協議》,轉載必須註明作者和本文連結
日拱一卒

相關文章