Once you have learned the basics of Git it’s a good idea to familiarize yourself with the various Git objects, and how they relate to each other.
- Keys and Values
- What Are The Git Objects?
- Working With Git Objects
Keys and Values
Every object in Git is assigned a key, specifically a SHA-1 hash, and the value is the object itself. You have surely noticed each commit is identified by a long string of characters, for example, this output from a simple git log
:
commit e4b6714cd1f7990ab22ddd6938d7e486464c5c76 (HEAD -> master) Author: Joe Example <[email protected]> Date: Wed Mar 2 11:01:35 2022 -0500 changed test file
However, it is not only commits that receive a SHA-1 hash, but all of the other objects get a hash as well. This is how Git can quickly identify different types of content, and allows you view objects by their hash, if needed.
What Are The Git Objects?
Here are the main Git object types:
- Commits
- Tags
- Files (known as “blobs”)
- Directories (known as “trees”)
Git uses the unique SHA-1 hash assigned to each of these objects for quick and reliable identification. For example, each tag references a commit; which means the hash associated with that tag always references the hash of the commit it references, even if the tag name changes.
Working With Git Objects
For most every day usage, there are some instances in which you might reference an object by its hash.
For example, if you want to checkout a commit you will use the checkout
command you will use the first 5 or so digits of the commit hash.
For referencing branches and tags, you can simply use the name of the branch or tag. However, using the hash will work as well.
But how do you find the hash for a tag? The Git cat-file
command can give you information about different objects. For example, if you wanted to get information about a tag:
git cat-file -p <tag name>
For example, if you have a tag named v1.0
, you can run this command:
git cat-file -p v1.o
And receive an ouput similiar to this:
tree 896d2e35fdc04b2c20d5df395263d95de6af334b parent ca2efb98ca118f735389524e6f487a73eac586d9 author Joe Example <[email protected]> 1646236895 -0500 committer Joe Example <[email protected]> 1646236895 -0500 changed test file