0
0
Gitdevops~5 mins

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

Choose your learning style9 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.