In Git, creating archives (like tarballs and zip files) is useful for packaging and distributing code snapshots. Archives are convenient for sharing, distributing releases, or backing up the repository at specific stages. This chapter explains how to create tarball (.tar.gz) and zip (.zip) archives in Git, providing readers with a step-by-step approach.
.git
directory and history..tar.gz
): Commonly used in Linux environments; supports high compression..zip
): Widely used in Windows and cross-platform environments.
git --version
git archive
CommandThe git archive
command allows you to create archives easily.
git archive [options]
git archive -o archive.tar.gz main
main
branch in .tar.gz
format.To create a .tar.gz
archive of the latest commit on the current branch:
git archive -o myproject.tar.gz HEAD
-o myproject.tar.gz
specifies the output file’s name.HEAD
denotes the latest commit. Replace with a branch name or tag if needed.To archive a specific branch or tag, use the branch or tag name:
git archive -o myproject_v1.0.tar.gz v1.0
v1.0
tag. Replace v1.0
with the desired tag or branch.To create a compressed tarball (.tar.gz
), add --format
:
git archive --format=tar.gz -o myproject.tar.gz main
--format=tar.gz
option specifies that the archive should be compressed in .tar.gz
format.To create a .zip
archive from the latest commit on the main branch:
git archive -o myproject.zip main
-o myproject.zip
specifies the output filename.main
is the branch to be archived.To create a zip archive with a directory prefix, use the --prefix
option:
git archive --prefix=myproject/ -o myproject.zip main
--prefix=myproject/
adds a myproject
folder to the root of the archive, organizing all files under that directory.You can selectively include certain files or directories in the archive by creating a custom .gitattributes
file:
.gitattributes
to the repository root and specify patterns for inclusion
/src/* export-ignore
/docs/* export-ignore
git archive -o filtered_archive.zip main
export-ignore
patterns will be included in the archive.Using .gitattributes
, you can also specify exclusions. Add export-ignore
to files or folders that shouldn’t appear in the archive:
/tests export-ignore
/README.md export-ignore
export-ignore
won’t be included in the archive, which is useful for omitting test files or documentation.You can automate archive creation whenever you push code by adding a post-commit
hook in .git/hooks/post-commit
.
Example post-commit
script:
#!/bin/bash
git archive -o latest_commit.zip HEAD
latest_commit.zip
with the latest commit’s contents every time a commit is made.To extract a tarball archive, use
tar -xzvf myproject.tar.gz
-x
extracts files, -z
decompresses, -v
shows the process, and -f
specifies the archive file.To extract a zip archive, use:
unzip myproject.zip
myproject.zip
into the current directory.
git archive --format=zip -o myproject.zip main
git branch
or git tag
to confirm.To automate archiving based on tags, you could write a script to create an archive whenever a new tag is pushed:
git archive -o "myproject_$(git describe --tags).zip" $(git describe --tags)
Archiving in Git is a powerful way to distribute, back up, and share snapshots of your codebase. By creating tarballs and zip archives, developers can easily share clean versions of their projects without unnecessary history data. This chapter provided a thorough guide on creating, customizing, and extracting these archives, ensuring you can manage and distribute your repository snapshots effectively. Happy coding !❤️