Using a Git bundle

In the last example, we saw how we could create bundles from the existing history that contains a specified range of history. Now, we'll learn to use these bundles either to create a new repository or to add the history to an existing one.

Getting ready

We'll use the same repository and methods as in the last example to create bundles, but we'll recreate them in this example to be able to use them one at a time. First, we'll prepare the repository and the first bundle, as shown in the following commands:

$ rm -rf offline-sharing
$ git clone https://github.com/dvaske/offline-sharing.git
Cloning into 'offline-sharing'...
remote: Counting objects: 32, done.
remote: Compressing objects: 100% (25/25), done.
remote: Total 32 (delta 7), reused 30 (delta 6)
Unpacking objects: 100% (32/32), done.
Checking connectivity... done.
$ cd offline-sharing
$ git checkout master
Branch master set up to track remote branch master from origin by rebasing.
Switched to a new branch 'master'
$ git bundle create myrepo.bundle master
Counting objects: 12, done.
Delta compression using up to 8 threads.
Compressing objects: 100% (11/11), done.
Writing objects: 100% (12/12), 1.88 KiB | 0 bytes/s, done.
Total 12 (delta 1), reused 0 (delta 0) 
$ git tag bundleForOtherRepo master

How to do it…

Now, let's create a new repository from the bundle file we just created. We can do that with the git clone command and by specifying the URL to the remote repository as the path to the bundle. We'll see how to do that in the following code snippet:

$ cd ..
$ git clone -b master offline-sharing/myrepo.bundle offline-other
Cloning into 'offline-other'...
Receiving objects: 100% (12/12), done.
Resolving deltas: 100% (1/1), done.
Checking connectivity... done.

The new repository is created in the offline-other folder. Let's check the history of that repository by using the following command:

$ cd offline-other
$ git log --oneline --decorate --all
1e42a2d (HEAD, origin/master, master) Calculate pi with more digits
ead7de4 Adds checker for prime number
337bfd0 Adds math, pi calculation
7229805 Offline sharing, patch, bundle and archive

The repository contains, as expected, all the history of the master branch in the original repository. We can now create a second bundle, the same as in the previous example, that contains history from the tag we created (bundleForOtherRepo) to the tip of the develop branch:

$ cd ..
$ cd offline-sharing
$ git bundle create myrepo.bundle bundleForOtherRepo..develop
Counting objects: 12, done.
Delta compression using up to 8 threads.
Compressing objects: 100% (9/9), done.
Writing objects: 100% (9/9), 1.47 KiB | 0 bytes/s, done.
Total 9 (delta 2), reused 0 (delta 0)
$ git bundle verify myrepo.bundle
The bundle contains this ref:
c131c8bb2bf8254e46c013bfb33f4a61f9d4b40e refs/heads/develop
The bundle requires this ref:
ead7de45a504ee19cece26daf45d0184296f3fec
myrepo.bundle is okay

As we also saw in the previous example, the bundle requires that the ead7de45a504ee19cece26daf45d0184296f3fec commit exists in the repository we'll use with the bundle. Let's check the repository we created from the first bundle for this commit by using the following command:

$ cd ..
$ cd offline-other
$ git show -s ead7de45a504ee19cece26daf45d0184296f3fec
commit ead7de45a504ee19cece26daf45d0184296f3fec
Author: Aske Olsson <[email protected]>
Date:   Wed Apr 9 21:28:51 2014 +0200

    Adds checker for prime number

The commit exists. Now we can use the new bundle file as it has the same filename and path as the first bundle we created. We can just use git fetch in the offline-other repository as follows:

$ git fetch
Receiving objects: 100% (9/9), done.
Resolving deltas: 100% (2/2), done.
From /path/to/repo/offline-sharing/myrepo.bundle
 * [new branch]      develop    -> origin/develop

We can now checkout, develop, and verify that the history for the develop and master branch matches the one in the original repository:


$ git checkout develop
Branch develop set up to track remote branch develop from origin by rebasing.
Switched to a new branch 'develop'
$ gitk --all

The previous command gives the following output:

How to do it…

There's more…

The bundle is useful to update the history for repositories on machines where the normal transport mechanisms can't be used due to missing network connections between the machines, firewall rules, and so on. There are, of course, other methods than the Git bundle to transport the history to remote machines. A bare repository on a USB stick could also be used, or even plain patches can be applied to the repository. The advantage of the Git bundle is that you don't have to write the entire history to a bare repository each time you need to update a remote, but only the part of history that is missing.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset