Create a backup or a bundle of your git repository

Introduction

Keeping a backup or moving around a local repository while retaining its entire versioning information can be achieved with the current utilities of git.

Bundles are a great way to backup entire Git repositories. They also let you share changes without a network connection.

Materials and Methods

We will use the git bundle utility inside our local repository and also git merge to deploy our bundled repo.

git bundle turns a repository into a single file that retains the versioning information of the entire project.

Here are a list of options that are available:

git bundle create <name-of-the-bundle> <git-rev-list-args>
git bundle verify <file>
git bundle unbundle <file> [<refname>...]

Create a bundle

While you are inside a repo run:

git bundle create ../myrepo.bundle master

It should print out something like this:

Counting objects: 43, done.
Delta compression using up to 2 threads.
Compressing objects: 100% (39/39), done.
Writing objects: 100% (43/43), 54.44 KiB | 0 bytes/s, done.
Total 43 (delta 8), reused 0 (delta 0)

This will create the bundle outside (one folder above) the repo and name itmyrepo.bundle. When we created the file It was like we where just pushing our master branch to a remote, except it’s contained in a file instead of a remote repository.

Verify the bundle

Before we move our bundle its a good idea to verify its content and check that the bundle file is valid and relevant to the current repository.

While you are inside a repo run:

git bundle verify ../myrepo.bundle

It should print out something like this:

The bundle contains this ref:
230b04bdd3367b2db73 refs/heads/master
The bundle records a complete history.
../myrepo.bundle is okay

Deploy the bundle

Assuming that you have the myrepo.bundle file in /home/user and you have created a folder newrepo and initialized a git repository inside it. Here is a generic procedure:

mkdir newrepo
cd newrepo
git init
git bundle unbundle ../myrepo.bundle

the last command will inform you with the HEAD of you repo

f7243ba54eb7de4b76a0 HEAD

Now you can merge the contents of myrepo.bubdle file with the one that you initialized inside the newrepo folder:

git merge f7243b

Alternative procedures are also available, so please follow the links provided on theReferences section

Results and discussion

As you can see once you create the bundle file you can easily move it to another folder on the same or an other machine. Assume you want to transfer the history from a repository R1 on machine A to another repository R2 on machine B. For whatever reason, direct connection between A and B is not allowed, but we can move data from A to B via some mechanism (USB flash drive, email, etc..). This way we can update R2 with development made on the branch master in R1.

References

  1. Ryan Hodson, Git Tips & Tricks : rypress.com
  2. Scott Chacon and Ben Straub, Pro Git : git-scm.com
Advertisement
Categories: Tags: , ,

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.