Sub topic 3
What Git Is and Where It Fits
This chapter assumes you can open a terminal, run simple shell commands, and move around files and folders. It also assumes you know why teams track changes to files and the difference between local, centralized, and distributed version control systems (VCS). Here you place Git in that landscape and learn the working model that later chapters build on.
What Git is
Git is a free, open-source, distributed version control system. Linus Torvalds created it in 2005 to manage the Linux kernel's source code. The kernel needed a tool that was fast, handled thousands of contributors, and worked without a single central server.
Three properties define Git in daily use:
- Distributed. Every clone holds the full project history, not just the latest files.
- Local-first. Most commands (commit, log, diff, branch) run on your machine and need no network.
- Snapshot-based. A commit records the state of your project at one moment in time.
Where Git fits
Git is a tool that runs on your computer. It is not a hosting service. Services such as GitHub, GitLab, and Bitbucket host Git repositories and add features like pull requests and issue tracking. You can use Git for years without any of them.
| Layer | Examples | Role |
|---|---|---|
| Version control tool | Git | Records history on your machine |
| Hosting service | GitHub, GitLab, Bitbucket | Shares repositories and adds collaboration features |
| Interface | Command line, editor integrations, GUI clients | Lets you drive Git |
This course uses the command line. Editors and GUI clients call the same underlying Git commands, so what you learn there carries over.
How Git thinks: snapshots
Many older systems store each file as a base version plus a list of changes to it. Git instead thinks in snapshots. Each commit records what every tracked file looked like at that moment. To save space, Git does not copy files that did not change. It points to the identical stored content from an earlier commit.
Each commit also stores:
- the author's name and email
- a timestamp
- a message describing the change
- a reference to its parent commit
That parent link chains commits into a history. Each commit is identified by a hash, a long hexadecimal string such as a3f9c1e, computed from its contents. Because the hash depends on the contents and the parent, a commit cannot be silently altered without changing its identifier.
The three areas
Git moves your work through three areas. Knowing them explains most of Git's behavior, including why git add exists.
- Working tree. The files you see and edit on disk.
- Staging area (also called the index). A holding area where you assemble what goes into the next commit.
- Repository. The
.gitdirectory, where Git stores committed snapshots.
The staging area lets you choose exactly which changes belong together. If you edited five files but only two belong to one logical change, you stage those two and commit them. The other three stay in your working tree for a later commit.
A first look at the model
You do not need to memorize commands yet. A later chapter walks through your first commit step by step. The short session below shows the three areas in action, assuming Git is already installed.
mkdir demo && cd demo
git init
echo "Hello, Git" > hello.txt
git status
git add hello.txt
git statusThe first git status lists hello.txt as untracked, meaning it exists in the working tree only. After git add, the second git status lists it under changes to be committed, meaning it is in the staging area. Nothing is in the repository's history until you run git commit.
Common misunderstandings
- "Git and GitHub are the same thing." They are not. Git is the tool. GitHub is one of several services that host Git repositories.
- "Git stores differences between versions." Conceptually it stores snapshots. Git does use compression internally, but you reason about commits as whole-project states.
- "Committing sends my work to a server." A commit is local. Sharing happens in a separate step that a later chapter covers.
- "Saving a file records it in Git." Saving changes only the working tree. Recording history takes an explicit
git addandgit commit.
Exercise
You edit three files, run git add on one of them, and then run git commit. Which changes does the new commit contain, and where are the other two edits?
Show answer
The commit contains only the one staged file. The other two edits remain in the working tree, unstaged and uncommitted, until you add and commit them later.
Exercise
A teammate says, "I committed my fix, so everyone can see it now." Why might that be wrong, and what does Git's design say about it?
Show answer
A commit is local. Git is distributed and local-first, so the commit exists only in the teammate's own repository until they share it with another repository, for example one on a hosting service.