๐ Git Introduction โ
What is Git? โ
Imagine you're writing a novel. By the time you reach Chapter 50, you suddenly feel that the ending you wrote in Chapter 3 was actually better and want to revert โ but you've already changed it 8 times and can't remember what you originally wrote.
If only you had a "time machine" that lets you freely travel between any version... Well, Git is the time machine of the code world.
Git is a Distributed Version Control System (DVCS), created by Linus Torvalds, the father of Linux, in 2005. That's right, the same person who created Linux โ and rumor has it he wrote the first version of Git in just about 10 days. We can only dream of being that brilliant.
๐ก Tip: In simple terms: Git is a "save system" for your code. In a video game, you save your progress at each level โ Git works the same way, except it saves your code and lets you return to any save point at any time.
Why not just "copy folders" to manage versions? โ
I know what you're thinking: why not just copy the folder and name them project_v1, project_v2, project_final, project_final_for_real, project_final_never_touching_again...
Don't laugh, I actually did this back in the day. What happened?
- My hard drive was stuffed with 20 nearly identical folders, running out of space
- Couldn't remember which version changed what
- Want to merge changes from two versions? Manually? Yeah right
- Collaborating with teammates? Sending zip files back and forth? Give me a break
Git elegantly solves all of this.
Centralized vs Distributed โ
Before Git, the mainstream version control systems were centralized, like SVN and CVS. They worked like a class sharing a single notebook:
๐ฆ Centralized Version Control (e.g., SVN)
โโโโโโโโโโโโโโโ
โ Central Server โ โ All version history lives here
โโโโโโโโฌโโโโโโโ
โ Network
โโโโโโผโโโโโ
โ โ โ
โผ โผ โผ
Alice Bob Charlie โ Everyone only has the latest versionThe problem is obvious: server goes down = everyone's work is gone. Network goes down = can't work.
๐ Distributed Version Control (e.g., Git)
โโโโโโโโ โโโโโโโโ โโโโโโโโ
โ Alice โ โ Bob โ โCharlieโ
โ(Full โ โ(Full โ โ(Full โ
โHistory)โโโ โHistory)โโโ โHistory)โ
โโโโโโโโ โโโโโโโโ โโโโโโโโ
Everyone has the full repository! Works offline!๐ Note: Remember this key difference: centralized systems store only "deltas" (differences), while Git stores "snapshots." Each commit is like taking a photograph, recording the complete state of all files at that moment. While this sounds like it takes more space, Git is smart โ unchanged files are stored as pointers, so there's no duplication.
Snapshots vs Deltas โ
| Comparison | Snapshots (Git) | Deltas (SVN/CVS) |
|---|---|---|
| Storage method | Saves complete snapshot per commit | Saves differences from previous version |
| Viewing history | Direct access, extremely fast | Requires rebuilding from the beginning |
| Branch cost | Nearly zero (just creates a pointer) | Needs to copy entire directory |
| Offline work | Fully supported | Not supported |
| Data integrity | SHA-1 hash verification | Relies on server |
Git's Design Philosophy โ
Understanding these philosophies will make Git's command design feel very logical:
- Integrity first โ Git uses SHA-1 hashes to identify everything; no change can go undetected
- Append-only โ Git almost never deletes data; most operations just add new data
- Local-first โ The vast majority of operations happen locally, making them blazing fast
- Three-state design โ Modified, Staged, Committed (covered in detail later)