Today in Edworking News we want to talk about Many of us create a few Git commits daily, either through GUI tools or the command line. It can be as simple as following these steps: Here, we've used Git high-level commands (also known as Porcelain commands) like git add, and git commit. However, there is another group of Git commands, known as Plumbing commands, that handle the low-level operations. In this blog post, we want to create a Git commit using these low-level operations, and not the git commit command.
Understanding Git Basics
Before diving into low-level commands and crafting a Git commit, we need to understand a few Git basics. Let's start with the states of a file in Git. Files in Git can be in one of these three states: modified, staged, or committed. Similarly, a Git project has three sections: the working directory, the staging area, and the Git directory.
Git Objects: Commits, Trees, and Blobs
A Git commit is a Git object. There are several types of objects in Git, including blob, tree, and commit objects. These objects can be found inside the `.git/objects` folder. If you look inside that folder, you'll see that everything is stored using a SHA-1 hash of the object's content rather than file names. This approach helps Git track all changes to the content of a file or directory and makes it impossible to alter the content without Git detecting it.
Blob Objects
We can store any blob (binary file) inside Git's database, making it a powerful content-addressable file system with a version-control system built on top of it. This can easily be done using one of Git's plumbing commands called `git hash-object`:
```bash
echo "hello world" | git hash-object -w --stdin
The `-w` flag tells Git not only to return the hash of the content passed to it via standard input (`--stdin`) but also to store that content inside the `.git/objects` folder as a blob. Essentially, Git writes a binary file with this content: `blob 11\0hello world`. Git then calculates the SHA-1 hash of this content and stores the file using the hash as the filename.
Tree Objects
Tree objects allow us to store file names for one or more files. You can think of tree objects as representing directories, while blob objects represent file contents. Essentially, a tree is a collection of references to blobs along with their file names, or other trees.
Description: Visual representation of a tree object in Git.
Commit Objects
A Git commit is essentially an object that contains a reference to a Git tree, along with information such as who made the changes (author), when they were made, and why they were made (commit message). A commit can also have zero parents (initial commit), one parent (normal commits), or multiple parents (merge commits).
Craft a Commit, the Hard Way
Now that we understand the Git objects related to a commit and their relationships, we can easily create a commit using low-level Git plumbing commands.
Initialize a new repository:
```bash
git initCreate a blob object:
```bash
echo "hello world" | git hash-object -w --stdinCreate an index with a single entry (our previously created blob):
```bash
git update-index --add --cacheinfo 100644 <blob_hash> hello.txtCreate a tree object from the index:
```bash
git write-treeCreate the commit object:
```bash
echo "Initial commit" | git commit-tree <tree_hash>View the content of the newly created commit:
```bash
git cat-file -p <commit_hash>View the log of the commit:
```bash
git logReset your current branch to point to the newly created commit:
```bash
git reset --hard <commit_hash>
Conclusion
Git has two sets of commands: Porcelain (high-level commands) such as `git add`, `git commit`, `git remote`, etc., and low-level Plumbing commands, which are used by higher-level commands to manipulate Git objects and references. We used these low-level commands to craft a commit by creating its underlying tree and blob objects.
Remember these 3 key ideas for your startup:
Understanding Git Internals: Knowing the low-level operations of Git can give your team a deeper understanding of version control, which can be crucial for resolving complex issues and optimizing workflows.
Enhanced Control: Using plumbing commands allows for more granular control over your repository, enabling custom automation scripts that can streamline your development process.
Cost Efficiency: By mastering Git's low-level commands, you can reduce dependency on third-party tools, saving costs and enhancing security.
Edworking is the best and smartest decision for SMEs and startups to be more productive. Edworking is a FREE superapp of productivity that includes all you need for work powered by AI in the same superapp, connecting Task Management, Docs, Chat, Videocall, and File Management. Save money today by not paying for Slack, Trello, Dropbox, Zoom, and Notion.
For more details, see the original source.