Now that we have created both blob and tree objects, the next step in the data model is to create the actual commit object.
Again, we'll use the repository created in the previous examples with the different objects written to the database.
As we saw in Chapter 1, Navigating Git, a commit object consists of the author and committer information, a root tree object, a parent commit (except for the first commit), and a commit message. We have the root tree object generated in the last example, and Git will pick up the author and committer information from the configuration. So, all we need to do is create a commit message and write the commit object. We can do this for each of the tree objects we created previously:
$ echo 'Initial commit - Good contents' | git commit-tree 4c4493f8 40f4783c37e7cb9d07a4a71100acf4c474a376b0 $ echo 'Second commit - Better contents' | git commit-tree -p 40f4783 2b969743 991ad244c6fdc84a983543cd8f2e89deca0eff29 $ echo 'Adds a subdirectory' | git commit-tree -p 991ad244 9387b1a7 e89518224a971df09a00242355b62278964d6811
Three commit objects are created. The -p
switch in the latter two commands tells Git to use the commit specified as a parent commit for the one to be created. Git will use the author and committer information it can find through the configuration options. We can verify that the commits were created with the cat-file
command, just like we did with the blobs and trees:
$ git cat-file -p 40f4783c37e7cb9d07a4a71100acf4c474a376b0 tree 4c4493f8029d491d280695e263e24772ab6962ce author Aske Olsson <[email protected]> 1398270736 +0200 committer Aske Olsson <[email protected]> 1398270736 +0200 Initial commit - Good contents $ git cat-file -t 40f4783c37e7cb9d07a4a71100acf4c474a376b0 commit $ git cat-file -p 991ad244c6fdc84a983543cd8f2e89deca0eff29 tree 2b9697438318f3a62a5e85d14a3b52d69b962907 parent 40f4783c37e7cb9d07a4a71100acf4c474a376b0 author Aske Olsson <[email protected]> 1398270736 +0200 committer Aske Olsson <[email protected]> 1398270736 +0200 Second commit - Better contents $ git cat-file -t 991ad244c6fdc84a983543cd8f2e89deca0eff29 commit $ git cat-file -p e89518224a971df09a00242355b62278964d6811 tree 5c23c103aeaa360342f36fe13a673fa473f665b8 parent 991ad244c6fdc84a983543cd8f2e89deca0eff29 author Aske Olsson <[email protected]> 1398270736 +0200 committer Aske Olsson <[email protected]> 1398270736 +0200 Adds a subdirectory $ git cat-file -t e89518224a971df09a00242355b62278964d6811 commit
As we specified, parent commits for the last two commit objects we made we actually created Git's history in the repository, which we can view with the log
command. We'll need to tell the log command to show the history from the latest commit, as we haven't updated any branches to point to it:
$ git log e89518224a971df09a00242355b62278964d6811 commit e89518224a971df09a00242355b62278964d6811 Author: Aske Olsson <[email protected]> Date: Wed Apr 23 18:32:16 2014 +0200 Adds a subdirectory commit 991ad244c6fdc84a983543cd8f2e89deca0eff29 Author: Aske Olsson <[email protected]> Date: Wed Apr 23 18:32:16 2014 +0200 Second commit - Better contents commit 40f4783c37e7cb9d07a4a71100acf4c474a376b0 Author: Aske Olsson <[email protected]> Date: Wed Apr 23 18:32:16 2014 +0200 Initial commit - Good contents