📖 Scenario: You are learning how Git saves your files and changes internally. Git stores data as objects in a special folder. Understanding this helps you see how Git tracks your work.
🎯 Goal: You will create a simple Git repository, add a file, commit it, and then explore how Git stores the file as an object.
📋 What You'll Learn
Create a new Git repository
Add a file with specific content
Commit the file to Git
Use Git commands to find and show the stored object
💡 Why This Matters
🌍 Real World
Understanding Git objects helps you know how your code changes are saved safely and efficiently.
💼 Career
Developers and DevOps engineers use Git daily; knowing its internals helps in debugging and advanced version control.
Progress0 / 4 steps
1
Create a new Git repository
Run the command git init myrepo to create a new Git repository folder called myrepo.
Git
Hint
Use git init followed by the folder name myrepo.
2
Add a file with content
Inside the myrepo folder, create a file named hello.txt with the exact content Hello Git!.
Git
Hint
Use echo "Hello Git!" > hello.txt inside the myrepo folder.
3
Commit the file to Git
Run git add hello.txt and then git commit -m "Add hello.txt" inside the myrepo folder to save the file in Git.
Git
Hint
Use git add hello.txt to stage the file, then git commit -m "Add hello.txt" to commit.
4
Find and show the Git object
Run git hash-object hello.txt to get the object hash, then run git cat-file -p <hash> replacing <hash> with the hash to see the stored content.
Git
Hint
Use git hash-object hello.txt to get the hash, save it in a variable, then use git cat-file -p with that hash.
Practice
(1/5)
1. What type of Git object stores the actual content of a file?
easy
A. Blob
B. Tree
C. Commit
D. Tag
Solution
Step 1: Understand Git object types
Git stores data in objects: blobs for file content, trees for directories, commits for snapshots, and tags for references.
Step 2: Identify the object for file content
The blob object specifically holds the content of files, not metadata or history.
Final Answer:
Blob -> Option A
Quick Check:
File content = Blob [OK]
Hint: Remember: Blob = file content, Tree = folder [OK]
Common Mistakes:
Confusing tree with blob
Thinking commit stores file content
Assuming tag stores file data
2. Which command correctly shows the type of a Git object given its hash abc123?
easy
A. git object-type abc123
B. git show-type abc123
C. git cat-file -t abc123
D. git object -show abc123
Solution
Step 1: Recall Git commands for object info
The command git cat-file is used to inspect Git objects. The -t option shows the object type.
Step 2: Match the correct syntax
git cat-file -t abc123 correctly shows the type of the object with hash abc123. Other options are invalid commands.
Final Answer:
git cat-file -t abc123 -> Option C
Quick Check:
Show object type = git cat-file -t [OK]
Hint: Use git cat-file -t to check object type fast [OK]
Common Mistakes:
Using git show-type which doesn't exist
Confusing git object commands
Forgetting the -t option
3. Given a Git commit object hash def456, what will the command git cat-file -p def456 display?
medium
A. The raw content of a file stored in the commit
B. The list of branches containing the commit
C. The tags pointing to this commit
D. The commit message, author info, and tree hash
Solution
Step 1: Understand what a commit object contains
A commit object stores metadata: author, date, commit message, and a tree hash representing the snapshot of files.
Step 2: Interpret git cat-file -p output for commit
The command git cat-file -p pretty-prints the commit object, showing its message, author info, and tree hash, not raw file content or branch/tag info.
Final Answer:
The commit message, author info, and tree hash -> Option D
Quick Check:
Commit object details = message + author + tree [OK]
Hint: Commit object shows metadata, not file content [OK]
Common Mistakes:
Expecting file content from commit object
Confusing branches or tags with commit data
Thinking commit object lists branches
4. You run git cat-file -p 123abc but get an error: fatal: Not a valid object name 123abc. What is the most likely cause?
medium
A. The command syntax is incorrect
B. The hash 123abc does not exist in the repository
C. You need to add files before using git cat-file
D. The repository is corrupted
Solution
Step 1: Analyze the error message
The error says the object name is invalid, meaning Git cannot find an object with hash 123abc.
Step 2: Check common causes
This usually happens if the hash is wrong or does not exist. The command syntax is correct, and adding files is unrelated. Repository corruption is less common and would cause other errors.
Final Answer:
The hash 123abc does not exist in the repository -> Option B
Quick Check:
Invalid object name = missing hash [OK]
Hint: Check if object hash exists before using git cat-file [OK]
Common Mistakes:
Assuming syntax error instead of missing object
Thinking files must be added before cat-file
Jumping to repository corruption conclusion
5. You want to find the SHA-1 hash of a file's content before committing. Which Git command sequence correctly creates the object and shows its hash without committing?
hard
A. git hash-object -w <file> && git cat-file -p <hash>
B. git commit -m 'temp' && git show <file>
C. git add <file> && git log --hash
D. git init && git status
Solution
Step 1: Understand git hash-object usage
The command git hash-object -w <file> creates a blob object from the file content and writes it to the object database, returning its hash.
Step 2: Verify object content with git cat-file
Using git cat-file -p <hash> shows the content of the created blob object, confirming the hash matches the file content.