Writing a blob object to the database

In this example, we'll see how we can use plumbing commands to add blob objects to the database. This is, of course, used internally by the git add command; however, this can also be useful if you, for example, need to add the public part of your GPG key to the Git repository so that signed tags can be verified. You can then, after you've added the key, tag the blob ID so that the other committers can find it.

Getting ready

We'll create and use a new repository for this example and the next couple of examples. Let's create a new repository in the myplumbing folder:

$ git init myplumbing
Initialized empty Git repository in /path/to/myplumbing/.git/
$ cd myplumbing
$ git status
On branch master

Initial commit

nothing to commit (create/copy files and use "git add" to track)

How to do it...

Git uses the hash-object plumbing command to write objects to its database. We can use it to update the database with the content of files, or pass the content directly on stdin. First, let's just see what is currently stored in the database (.git/objects) using the following command:

$ find .git/objects
.git/objects
.git/objects/info
.git/objects/pack
$ find .git/objects -type f

The database is empty as expected when we have just created the repository. Now, we can write an object to the database:

echo 'This is the content of my file' | git hash-object -w --stdin
70bacd9f51c26d602f474bbdc9f60644aa449e97

We can also try to use the hash-object command on a regular file:

$ echo 'This content is good' > mytest.txt
$ git hash-object -w mytest.txt
926e8ffd3258ed6edd1e254438f02fd24e417acc

We can update the file and write the new content to the database:

$ echo 'This content is better' > mytest.txt
$ git hash-object -w mytest.txt
6b3da706d14c3820597ec7109f163bc144dcbb22

How it works…

The hash-object function will create a SHA-1 hash of the input given and if the -w switch is used, write it to the database. The command defaults to blob objects. We can investigate the contents of the database using the following command:

find .git/objects -type f
.git/objects/6b/3da706d14c3820597ec7109f163bc144dcbb22
.git/objects/70/bacd9f51c26d602f474bbdc9f60644aa449e97
.git/objects/92/6e8ffd3258ed6edd1e254438f02fd24e417acc

We can see that the database contains the three objects we just created. As you can see, Git stores each object as a file where the two first digits of the SHA-1 are used as a subdirectory and the remaining 38 objects as the filename. To check the object in Git's database, we can use the cat-file command, just like we did in Chapter 1, Navigating Git. To check the contents of an object, we use the -p (pretty print) switch:

git cat-file -p 70bacd9f51c26d602f474bbdc9f60644aa449e97
This is the content of my file

We can also use the cat-file command to check the type of an object:

git cat-file -t 926e8ffd3258ed6edd1e254438f02fd24e417acc
blob

There's more…

We can use the cat-file command to update files in our working directory, so to revert mytest.txt to the first version, use the following command:

$ git cat-file –p 926e8ffd3258ed6edd1e254438f02fd24e417acc > mytest.txt
$ cat mytest.txt
This content is good
..................Content has been hidden....................

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