How to git bundle a complete repo
GitGit Problem Overview
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:
- Bundle all the branches in the current repo
- 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 it's remote refs.
Git Solutions
Solution 1 - Git
> 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.
Solution 2 - Git
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
Solution 3 - Git
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
Solution 4 - Git
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.