The git archive command is a versatile tool used to create a clean, portable snapshot of a Git repository at a specific state (commit, branch, or tag) without including Git history or metadata. This command is beneficial for sharing or distributing project versions, archiving specific commits, or preparing code releases. In this chapter, we will explore everything from the basics of creating archives to more advanced customization options, ensuring a complete understanding of git archive.
git archive
Command?git archive
creates an archive file (e.g., .tar
, .tar.gz
, or .zip
) with all tracked files at a specific commit, branch, or tag..git
directory or history.git archive
?git archive
The general syntax of the
git archive
command is:
git archive [options]
To create a .tar.gz
archive of the current branch:
git archive -o project.tar.gz HEAD
-o project.tar.gz
specifies the output filename.HEAD
points to the latest commit on the current branch.You can create an archive from a specific branch or tag instead of the latest commit.
git archive -o feature_branch.tar.gz feature-branch
git archive -o release_v1.0.tar.gz v1.0
feature-branch
, while the second archives the state at the v1.0
tag. git archive
Git supports different formats when creating archives. The most common formats are:
.zip
format, widely used on various platforms.Use the -
--format
option to specify the archive format.
git archive --format=zip -o project.zip main
--format=zip
specifies the .zip
format for the archive of the main
branch.--prefix
Adding a prefix helps keep files organized within a folder when extracted.
git archive --prefix=project/ -o project_archive.zip main
--prefix=project/
adds a project/
folder in the archive, grouping files inside this directory. .gitattributes
The .gitattributes
file allows you to control what gets included or excluded in an archive.
Create a .gitattributes
file in the repository’s root.
Add specific rules to include or exclude files:
/docs export-ignore
/tests export-ignore
git archive -o filtered_project.zip main
export-ignore
will be excluded, allowing for a clean export of only essential files.To streamline exports, you can set up a Git alias for commonly used archive commands.
git config --global alias.export-main "archive -o main_archive.tar.gz main"
git archive -o main_archive.tar.gz main
, saving time and improving efficiency.You can automate archive creation by using Git hooks. The post-commit
hook is triggered after each commit, allowing for automatic archive generation.
Example post-commit
script in .git/hooks/post-commit
:
#!/bin/bash
git archive -o latest_commit_archive.zip HEAD
latest_commit_archive.zip
with each new commit.
git archive --format=zip -o project.zip main
If files aren’t organized in a single folder after extraction, use the --prefix
option to add a root folder in the archive.
Use naming conventions that specify the project, version, and branch:
project__.tar.gz
.gitattributes
for Consistent Export ConfigurationsUsing .gitattributes
enables consistent exclusions and inclusions across different archives.
The git archive command is an effective tool for creating clean snapshots of a Git repository. By following the detailed steps in this chapter, you can create, customize, and automate archive exports in various formats. This command streamlines sharing and backing up projects while offering options for custom exclusions and organizational prefixes, making it an invaluable part of any Git workflow. Happy coding !❤️