The Wayback Machine - https://web.archive.org/web/20240829095341/https://www.geeksforgeeks.org/git-object-model/
Open In App

Git – Object Model

Last Updated : 12 Jun, 2024
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

Git is a distributed version control system. At the heart of Git’s powerful capabilities lies its object model. Understanding Git’s object model is important for mastering its use and leveraging its full potential. This article will dive deep into the components of the Git object model, explaining how each part functions and contributes to the system’s efficiency.

When we work with git, the first command that we use to initialize our repository to git repository is the git init command. After using this command, we can see that the .git folder is created. Let’s get deep dive into the folder and see what all we have got in the .git folder.

Git, a distributed version control system, has revolutionized how developers manage and track changes in their projects. At the heart of Git’s powerful capabilities lies its object model. Understanding Git’s object model is crucial for mastering its use and leveraging its full potential. This article will dive deep into the components of the Git object model, explaining how each part functions and contributes to the system’s efficiency.

We can see that we have many subdirectories in the .git folder, but the subdirectory that we would have a look at is the .git/objects subdirectory. Generally, the objects folder consists of four different types of objects – blob, tree, commit and tag. 

.git/objects directory is empty as of now but we will see some changes in it and we will understand why this directory is important to us. In Git, every commit, every tree, and every file is saved in the objects folder as a hash value. There is a unique hash value for every object which helps Git to where it is located. The folders are created accordingly. We will learn about hashes as we move forward. Since the objects folder is empty as of now, let’s create a file demo.txt and write “Hello Geeks” in it.

We are using Ubuntu here but Windows users can work with the git bash . Ubuntu Commands can be run on git bash.

Now, let’s add our changes to the staging area and commit our changes by using – 

git add demo.txt 

git commit -m “First Commit”

Let us observe changes in our objects folder after this command.

So we got new directories which are named 2 characters long and have 38 characters long file name.

Note: You might have different hashes in your computers so no need to worry if you don’t get the same hashes.

Git generates a 40-character checksum (SHA-1) hash for every object and the first two characters of that checksum are used as the directory name and the other 38 as a file name.

Therefore the 40 character hashes that we have are –

6d510f79378ca9954e1078a70ff4fdaaa64494fe

7c5d8f39237365acdf66527189ba4fbb6c59b4fc

893f819f4a3b12c76aec907ad636edff48ffcfad

Let us now check what are these 3 different objects and their types. We will use the command – git cat-file 

However , the command that we would be needing are – 

  • git cat-file -t <hash value>  // used for showing type of object
  • git cat-file -p <hash-value> // used for showing content of object

Let us check the type of our objects – 

git cat-file -t <40 character hash value>  //checks the type of the object

So, the types that we have got are – blob, tree, and commit. Let us understand them in more detail.

1. Blob Object

Blob – Binary Large Object

Blob stores the contents of the file. Whenever we commit our files the first type of objects that are created are the blob objects. Let us have a look at the content of our blob object with the help of –

git cat-file -p <blob object hash value> 

We can see that we got the content of our file demo.txt that we wrote initially i.e Hello Geeks.

2. Tree Object

Tree objects contain a list of all files in our repository with a pointer to the blob object assigned to them. Let us have a look at the content of our tree object with the help of –

git cat-file -p <tree object hash value> 

Each line in a Tree object has – file permissions, object type, object hash, and filename. Let us break down the line – 

File Permission (100644) – Permissions of 644 means that the owner of the file has read and write access, while the other users on the system only have read access. blob represents the type of object. 40 character hash represents the hash of the object. You can verify it with your blob hash. demo.txt represents the filename.

3. Commit Object

Git creates a commit object that has a pointer to its tree object. The commit object contains – tree object hash, parent commit hash, author, committer, date, and message. Let us have a look at the content of our commit object with the help of –

git log 

git cat-file -p <commit object hash value> 

As clear from the above image, the commit object consists of – Tree object, Name of the author, Name of the committer, and the commit message. Since we have covered 3 different types of objects, we are left with one i.e Tag Object.

4. Tag Object

A tag object contains an object name, object type, tag name, the name of the person who created the tag, and a message. Let us make an annotated tag using – 

git tag -a “first-tag” -m “Tag For First Commit” 

Let us now check our objects folder to see whether the tag object is created or not.

We can see that a new directory “97” is being added. Let us now verify our type and see the contents of our tag object.

git cat-file -t <tag object hash value>

git cat-file -p <tag object hash value>

We can see that the tag object is pointing to the commit object (verify using our commit hash value) as we expected. I hope you would have found this article useful. We have covered all four object types and you can try more by doing some changes and repeating above the steps again.



Similar Reads

