git Verified current stable Not installed? Version Control

Git / Archive With Prefix Path

Archive With Prefix Path

Archives the repository content while prepending a specified path to all files in the resulting archive.

$
Terminal
git archive -o <path/to/file.tar> --prefix <path/to/prepend>/ HEAD

When To Use

During packaging for distribution where directory structure must be maintained, critical for deployment procedures.

Pro Tip

Prepend paths carefully; improper paths may lead to build failures on deployment systems.

Anatomy of Output

Understanding the result

Archive created at 'path/to/file.tar' with prefix 'deploy/'... Archive Creation Confirmation

Verifies the archive's path and the added prefix.

file1.txt added under 'deploy/' prefix File Addition Status

Listing of each file as per specified prefix.

Error: Could not find any files to add under 'path/to/file' Error Status

Indicates may need to correct the source path.

Power User Variants

Optimized versions

git archive --format zip -o archive.zip --prefix 'dist/' HEAD

Specifies a different output format and prefixing.

git archive --prefix 'release/' --output 'release.tar' HEAD

Archiving with a customization for release management.

Troubleshooting

Common pitfalls

fatal: cannot create output file 'path/to/file.tar': Permission denied

Solution: Check write permissions for the directory.

error: path 'path/to/prepend/' does not exist

Solution: Ensure that the specified prepend path is valid and accessible.

fatal: no matching files to archive

Solution: Validate that the specified commit has files to archive.

Command Breakdown

What each part is doing

git
Base Command
The executable that performs this operation. Here it runs Git before the shell applies any redirect operators.
-o
o| output
The value supplied for o| output.
<path/to/file.tar>
Input Files
The file path or paths supplied to this command.
<path/to/prepend>
path to prepend
The value supplied for path to prepend.
-o
Command Option
Tool-specific option used by this command invocation.
--prefix
Command Option
Tool-specific option used by this command invocation.

Alternative Approaches

Comparable commands in other tools

Alternative version control tools for the same job.