What Really Happens Inside Git When You Init, Add, Commit, and Branch?
This article explains the inner workings of Git by detailing what occurs during initialization, adding files, committing changes, managing branches, and interacting with remote repositories, covering the .git directory structure, objects, SHA‑1 hashing, the index, HEAD, refs, and push operations.
Git Init and .git Directory Structure
Running git init creates a .git folder that contains all metadata needed for version control, including HEAD , config , description , hooks , info , objects , and refs .
# left side execution
mkdir git-demo
cd git-demo && git init
rm -rf .git/hooks/*.sample
# right side execution
watch -n 1 -d find ..git Directory Contents
.git/config – local repository configuration file
.git/objects – stores objects (blobs, commits, trees)
.git/info – exclusion information
.git/hooks – default hook scripts
.git/HEAD – pointer to the current branch
.git/refs – pointers to branch heads and tags
tree .git
.git
├── HEAD
├── config
├── description
├── hooks
├── info
│ └── exclude
├── objects
│ ├── info
│ └── pack
└── refs
├── heads
│ ├── dev
│ ├── master
│ └── tmp
└── tagsUnderstanding git add and Blob Objects
When git add is executed, Git creates a blob object that stores the file’s content and type, but not its name. The blob’s SHA‑1 hash is calculated as blob <size>\0<content>. Identical file contents share the same blob.
# left side execution
echo "hello git" > hello.txt
git status
git add hello.txt
# right side execution
watch -n 1 -d find .The resulting object can be inspected:
git cat-file -t 8d0e41 # shows "blob"
git cat-file -p 8d0e41 # shows file content
git cat-file -s 8d0e41 # shows size (10 bytes)Index (Staging Area)
The index file records file names and references to their blob objects. Commands like git ls-files -s display the staged entries.
git ls-files -sCommit Objects and Tree Structure
Executing git commit creates a commit object that references a tree object (the snapshot of the project) and parent commits. The tree object contains entries for files (blobs) and sub‑directories (sub‑trees).
# left side execution
git commit -m "1st commit"
git cat-file -t 6e4a700 # shows "commit"
git cat-file -p 6e4a700 # displays commit metadata
git cat-file -t 64d6ef5 # shows "tree"
git cat-file -p 64d6ef5 # displays tree entriesHEAD and Branch Pointers
HEAD always points to the current branch’s latest commit. Branches are simple pointers stored under .git/refs/heads . Creating a branch with git branch dev adds dev under refs/heads . Switching branches updates HEAD to point to the new branch.
git branch dev
cat .git/refs/heads/dev # shows commit hash
cat .git/HEAD # shows "ref: refs/heads/master"
git checkout dev
cat .git/HEAD # now shows "ref: refs/heads/dev"Remote Repositories and Pushing
To link a local repository with a remote server:
git remote add origin [email protected]:escapelife/git-demo.gitThe remote configuration appears in .git/config under a [remote "origin"] section. Pushing the local master branch to the remote creates corresponding refs under .git/refs/remotes/origin .
git push -u origin masterRemote Tracking Structure
tree .git
├── logs
│ ├── HEAD
│ └── refs
│ ├── heads
│ │ ├── dev
│ │ ├── master
│ │ └── tmp
│ └── remotes
│ └── origin
│ └── master
└── refs
├── heads
│ ├── dev
│ ├── master
│ └── tmp
└── remotes
└── origin
└── masterRecovering Lost Commits
Even after deleting a branch, its commits remain reachable via git reflog, which records all reference updates.
git reflog
git checkout 9fb7a14 # restore a lost commit
git checkout -b devDiff Logic
Git compares the SHA‑1 hashes of blobs in the index versus the working tree to generate diffs. Commands like git diff, git diff --cached, and git diff HEAD show changes at different stages.
echo "hello" > file1.txt
git diff
git diff --cached
git diff HEADSummary of File Lifecycle
Files move between three areas: the working directory, the index (staging area), and the repository (committed snapshots). Git tracks their state using blob objects, tree objects, and commit objects, while branch pointers and HEAD manage the current context.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
MaGe Linux Operations
Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
