How Git Works: From git add to git push, Step by Step

Diagram of how Git works: files move from working directory to staging, into a commit, then pushed to GitHub

How Git works comes down to three ideas: every commit is a full snapshot of your project, every object is named by a hash of its content, and branches are tiny pointers to commits. That is the whole model. Once you can picture it, git add, git commit and git push stop feeling like magic spells and start behaving predictably.

This guide opens the hood. We'll cover why Git exists, what lives inside the .git folder, and the exact path your code takes from your editor to GitHub. Then we'll walk through the three mistakes that turn an ordinary workday into an incident, and how to avoid each one.

TL;DR
  • Git is distributed: every clone holds the complete project history.
  • Each commit stores a snapshot of the whole project, not a list of edits.
  • Everything is built from blobs, trees and commits, each named by its SHA-1 hash.
  • A branch is a small file holding one commit hash, and HEAD points to your current branch.
  • git add stages a snapshot, git commit freezes it locally, and git push sends only the missing objects.
  1. 0:00Intro
  2. 0:24Git feels scary for a reason
  3. 1:10Every clone holds everything
  4. 1:53Built in weeks, born 2005
  5. 2:38Snapshots, not diffs
  6. 3:19Blobs, trees and commits
  7. 4:02Named by its fingerprint
  8. 4:49A branch is a pointer
  9. 5:34Stage it in the index
  10. 6:19Commit freezes the snapshot
  11. 6:58Push: share your commits
  12. 7:41Git runs the world
  13. 8:12One bug fix, start to finish
  14. 8:42Three traps that burn teams
  15. 9:30The whole model, one slide
  16. 9:58Master the model, not the commands

Why does Git feel so confusing?

Git feels scary when you can't see what it's doing. Most developers learn it as a list of commands to memorize. That works until something unusual happens: a merge conflict right before a demo, a detached HEAD warning you've never seen, or the sinking thought that you just erased your team's work.

Memorized commands are fragile. A fix copied from a forum works, until your situation is slightly different and it quietly makes things worse. The real problem is an invisible model. Git does something simple underneath, but if nobody showed you the picture, every error looks random. The cure is to learn a handful of ideas: snapshots, objects and pointers.

  • Memorized commands break in unusual situations.
  • You can't fix what you can't picture.
  • A few core ideas make every command make sense.

What is Git, and why was it created?

Git is a distributed version control system, and distributed is the key word. There is no single master copy. Your laptop holds a complete repository. So does your teammate's machine, and so does the CI server that clones it to run tests. They all sync through a shared remote such as GitHub.

That design pays off daily. Browsing old commits never waits on a server. On a plane or bad Wi-Fi, you can still commit, branch and read history, then sync later. And there's no single point of failure: if the server dies, any clone can rebuild it, because every copy is the whole thing.

Git was born in a hurry. The Linux kernel used a tool called BitKeeper, then access was lost and the project needed a replacement fast. Linus Torvalds wrote Git and had it working within weeks in 2005. Built for a huge codebase with contributors worldwide, it was designed for speed and integrity. Those two goals shaped every choice that follows.

  • Full history on your disk
  • Works offline
  • Any clone can restore a lost server

Does Git store diffs or snapshots?

Snapshots. This is the idea that changes everything. Most people assume Git saves changes. It doesn't. Each commit records the state of the whole project. Diff-based tools store edits line by line and rebuild an old version by replaying every change in order, which gets slower the further back you go.

Git loads any commit directly. Nothing gets replayed. So why doesn't storing whole snapshots fill your disk? Because unchanged files are reused. If a file didn't change, the new snapshot simply points to the existing copy. Same file, same object, no duplicate. That keeps snapshots cheap and makes switching branches or checking out old versions feel instant.

  • Diffs: rebuild versions by replaying edits
  • Snapshots: any version loads directly
  • Unchanged files are stored once and reused

Blobs, trees and commits: what's inside .git?

Git's entire database is built from three kinds of objects. A blob holds the raw contents of a file. It doesn't even store the filename. A tree is a directory listing that maps each filename to a blob and each subfolder to another tree. A commit points to the root tree and records the parent commit, the author and the message.

Picture one commit. It points to a tree. That tree lists the folder and points to a README blob and an app.js blob. Every link is just a hash. Chain each commit to its parent and you get your history.

Those hashes are fingerprints. Take a file containing hello. Git adds a header, giving blob 6\0hello\n, then runs SHA-1 to get ce013625030ba8db and so on, forty hex characters in total. The object is stored in .git/objects/ce, named by the rest of the hash. Same content produces the same hash on every machine, so identical files are stored once.

Fingerprints also make history tamper-evident. Change one byte in a blob and its hash changes. The tree that lists it changes too, and so does the commit above it. History can't be quietly edited.

  • Blob: file contents, no name or path
  • Tree: names mapped to blobs and subtrees
  • Commit: root tree, parent, author, message

What is a Git branch, really?

Once commits have hashes, branches become almost funny. A branch is just a name for one commit hash. Creating one writes a single tiny file. Nothing is copied and no folders are duplicated. That's why making a branch is nearly instant, even in an enormous repository.

