archiving git repos

created onOctober 25, 2023

There are three ways for creating an archive from a git repo: , , and plain tarballs. Spoiler: as long as you want to archive the local repo in its current state –that is with uncommitted changes– making a tarball from a git repo is not only the most simple way to create an archive from a git repo, but also the best one.

git archive

This is great if you just want to keep the source code of a certain branch without any version control information.

creates a tarball (or zip file) without the directory. Thus, the archive will not contain any version control information like the repo’s history. The archive can include only one branch of the repo.

archive a branch of the local repo

Archive the contents of a certain revision in the currently checked out branch:

git archive -o ../whatever.tar 1783c63

Archive the contents of the latest commit of the currently checked out branch.

git archive -o ../whatever.tar HEAD

Archive the contents of a specific branch (the branch can differ from the currently checked out branch)

git archive -o ../whatever.tar feature-a

archive a branch on a remote repo

git archive -o /path/to/git-archive-donkeys.tar --prefix=donkeys/ --remote=gitea@idoru:sites/donkeys.git master
In tutorials, you often see something like . This is unnecessary verbose, as the output format can be inferred from the output file suffix. You can use the shorthand instead of . Together, this boils down the command above to

git bundle

This is good for making a backup of a repo including all version control information. But it has some drawbacks compared to a plain tarball of an archive.

You can back up a single branch or a whole repo.

To restore a repo from a git bundle, you don’t extract it. You a do a from the bundle.

There are some notable things about git bundles:

  • if you clone from a bundle, upstream is set to the bundle file. If you want the remote origin to point to the same URL like the remote origin of the repo from which the bundle was created, the original remote origin must be restored by hand.
  • you can pull from a bundle, i.e. pull a branch from a bundle into an existing repo. Obviously, you can’t push to a bundle 😁

That said, a bundle has some disadvantages compared to a backup that’s simply a tarball of the repo:

  • untracked files and stashes are not included in the bundle.
  • the remote origin must be restored by hand. Additionally, you have to know the URL of the remote origin.

git bundle from local repo

To create a bundle that includes all branches, from the root of the git repo run:

git bundle create /path/to/git-archive-donkeys.bundle --all

To create a bundle from a single branch, i.e. branch main, run:

git bundle create /path/to/whatever.bundle main

Create a bundle that contains all revisions up to (and including) a specific revision:

git bundle create /path/to/whatever.bundle main 1783c63

git clone from bundle

Restoring a repo from a git bundle is done with :

, i.e.

git clone git-archive-donkeys.bundle donkeys/

archive tarball

This is my favourite way of backing up a git repo.

Archiving a repo by creating a tarball (or zip file) including the directory will create a ‘snapshot’ of the repo that will include:

  • all files, tracked or untracked.
  • all stashes.
  • object files.
  • any temporary editor files and other garbage. You might want to clean the repo before backing it up with tar.

The remote origin remains intact. No git foo is necessary to get started, just unpack the tarball. You might have to remove IDE-specific files and directories like the dir. After that, you are ready to go.

git archive tar from remote

git archive -o /path/to/git-archive-donkeys.tar --prefix=donkeys/ --remote=gitea@idoru:sites/donkeys.git master