Learning center

Hand over a Git branch as a file when the receiver has no remote

Use git bundle when the receiver cannot reach your remote. Before you send the file, decide whether it should carry the whole history or only the commits the receiver lacks, because a bundle of a range is useless to someone without its base.

What goes wrong#

A range bundle records the commits it assumes the receiver has. Git calls them prerequisites. In this fixture, topic is one commit on top of main, whose last two commits are c2 and c3:

git bundle create topic.bundle main~1..topic
git bundle verify topic.bundle

In a full clone of main, verify prints the ref the bundle contains and the commit it requires, and exits 0. In a clone made with --depth 1, and in an empty repository, it prints:

error: Repository lacks these prerequisite commits:
error: 087208efc4e3006538adc8f4f5d03e8fe84b9026

and exits 1. Cloning straight from that file fails for the same reason, with exit 128. The shallow clone is the case that surprises people: its user has the repository and the branch tip checked out, and still lacks the commit the bundle needs. Git 2.43 produced these results. The git bundle manual describes both forms.

Choose the range#

Receiver hasCreateCost
Nothinggit bundle create all.bundle --allEvery reachable object, so every branch and tag goes with it
Nothing, and needs one branchgit bundle create topic.bundle topicThat branch's whole history
A full clone that holds a commit you can namegit bundle create topic.bundle BASE..topicOnly the new commits
A shallow cloneOne of the first twoA range needs history the clone may not hold

Ask the receiver for git rev-parse origin/main and git rev-parse --is-shallow-repository before you build a range. Two lines of text are cheaper than a second file transfer.

Check --all before it leaves your organization. It carries branches nobody mentioned, and a file deleted in the latest commit is still in the history.

Write the handoff#

  1. The bundle's name and its sha256sum.
  2. The output of git bundle list-heads topic.bundle.
  3. The prerequisite commit, or "self-contained".
  4. The command to import it: git fetch topic.bundle topic:topic.

The receiver runs git bundle verify, fetches, and compares git rev-parse topic with the head you listed. The two match only if they have the same commit, and a commit names its tree and its whole ancestry.

What a verified bundle does not show#

verify checks the format and the prerequisites. It says nothing about files Git does not track, about submodules, whose commits live in another repository, or about Git LFS, where the history holds only pointers. If the project uses either, hand those over separately and name them in the message.

Worked example#

This example is fictional. A contractor's network blocks the code host, so Ana sends fix.bundle, built as main..fix. Verification fails on a prerequisite. The contractor's copy turns out to be a source archive committed into a new repository, so its history shares no commit with Ana's. A bundle cannot help with a range there. Ana sends a self-contained bundle of fix, and the contractor clones from it and rebuilds their two local commits on top as patches.

When you send a few commits for someone to apply on their own base, patches with a recorded base are the smaller handoff. The handoff guide lists the other forms.

One month of free code review.

New organizations get one calendar month of hosted code review for private and public GitHub repositories. No card or cloud connection required. Eligibility and fair-use terms.

Start free with GitHub

Related reading