Git Archive: Practical Guide for Efficient Packaging and Project Delivery
During software development and project delivery, we often need to package source code while excluding directories like .git/
and node_modules
.
The common approach is to manually select the required files for packaging.
Or, delete unwanted files first, then package the entire folder.
Both methods require manual filtering.
So, is there an elegant way to conveniently package project source code?
TL;DR
Use the git archive
command for packaging, which automatically excludes the .git/
directory and files listed in .gitignore
.
Add a command called Git-ArchiveProject
to your Windows PowerShell Profile
.
When you use this command in a project directory, it packages the entire project and outputs a file named yyyyMMdd-projectName.zip
in the parent directory.
|
|
If you’re using Linux, you can write a similar script in Bash.
What is Git Archive?
git archive
is a command provided by Git to create source code archive files (like .zip or .tar) from a Git repository while automatically excluding version control information. It’s primarily used for:
- Creating source code packages for releases
- Distributing project code to users who don’t need access to version history
- Deploying applications to server environments
The main feature of git archive
is that it only packages files committed to the repository and automatically excludes the .git directory and files specified in .gitignore.
Basic Usage
Creating ZIP Format Archives
|
|
Creating Compressed TAR Files
|
|
Key Parameters Explained
1. Format Specification: --format
|
|
Supported formats primarily include zip
and tar
. If not specified, Git will guess the format based on the output filename.
2. Output Location: --output
|
|
Specifies the save path and filename for the archive file.
3. Adding a Prefix: --prefix
|
|
Adds a directory prefix to all files in the archive, creating a top-level directory.
4. Version Specification
|
|
5. Directory-Limited Archiving
|
|
Archives only a specific subdirectory of the repository.
6. Remote Repository: --remote
|
|
Creates an archive from a remote repository (requires remote server support).
Archive File Scope Explanation
Understanding what files git archive
includes is important:
- Committed files: Will be archived (latest committed version)
- Modified but uncommitted files: Will NOT be archived!
- Staged but uncommitted files: Will NOT be archived!
- Untracked files: Will NOT be archived!
- Ignored files: Will NOT be archived!
Special note: git archive
cannot include the .git directory by design, with the purpose of creating clean archives without version control information.
Comparison with Other Packaging Methods
Compared to manual packaging with tools like 7-Zip or WinRAR, git archive
offers these advantages:
- Automatic exclusion of non-tracked files: No need to manually specify exclusion rules
- Follows .gitignore rules: Maintains consistency with version control
- Excludes the .git directory: Prevents leakage of version control information
- Can specify any historical version: Makes it easy to create archives of specific versions
Conclusion
Not every client uses Git, and many private projects cannot be conveniently uploaded online.
For small projects, zip archives are the most direct delivery method.
Previously, I would manually select files for packaging, which was inefficient.
Later, I used 7z’s exclude command parameters to exclude some files, but that was still a bit cumbersome.
Since discovering the git archive
command, project delivery has become much more convenient.
Author Xianmin
LastMod 2025-04-13
License 原创文章,如需转载请注明文章作者和出处。谢谢!