You can prove it in the terminal. Create a branch, then read its file in .git/refs/heads: it contains one commit hash. Read .git/HEAD and it says ref: refs/heads/feature. HEAD is your you-are-here marker. It points to your current branch, and when you commit, that branch moves forward. Deleting a branch just removes a pointer, so make one for every idea.

  • A branch stores one commit hash
  • HEAD points to the branch you're on
  • Branch freely, delete freely
git switch -c feature
cat .git/refs/heads/feature
cat .git/HEAD

What does git add do? The staging area explained

Staging is the step most people skip past, but it's where you take control. Your working directory is your sandbox: the real files in your editor. Git watches them but records nothing yet. Run git status and app.js shows as modified but not staged. Run git add app.js, check again, and it's listed under changes to be committed.

The staging area, also called the index, is the draft of your next snapshot. When you run git add, Git already writes a blob for the file and records it in the index. Why bother with this middle step? Because you choose exactly what goes in. Fixed a bug and tweaked a config? Stage them separately and get two clean commits.

  • Working directory: where you edit
  • Staging area (index): the draft of your next commit
  • Stage related changes together, unrelated ones apart
git status
git add app.js
git status

What happens when you run git commit and git push?

git commit freezes the snapshot in three quick moves. You type git commit -m "Fix login". Git builds a tree from the index, writes a commit pointing at that tree, then moves your branch forward. The result might be commit a1b2c3d with parent 9fceb02. That parent link is what makes history: a chain you can walk back to the very first commit.

Here's what surprises many people: committing is entirely local. Nothing has left your machine, and your teammates see nothing until you push.

git push is a short conversation between two repositories. You run git push origin main. GitHub replies that main is at 9fceb02. Your laptop compares, sends a pack with the one new commit and its objects, and GitHub answers that main is now a1b2c3d. Because objects are named by hash, both sides know instantly what already exists, so only the missing pieces travel.

If the push is rejected, someone pushed first, and your push would drop their commits. Git refuses to do that. Pull, integrate their work, then push again. Don't reach for force.

  • Commit: build tree, write commit, move branch
  • Commits stay local until you push
  • Push sends only objects the remote lacks

How teams use Git: one bug fix, start to finish

Git is now the default across the industry. It runs the Linux kernel it was built for, it's the foundation of hosting platforms like GitHub and GitLab, and it powers company codebases and open source projects of every size. It won because it's fast, works offline, costs nothing, and every clone is a backup. Once hosting platforms made sharing easy, it became the standard.

Here's how a real login fix moves through that model. Clone the repo and you get the full history. Create a branch called fix-login, which is just a fresh pointer. Edit app.js, stage it with git add, snapshot it with git commit, and share the branch with git push. Open a pull request so a teammate can review it, then merge to main and the fix ships.

  • Clone → new branch → edit app.js
  • git add → git commit → git push
  • Pull request → merge to main

Common Git mistakes that burn teams

Git protects your history unless you tell it not to. The classic disaster starts with git push --force on main. Force-pushing a shared branch replaces the remote branch with yours and wipes commits others pushed. If you must force, use --force-with-lease, which refuses when the remote has moved since you last fetched.

Committing secrets is worse than it looks. Deleting the file later doesn't help, because the blob is still in history. Use .gitignore to keep sensitive files out, and rotate any leaked key immediately.

Rewriting public history, by rebasing or resetting commits others have already pulled, changes their hashes and breaks everyone's clones. On shared branches, use git revert instead. It records a new commit that undoes the change, and the existing history stays intact.

  • Prefer --force-with-lease over --force
  • Pushed secrets live in history, so rotate them
  • Use git revert, not reset, on shared commits

The bottom line

  • Commits are snapshots, not diffs.
  • Blobs, trees and commits are named by their hash.
  • Branches are pointers to commits.
  • The flow is edit, add, commit, push.
  • Every clone holds the full history.
  • Never rewrite shared history.

Quick-fire questions

Is git commit the same as saving to GitHub?

No. A commit is stored only in your local repository. Your teammates see nothing until you run git push, which sends the new commits to the remote.

Why do I need git add before git commit?

git add puts changes into the staging area, the draft of your next snapshot. This lets you choose exactly what goes into each commit, so unrelated changes can become separate, clean commits.

Why is creating a branch in Git so fast?

A branch is a small file that holds one commit hash. Creating it copies no code, so it's nearly instant even in a huge repository.

Why was my git push rejected?

Usually someone else pushed to the branch first, and your push would drop their commits. Pull, integrate their work, then push again instead of forcing.

If I delete a file with a password in it, is the secret gone?

No. The file's blob still exists in earlier commits, so the secret remains in history. Rotate the leaked key immediately and use .gitignore to keep secrets out in the future.

What's the difference between git revert and git reset on shared branches?

Resetting or rebasing commits others have pulled changes their hashes and breaks teammates' clones. git revert adds a new commit that undoes the change and leaves the shared history intact.

Watch the full video on YouTube →

Souy Soeng

Souy Soeng

Hi there 👋, I’m Soeng Souy (StarCode Kh)
-------------------------------------------
🌱 I’m currently creating a sample Laravel and React Vue Livewire
👯 I’m looking to collaborate on open-source PHP & JavaScript projects
💬 Ask me about Laravel, MySQL, or Flutter
⚡ Fun fact: I love turning ☕️ into code!

Post a Comment

CAN FEEDBACK
Ad