2010-03-31

"Abusing" git storage

A git repository is primarily intended to store multiple branches of a single program or component. The underlying system however is much more flexible. Here are two ways to add files which are related to the project but outside the normal history.

Storing a single file all by itself

This intermediate trick allows you to store a single file into a git repository without the file being in any of the branches.

First of all, let's create a new file:
echo "Hi" > message.txt
and store it in git:
git hash-object -w message.txt
b14df6442ea5a1b382985a6549b85d435376c351
At this stage, the file is stored within the git repository but there is no other way to get to it other than using the hash:
git cat-file blob b14df6442ea5a1b382985a6549b85d435376c351
Hi
A good way to point to the stored contents is to use a tag:
git tag message.txt b14df6442ea5a1b382985a6549b85d435376c351
Now you can access your file this way:
git cat-file blob message.txt
Hi

Creating an empty branch

As seen in this git screencast or in the Git Community Book, you can create a branch without a parent or any initial contents:
git symbolic-ref HEAD refs/heads/foo
rm .git/index
git clean -fdx
To finish it off, add what you were planning on storing there and commit it:
git add myfile
git commit -m 'Initial commit'