Demystifying Git: From Init to Commit, Branches, and Remote Repos
This article walks through the inner workings of Git, explaining how initializing a repository creates the .git structure, how objects, blobs, and SHA‑1 hashes store file contents, the roles of the working directory, index, and commit objects, and how branches, HEAD, and remote connections operate together to manage version history.
Git is a distributed version‑control system whose design differs fundamentally from other VCSs; understanding its concepts—initialization, object storage, and workflow—makes using it intuitive.
Initializing a Repository
Running git init creates a .git directory containing HEAD, config, description, hooks, info, objects, and refs. The config file holds local settings (e.g., user.name, user.email).
# left side
mkdir git-demo
cd git-demo && git init
rm -rf .git/hooks/*.sample
# right side
watch -n 1 -d find .Git Objects and SHA‑1
When git add is executed, Git creates a blob object that stores the file’s content and type, not its name. The object’s identifier is the SHA‑1 hash of blob <size>\0<content>. Identical file contents share the same blob.
# view object type and content
git cat-file -t 8d0e41
blob
git cat-file -p 8d0e41
hello git
git cat-file -s 8d0e41
10Working Directory, Index, and Commit
The working directory holds actual files. git add copies file contents into the index (a binary .git/index file) and creates blob objects. git status compares the index with the working directory to show changes.
When git commit runs, Git writes a tree object that records the snapshot of the index (file names and blob hashes) and a commit object that references the tree, parent commit(s), author, and message. The HEAD file points to the current branch’s latest commit.
# commit example
git commit -m "1st commit"
git cat-file -t 6e4a700 # commit
git cat-file -p 6e4a700 # shows tree and metadataBranches and HEAD
A branch is a named pointer to a commit (e.g., refs/heads/master). HEAD always points to the current branch; switching branches updates HEAD. Deleting a branch removes only the pointer, not the underlying commits.
# create and switch branch
git branch dev
git checkout dev
cat .git/HEAD # now points to refs/heads/devRemote Repositories
Link a local repo to a remote with git remote add origin <url>. The remote’s configuration appears in .git/config. Pushing with git push -u origin master transfers new objects, creates remote refs (e.g., refs/remotes/origin/master), and updates the remote’s commit history.
# add remote and push
git remote add origin [email protected]:escapelife/git-demo.git
git push -u origin masterRecovering Lost Commits
Even after deleting a branch, its commits remain reachable via git reflog, which records all reference updates.
# find lost commit
git reflog
git checkout <commit-hash>Summary of File Lifecycle
Files move from the working directory → index (staged) → commit (snapshot). Git tracks changes using blobs (content), trees (directory structure), and commits (history), while branches and HEAD manage which snapshot is active.
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.
Open Source Linux
Focused on sharing Linux/Unix content, covering fundamentals, system development, network programming, automation/operations, cloud computing, and related professional knowledge.
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.
