Git Bundle Tutorial
Updates:
- 2026-11-24: Add the Mermaid chart and more use cases.
What is the git bundle command
git bundle is a relatively less commonly used git command. Its purpose is to package a git repo into a bundle file, which can then be used by others to recreate the original git repo. This bundle file contains git objects and refs, plus the git history you selected.
The typical use case is copying a git repo offline. Let’s assume you have two machines
- Host A: The main machine you’re working on, which has access to the git server.
- Host B: This sits in an isolated environment and has no access to the git server used by Host A.
Let’s assume that there is no SSH tunnel between these hosts, as shown by this Mermaid chart.
flowchart TB
subgraph Machines
direction LR
ha(Host A) x--x|ssh| hb(Host B)
end
ha ---> gita
hb ---x gita
gita(Git Server)
In this scenario, how would you send the git repo from Host A to Host B including git history? Here comes the git bundle command :)
Now that we see where the git bundle fits, we should know why it’s barely used. However, I did meet this kind of use case in my work :(
Why not just use tar or other package tools?
That brings us to the next question: why not just use tar or any other packaging command? These packaging tools can handle virtually any packaging task and can customize to your needs.
In my opinion, the main reason to use git bundle is that it knows the git semantics. The tar command works file/directory level, which has the following drawbacks.
- It does not respect the
.gitignorefile: the git repo usually has a.gitignorefile which states the files and directories to ignore (e.g., the.venv,node_modules, or any binary files). Although thetarcommand provides us with the--excludeoption, it is tedious to repeat each file and directory to ignore. The.tarignorealleviates this burden, but it requires maintaining a separate file. - It does not know the git objects and git history: you cannot make an incremental package by specifying the git commit range (e.g.
commit1..commit2) in thetarcommand
The git bundle solves these problems because it’s deeply integrated into the git ecosystem.
Usage
Now let’s dive into the git bundle command. I found that enumerating each use case is meaningless because we humans have poor memory. Let’s focus on these three typical use cases.
Use Case 1: full backup
Host A: Run the following command to package the whole git repo
$ cd some-git-repo
$ git bundle create repo.bundle
Host B: Use git clone to unpack the bundle file and restore the git repo.
$ git clone repo.bundle target-dir
At this point, if you check the remote repo info of this target-dir repo, you will notice that its remote repository has been set to the repo.bundle file.
$ cd target-dir
$ git remote -v
# output:
# origin <path_to_repo.bundle> (fetch)
# origin <path_to_repo.bundle> (push)
Use Case 2: Incremental Update (Same Branch)
Host A: Let’s assume that the developer working on Host A has added more commits, as shown below
* 9ac69b0 (HEAD -> main) add 5.txt -- new commit
* 0350a1e add 4.txt -- new commit
* 21486d5 add 3.txt
* a051186 add 2.txt
* 2820a6c add 1.txt
The developer needs to bundle the two newly added commits and transfer them to Host B. Git bundles support specifying a commit range when creating a bundle file, allowing us to include exactly the commits we want to transfer.
$ git bundle create increment.bundle 21486d5..main
Host B: After retrieving the increment.bundle file, we can use the git fetch and git merge commands to get the incremental commits (assume that the code on Host B has not diverged)
$ cd target-dir
$ git fetch increment.bundle main
$ git merge --ff-only FETCH_HEAD
Use Case 3: Incremental Updates (New Branch)
Host A: Similar to use case 2, but this time the developer has created new commits on a new branch called feature_a
$ git bundle create feature_a.bundle feature_a
Host B
$ cd target-dir
$ git fetch feature_a.bundle feature_a:feature_a
Note the feature_a:feature_a here; it has the <source>:<target> form. In this case, we ask git fetch to retrieve commits from the feature_a.bundle and sync the commits from branch feature_a (in the bundle file) to the branch feature_a (local git repo). Can we write main:main in use case 2? Of course we can, if we are not on the main branch. Otherwise, you would get an error that says fatal: refusing to fetch into branch ....
Debugging
These 3 use cases should cover the most common usages. However, you may need to debug a bundle file sometimes. The following commands may be helpful.
Check available references in a bundle file
# syntax: git bundle list-heads <BUNDLE_FILE>
$ git bundle list-heads increment.bundle
Verify if a bundle file is applicable
# syntax: git bundle verify <BUNDLE_FILE>
$ git bundle verify increment.bundle
# output:
# The bundle contains this ref:
# 9ac69b08060859bc4b2172a8238cb841846ec5e0 refs/heads/main
# The bundle requires this ref:
# 21486d53326de40678a54159de656714a59b8d09
# The bundle uses this hash algorithm: sha1
# increment.bundle is okay
We can see from the output in this example that this bundle file requires a specific commit 21486d53326de40678a54159de656714a59b8d09 to exist.