How to archive a Git branch

Kenny DuMez
Kenny DuMez
Graphite software engineer


Note

This guide explains this concept in vanilla Git. For Graphite documentation, see our CLI docs.


Creating an archive of a Git branch can be very useful, especially when you need to preserve the state of a branch at a specific point in time for compliance, backup, or historical reference. This guide will provide a detailed explanation of how to archive a Git branch, using commands and examples that you can follow directly from your terminal.

The git archive command is used to create an archive (like a .zip or .tar file) from a tree or a set of files in a Git repository.

Before archiving, you might want to ensure that your local branch is up-to-date with its remote counterpart. Here's how you can update your local branch:

Terminal
git checkout your-branch-name
git pull origin your-branch-name

Replace your-branch-name with the name of the branch you wish to archive.

To create an archive of your branch, use the following command:

Terminal
git archive --format=zip --output=/path/to/your-archive.zip your-branch-name
  • --format=zip: specifies the format of the archive. Other formats you can use include tar or tar.gz.
  • --output=/path/to/your-archive.zip: specifies the path and filename of the output archive.
  • your-branch-name: is the name of the branch you are archiving.

This command will create a .zip file containing the current snapshot of the branch.

If you want to archive branches that are no longer actively used but you want to keep a historical record of, you can use the same command. First, ensure you have a list of all branches, including those not checked out locally:

Terminal
git fetch --all
git branch -a

Then, you can archive each branch using the same git archive command:

Terminal
git archive --format=zip --output=/path/to/old-branch-archive.zip old-branch-name

Replace old-branch-name with the name of the branch you want to archive.

For further reading on the Git archive command, see the official Git documentation.

Git inspired
Graphite's CLI and VS Code extension make working with Git effortless.
Learn more

Graphite
Git stacked on GitHub

Stacked pull requests are easier to read, easier to write, and easier to manage.
Teams that stack ship better software, faster.

Or install our CLI.
Product Screenshot 1
Product Screenshot 2