When we created our local repository with the command
git init, we took a project folder that we created and converted it. If you look at the project folder, you will see a folder called .git. Inside this folder is a whole world of structure, called the working git tree!
Barking up the right tree: Git tree structure
As you work more with local repositories, you will come across issues that you may have to fix manually in your local repository folder. It is also important to visualize how your repository looks while you are using Git in case your files and commits don't show up correctly on Git Bash!
How can you gain that control? 🕶
Learn the file structure of Git to become a real Git hacker! The Git file directory is structured like a tree. It starts with a HEAD that points to the latest commit on the working branch. The HEAD will always show you where you are working from on Git Bash.
For example, when you are working on the master branch in your local repository, you can use the command:
git checkout wwbranch
The HEAD will now point to wwbranch as the new working directory. With every commit, it points to the most recent one.
You know what a commit is, but how does it look behind the scenes? Every commit has a Git tree that contains three main types of objects:
What happens behind the scenes after you make a commit?
Blob object- Let's say you add three files to your commit. After the commit is made, these three files are encrypted, compressed, and stored as objects called blobs. A hash algorithm called SHA - 1 is used on each blob to generate a hash identifier for each file.
Tree object - These hash identifiers are put in a list called a tree, the second object type in the tree. There is only one tree object per commit.
Commit object - Lastly, a hash of the tree object is generated using the SHA 1 hash algorithm to put in the commit. This becomes the unique identifier for this commit.
If you look at the diagram below, you should get a good visual of the Git tree of a commit that I just described.
Git file directory
Let's get back to the
.git folder I was telling you about at the beginning of the chapter. We will look at how the directory structure is organized, and where the components of the Git tree fall in to it.
The .git directory holds the meat of your local repo. It holds subdirectories for
HEAD, and more.
The objects folder is where the three objects: commit, tree and blob are housed. Each object gets its own sub folder.
The refs folder holds references to all of the objects in the directory. Let's say you have some orphaned objects on branches that no longer exist. You can go into this folder and prune them off of your branch.
The config folder contains all of your configurations including the credentials you use to connect to GitHub; your email and name that you used to configure your global config.
The HEAD file is what contains the pointer or reference to your working directory or branch and its latest commit.
The index file is for when you use
git addto stage your files for a commit. They leave the index file after the commit.
Beyond these folders, you can also get a visual look at the my FixMe local repo on Git Bash by typing in the following command:
git log --graph --full-history --all --pretty=format:"%h%x09%d%x20%s"
Visualizing is always helpful! You can also use the Gnu32 tree tool to see the file directory in action. Check it out and explore! 🤓
Your Git file structure on the local repository contains your commits.
The commit contains three types of objects which makes up the Git tree
Your local repository's file structure contains a folder for those objects.
Check out more detailed visuals of the Git tree from the Git website.