Difference Between Git remote prune, Git prune and Git fetch --prune
Git is a version control system that helps developers manage and track changes in their codebase. Among its many commands, git remote prune, git prune, and git fetch --prune are essential for maintaining a clean and organized repository. This article will explain the differences between these commands, their syntax, uses, and provide examples to il
4 min read
Git - git-show Command Line Utility
Git is a free and open-source distributed version control system designed to handle everything from small to very large projects with speed and efficiency. Git relies on the basis of distributed development of software where more than one developer may have access to the source code of a specific application and can modify changes to it that may be
3 min read
Git LFS: Managing Large Files in Git Repositories
Git, undoubtedly one of the most popular version control systems, is widely used for managing source code in software development projects. However, one of its limitations is its handling of large files. Traditional Git repositories struggle to efficiently manage large files, leading to bloated repositories and slow performance. This is where Git L
4 min read
Git Subtree vs. Git Submodule
Git Subtree and Git Submodule are both mechanisms in Git that allow you to incorporate external repositories into your own repository. They provide a way to manage dependencies and include code from other projects while keeping your repository organized and maintainable. Git Subtree: Git Subtree allows you to insert a separate repository as a subdi
4 min read
Difference Between Git Fetch and Git Pull
Understanding the difference between git fetch and git pull is important for effective version control in Git. These commands are important for managing your repository and collaborating with team members. In this article, Let us look at Git Fetch and Git Pull separately with the help of an example. What is Git Fetch?The Git Fetch command is used t
4 min read
Difference Between Git Push Origin and Git Push Origin Master
Understanding the difference between git push origin and git push origin master is important for efficient version control in Git. These commands are used to upload changes from your local repository to a remote repository, but they function differently. This article will explain these differences, how each command works, and when to use them. What
3 min read
How to Set Up Git Using Git Config?
Git is a powerful version control system that helps developers manage and track changes in their code. Setting up Git correctly is important for maintaining an organized and efficient workflow. In this article, we'll walk you through the process of setting up Git using the git config command. PrerequisitesBefore we dive into the setup process, make
3 min read
How To Exit Git Log or Git Diff?
When working with Git, the git log and git diff commands are important for viewing commit history and changes between commits, respectively. However, if you're new to Git, you might find yourself stuck in these views, unsure how to exit. This article will guide you through the steps to exit git log or git diff, ensuring a smooth experience while na
2 min read
Git - git prune
Git is a powerful version control system that helps developers manage their code efficiently. However, as your project evolves, you may accumulate unnecessary data that clutters your repository. This is where git prune comes in view. What is git prune?git prune is a Git command that removes objects that are no longer referenced by any branch or tag
5 min read
Difference between “git add -A” and “git add”
When working with Git, the git add command is used to move changes from your working directory to the staging area. However, there are different options and flags that can be used with git add, such as -A, which might lead to some confusion. In this article, we'll explore the differences between git add -A and git add. 1. Git add- CommandThe git ad
2 min read
Git - Difference Between Git Revert, Checkout and Reset
Git offers a range of commands to manage and manipulate your codebase. Among these commands, git revert, git checkout, and git reset are frequently used for different purposes. Understanding the differences between these commands is important for effective version control. In this article, we'll explore the functionalities and use cases of git reve
6 min read
How to Fix Git Error "Unable to create '/path/my_project/.git/index.lock'"?
The Git error "Unable to create '/path/my_project/.git/index.lock'" typically occurs when Git is unable to create or write to the index.lock file. This file is used to prevent simultaneous processes from modifying the repository, which could lead to corruption. Table of Content Remove the Lock File ManuallyUse Git Command to Remove the Lock FileRes
2 min read
Difference Between "git commit" and "git push"?
Git commit and git push are two essential commands you'll use a lot when working with Git. even their frequent use together, they have different functions. In order to help you understand when and how to use these two commands effectively in your version control workflow, this article will break down their differences. What is git commit?git commit
2 min read
Mongoose Document Model.prototype.model() API
The Mongoose Document API Model.prototype.model() method of the Mongoose API is used on the Document model. It allows to get the model instance. We can call this method on any model object and by providing another model name to the method parameter we will get the new instance of that model. Let us understand the model() method using an example. Sy
3 min read
Introduction to Model View View Model (MVVM)
Description of Model is as follows: MODEL: ( Reusable Code – DATA ) Business Objects that encapsulate data and behavior of application domain, Simply hold the data. VIEW: ( Platform Specific Code - USER INTERFACE ) What the user sees, The Formatted data. VIEWMODEL: ( Reusable Code – LOGIC ) Link between Model and View OR It Retrieves data from Mode
3 min read
Difference between id and cid properties on a model object in Backbone.js
Backbone.js is a compact library used to organize JavaScript code. An MVC/MV* framework is another term for it. If MVC is unfamiliar to you, it is merely a technique for designing user interfaces. The creation of a program's user interface is made considerably easier by JavaScript functions. BackboneJS provides a variety of building elements to aid
3 min read
Page Object Model (POM)
What is POM? POM is a design pattern which is commonly used in Selenium for Automating the Test Cases. This design pattern can be used with any kind of framework like keyword-driven, Data-driven, hybrid framework, etc. The Page object is an object-oriented class which acts as an interface for the page of your Application under test. Page class cont
3 min read
EmberJS Object Model
An object model refers to the way objects are structured and organized within the framework. Ember.js is an open-source JavaScript framework for building web applications, and it follows the Model-View-Controller (MVC) architectural pattern. In Ember.js, the item model rotates around models, which address the fundamental information of your applica
2 min read
Browser Object Model
Browser Object Model (BOM) is a programming interface JavaScript tool for working with web browsers. This enables access &amp; manipulation of the browser window, frames, and other browser-related objects by facilitating the JavaScript code. The main object is "window," which helps to interact with the browser. By default, you can call window funct
3 min read
What is the DOM (Document Object Model) ?
The DOM, or Document Object Model, is a programming interface for web documents. It represents the structure of a document as a tree of objects, where each object corresponds to a part of the document, such as elements, attributes, and text. The DOM provides a way for programs to manipulate the structure, style, and content of web documents dynamic
2 min read
What is BOM( Browser Object Model) ?
The Browser Object Model (BOM) is a fundamental component of web development. It includes a collection of objects and methods that enable interaction with the browser window. Unlike the Document Object Model (DOM), which focuses on page structure, the BOM enables manipulation of the browser. Central to the BOM is the window object which is a global
1 min read
HTML DOM (Document Object Model)
HTML DOM (Document Object Model) is a hierarchical representation of HTML documents. It defines the structure and properties of elements on a webpage, enabling JavaScript to dynamically access, manipulate, and update content, enhancing interactivity and functionality. Note: It is called a Logical structure because DOM doesn't specify any relationsh
9 min read
How will you access the reference to same object within the object in PHP ?
In this article, we will see how we can access the reference to the same object within that object in PHP. To do that we have to use the "$this" keyword provided by PHP. PHP $this Keyword: It is used to refer to the current objectIt can only be used inside the internal methods of the class.$this keyword can refer to another object only if it is ins
2 min read
HTML | DOM Object Object
The Object object represents only HTML &lt;object&gt; element. We can access any &lt;object&gt; element by using the getElementById(); and also can create object element by using createElement(); method.Syntax: It is used to access Object element document.getElementById("id"); It is used to create object element document.createElement("object"); Pr
2 min read
How to check a JavaScript Object is a DOM Object ?
Prerequisite: DOM (Document object model), Instanceof Operator DOM (Document Object Model): Document Object Model is a hierarchical representation of HTML and XML documents in a format that is easier to interpret in terms of programming. It makes manipulation of tags, elements, attributes, and classes by interpreting its structure in the form of a
2 min read
How to access an object having spaces in the object's key using JavaScript ?
Given an object and the task is to access the object in which the key contains spaces. There are a few methods to solve this problem which are discussed below: Approach 1: Create an object having space-separated keys.Use square bracket notation instead of dot notation to access the property. Example: This example implements the above approach. C/C+
2 min read
How to set an object key inside a state object in React Hooks?
We can update a React hooks state object that has a nested object containing objects with index keys with the following approach, Before doing so, consider the following example: Example: Following is the default state object: const [data, setData] = useState({ name:'', contact:'', address:{ 0:{}, 1:{}, } }) Following is the desired output after up
2 min read
How to check if the provided value is an object created by the Object constructor in JavaScript ?
In this article, we will learn how to check if the provided value is an object created by the Object constructor in JavaScript. Almost all the values in JavaScript are objects except primitive values. There are several methods that can be used to check if the provided value is an object created by the Object constructor in JavaScript. Using the ins
3 min read
How to create a new object from the specified object, where all the keys are in lowercase in JavaScript?
In this article, we will learn how to make a new object from the specified object where all the keys are in lowercase using JavaScript. Example: Here, we have converted the upper case to lower case key values. Input: {RollNo : 1, Mark : 78} Output: {rollno : 1, mark : 78} Approach 1: A simple approach is to Extract keys from Object and LowerCase to
2 min read
How to use array that include and check an object against a property of an object ?
Array.includes() Method: In JavaScript, includes() method is used to determine that a particular element is present in an array or not. It returns true if the element is present and false when it is absent. Syntax: array_name.includes(searchElement, ?fromIndex) Parameters: searchElement: The element to be search in the array.fromIndex: The index fr
3 min read
Article Tags :