174

I need to transfer a complete repo to a new non-networked machine, preferable as a single file entity. The git bundle allows a git fetch, git pull style operation in a sneakernet environment but appears to assume that you already have a working version of the repo on the destination machine.

What is the right invocation to:

  1. Bundle all the branches in the current repo
  2. Start up the new repo on the destination directory, i.e. get the root commit correctly installed

I've sent a patch upstream to clarify:

`git clone` can use any bundle created without negative refspecs
(e.g., `new`, but not `old..new`).
If you want to match `git clone --mirror`, which would clone other
refs such as `refs/remotes/*`, use `--all`.
If you want to provide the same set of refs that a clone directly
from the source repository would get, use `--branches --tags` for
the `<git-rev-list-args>`.

So $ git bundle create repo.bundle --branches --tags best matches cloning.

$ git bundle create repo.bundle --all will provide a mirror image of your source machine, including its remote refs.

4 Answers 4

273

What is the right invocation to:

  • Bundle all the branches in the current repo

Simple:

$ git bundle create repo.bundle --all

Here repo.bundle is the name of bundle file you want to create. Note that --all would not include remote-tracking branches... just like ordinary clone wouldn't either.

  • Start up the new repo on the destination directory, i.e. get the root commit correctly installed

First, clone is just init + fetch (+ administrativia).

Second, you can use bundle file everywhere the repository URL can be used, so you can simply clone from a bundle file:

$ git clone repo.bundle

This would create repo as a git repository.

16
  • 11
    Thanks, the --all options isn't in my man page for bundle (I'm looking at version 1.7.6.msysgit.0), nor is the use of the .bundle file in the URLs section for clone. It gives me greater confidence to recommend its use. Aug 3, 2012 at 13:19
  • 27
    The synopsis for create command is git bundle create <file> <git-rev-list-args>. Running man git-rev-list (or man git-log) would give you --all. But I agree that it should be more visible in the bundle command documentation. Aug 3, 2012 at 13:37
  • 2
    @Philip Oakley: The git-rev-list-args is how you select refs that are shown by git show-ref command. Aug 4, 2012 at 17:21
  • 2
    --all did include remote-tracking branches for me (git 2.1.4). I could see refs/remotes/origin/* with git bundle list-heads bundlefile or git ls-remote bundlefile. Although getting them back from the bundle is little tricky.
    – Alex
    Jun 25, 2016 at 20:42
  • 2
    OK, stackoverflow.com/a/48391271/3782797 gives the solution (the issue is actually when "restoring" the repository from the bundle, not creating the bundle): git init ., then git fetch --update-head-ok /path/to/bundle '*:*'. I've checked that the output of git log --all --date=iso-local -m --name-status --decorate=full --graph --date-order is the same.
    – vinc17
    Dec 9, 2020 at 0:28
49

First clone the repository, and include the --mirror option.

git clone --mirror [email protected]:path/repo.git

This ensures all remote branched are also local branches ready for bundeling.

Then run

git bundle create repo.bundle --all as described by the answer from Jakub Narębski

1
  • not sure if it's just me, but after running git bundle create --all - the submodules aren't there
    – Ricky Levi
    May 22 at 10:22
5

I would suggest you tar or zip the .git folder and simply unpack it in the new location and then do git reset --hard HEAD. Everything required for all the branches is under .git and all you should need to do is adjust any remotes in the .git/config file or remove them.

tar cf myrepo.tgz .git
cp myrepo.tgz [USB_STICK]
... move to new machine ...
mkdir myrepo && cd myrepo
tar xpf [USB_STICK]/myrepo.tgz
git reset --hard HEAD
2
  • 5
    One caveat is that you'll need to look at the .git/config file to check if the original repo owner had any user specific stuff in there. Aug 3, 2012 at 9:23
  • @patthoyts: Given that it's disconnected, there'd be no remotes ;-) It does look like bundle is (may be) missing an option, and maybe clone (need to think about cloning from a bundle) Aug 3, 2012 at 9:33
4

With Git 2.34 (Q4 2021), git bundle create is further clarified:

See commit 1d9c8da, commit 0bb92f3, commit 9ab80dd, commit 5c8273d (31 Jul 2021) by Ævar Arnfjörð Bjarmason (avar).
(Merged by Junio C Hamano -- gitster -- in commit f19b275, 24 Aug 2021)

bundle doc: elaborate on object prerequisites

Signed-off-by: Ævar Arnfjörð Bjarmason

Split out the discussion bout "object prerequisites" into its own section, and add some more examples of the common cases.

See 2e0afaf ("Add git-bundle: move objects and references by archive", 2007-02-22, Git v1.5.1-rc1 -- merge) for the introduction of the documentation being changed here.

git bundle now includes in its man page:

OBJECT PREREQUISITES

When creating bundles it is possible to create a self-contained bundle that can be unbundled in a repository with no common history, as well as providing negative revisions to exclude objects needed in the earlier parts of the history.

Feeding a revision such as new to git bundle create will create a bundle file that contains all the objects reachable from the revision new. That bundle can be unbundled in any repository to obtain a full history that leads to the revision new:

$ git bundle create full.bundle new

A revision range such as old..new will produce a bundle file that will require the revision old (and any objects reachable from it) to exist for the bundle to be "unbundle"-able:

$ git bundle create full.bundle old..new

A self-contained bundle without any prerequisites can be extracted into anywhere, even into an empty repository, or be cloned from (i.e., new, but not old..new).

git bundle now includes in its man page:

The 'git bundle verify' command can be used to check whether your recipient repository has the required prerequisite commits for a bundle.

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.