Explain the four basic object models in Git's internal structure (Object Model), and how you represent your repository as a Directed Acyclic Graph (DAG)?
Git stores all project data and history in four immutable object types: blobs store raw file content without names or permissions; trees represent directory structure and associate names and permissions with blobs or subtrees; commits record a project-tree snapshot with author, date, message, and parent; and annotated tags name and sign a particular commit. Together, commit relationships form a directed acyclic graph (DAG).
Detailed Technical Explanation & PracticeExpand DetailsCollapse Details
The four basic objects in the .git/objects folder:
- Blob (Binary Large Object):
- Stores binary or textual content only. If you have two files with the same content and different names, Git creates a single
blobobject for them.
- Stores binary or textual content only. If you have two files with the same content and different names, Git creates a single
- Tree:
- Represents the directory. It contains a list of rows that includes: the type of object (
blobortree), the file name, its authority number (such as100644or100755), and its SHA.
- Represents the directory. It contains a list of rows that includes: the type of object (
- Commit:
- A snapshot of time indicating the project’s root tree, an indication of the parent hash, the identity of the author and the modification date.
- Annotated Tag:
- A permanent object denoting a commitment, containing the tag message, signer’s name, and electronic signature.
[ Commit Object ]
├── Parent: a1b2c3d...
├── Author: Mustafa...
└── Tree: 8f9e2b1...
│
[ Tree Object (Root) ]
├── 100644 blob 3e4f... (README.md)
└── 040000 tree d4c5... (src/)
│
[ Tree Object (src/) ]
└── 100644 blob 7a8b... (main.go)Inspect objects directly from the command line:
# Find out the type of object
git cat-file -t <hash>
# Read the content of the object
git cat-file -p <hash>- The belief that Git stores only modification differences (Diffs/Deltas) between commits like legacy VCS systems; Git stores Full Snapshots, reusing the same Blob pointers for unchanged files.
- The belief that the file name is stored inside a Blob; the Blob contains only the raw content, and the file name is stored exclusively in its containing Tree object.
- How can you explore the type and content of any object inside a .git/objects folder using the git cat-file command?
- How does Git ensure that the reference history is safe and has not been tampered with via hash functions (SHA-1 and SHA-256)?
What are the Three States in which a file can exist in Git, and what is the role of the Staging Area (Index)?
The three states are: 1. The Working Directory, where you edit and create files locally; 2. The Staging Area / Index, which is a binary file that records exactly what changes will be made in the next commit; 3. The Git Repository / HEAD, where changes are saved permanently in the commits database. The Staging Area allows the developer to organize and draft small, precise commits without having to record all the modifications to the working directory at once.
Detailed Technical Explanation & PracticeExpand DetailsCollapse Details
The flow of the three cases:
[ Working Directory ] ──(git add)──> [ Staging Area (Index) ] ──(git commit)──> [ Git Repository ]
│ │ │
└─── (git diff) ──────────────────────┘ │
│ │
(git diff HEAD) ─────────────────────────────────┘git diff: Compares the current modifications in Working Directory with what has been prepared in Staging Area.git diff --cached: Compares modifications in Staging Area with the last commit in HEAD.- Preparing specific parts (Interactive Staging):
This allows each code paragraph to be reviewed individually and accepted or postponed to organize commit messages in a professional manner.git add -p file.txt
- The belief that Staging Area is just an imaginary concept; it is a real physical file located in .git/index whose data is updated and stored with every git add command.
- Always using git commit -a to override the Staging Area, committing unwanted test edits to the project history.
- What is the difference between the git diff command and the git diff --cached (or --staged) command?
- How do you prepare only specific parts (Hunks) of the same file for commit but not the rest of the file?