Bird
Raised Fist0
Gitdevops~5 mins

How Git stores objects - Step-by-Step CLI Walkthrough

Choose your learning style10 modes available
Introduction
Git saves your project files and history in a special way to keep track of changes efficiently. It stores data as objects that represent files, folders, and commits, making it easy to manage versions and recover past work.
When you want to understand how Git saves your project data behind the scenes
When you need to troubleshoot issues with corrupted Git repositories
When you want to learn how Git tracks changes without saving full copies every time
When you want to explore the .git folder to see how your project history is stored
When you want to manually inspect or recover data from Git objects
Commands
This command creates a new Git repository named 'my-project'. It sets up the .git folder where Git will store all objects and history.
Terminal
git init my-project
Expected OutputExpected
Initialized empty Git repository in /home/user/my-project/.git/
Change directory into the new project folder to start working inside the Git repository.
Terminal
cd my-project
Expected OutputExpected
No output (command runs silently)
Create a new file named file.txt with the content 'Hello Git'. This file will be added to Git next.
Terminal
echo "Hello Git" > file.txt
Expected OutputExpected
No output (command runs silently)
Add the file.txt to the staging area. Git now prepares to save this file as an object in the repository.
Terminal
git add file.txt
Expected OutputExpected
No output (command runs silently)
Save the staged file as a commit object. Git stores the file content as a blob object and records the commit with metadata.
Terminal
git commit -m "Add file.txt with greeting"
Expected OutputExpected
[master (root-commit) abcdef1] Add file.txt with greeting 1 file changed, 1 insertion(+) create mode 100644 file.txt
→
-m - Add a commit message inline
Show the tree object of the latest commit. This lists files and folders stored as objects in Git.
Terminal
git cat-file -p HEAD^{tree}
Expected OutputExpected
100644 blob abcdef2 file.txt
Display the content of the blob object representing file.txt. This shows the actual file content stored by Git.
Terminal
git cat-file -p abcdef2
Expected OutputExpected
Hello Git
Key Concept

If you remember nothing else from this pattern, remember: Git stores your files and history as objects (blobs, trees, commits) inside the .git folder to track changes efficiently.

Common Mistakes
Trying to edit files directly inside the .git/objects folder
Git objects are stored in a compressed and hashed format; manual edits corrupt the repository.
Use Git commands like add, commit, and checkout to manage files safely.
Not adding files before committing
Git only saves files that are staged; unadded files are ignored in commits.
Always run 'git add <filename>' before committing to include changes.
Summary
Initialize a Git repository to create the .git folder where objects are stored.
Add files to the staging area to prepare them for saving as objects.
Commit changes to save files as blob objects and record commits with metadata.
Use git cat-file to inspect Git objects like blobs and trees inside the repository.

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

  1. 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.
  2. Step 2: Identify the object for file content

    The blob object specifically holds the content of files, not metadata or history.
  3. Final Answer:

    Blob -> Option A
  4. 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

  1. 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.
  2. 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.
  3. Final Answer:

    git cat-file -t abc123 -> Option C
  4. 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

  1. 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.
  2. 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.
  3. Final Answer:

    The commit message, author info, and tree hash -> Option D
  4. 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

  1. Step 1: Analyze the error message

    The error says the object name is invalid, meaning Git cannot find an object with hash 123abc.
  2. 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.
  3. Final Answer:

    The hash 123abc does not exist in the repository -> Option B
  4. 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

  1. 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.
  2. 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.
  3. Final Answer:

    git hash-object -w <file> && git cat-file -p <hash> -> Option A
  4. Quick Check:

    hash-object -w creates blob and shows hash [OK]
Hint: Use git hash-object -w to create and get file hash [OK]
Common Mistakes:
  • Using git commit before hashing
  • Confusing git add with hashing
  • Thinking git init shows file hash
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard