Skip to main content

Getting Started with Git — Chapter 1 / 4

Understanding the Git model

Before typing commands, you need to understand how Git works. In this chapter, we'll compare Git to a photographer's notebook. After this chapter, Git will no longer be a black box to you.

Chapter 1 / 4

Step 01 / 16

The analogy: the photographer's notebook

Before diving into technical vocabulary, here is an analogy that will guide you throughout this chapter. Imagine a photographer documenting a construction project:

  • The camera = the Working Directory (your work folder)
  • The selection tray = the Staging Area (you choose which photos to keep)
  • The photo notebook = the Repository (the complete album, the history)
  • A page in the notebook = a Commit (a dated photo with a caption)
  • A bookmark = a Branch (a marker placed on a page)
  • The open page = HEAD (the page you are currently looking at)

The analogy

We will come back to each element as we go through the chapter. For now, keep the big picture in mind: Git is a photographer's notebook that documents each step of your project.

📷 Your camera
Working directory (your working folder)
🗂️ The selection tray
Staging area (files ready to save)
📖 The notebook
Repository (the history of your saves)
📄 A notebook page
A commit (a full snapshot)
🔖 A bookmark
A branch (a pointer to a page)

Step 02 / 16

Why Git exists

In 2005, Linus Torvalds (the creator of Linux) needed a tool to manage the Linux kernel source code. Existing tools like SVN or CVS were centralized: a single server held the history, and if the server went down, everything was lost.

Linus created Git with clear goals: speed, distributed design, support for non-linear development (branches), and the ability to handle very large projects like the Linux kernel.

Concept

Distributed means that every developer has a complete copy of the history on their machine. You can work offline, commit, create branches, and sync later. There is no mandatory "central server".

Warning

Git is not GitHub. Git is the tool (command line) that manages versions. GitHub is a web platform that hosts Git repositories and adds features (pull requests, issues, etc.). You can use Git without GitHub.

Step 03 / 16

Snapshot: the true nature of Git

Concept

Snapshot: a complete photo of all your project files at a given moment.

Most older versioning systems (like SVN) store differences (deltas): "in file X, line 3 was modified". Git works differently: at each commit, Git takes a complete snapshot of all the project files.

SVN stores differences between versions. Git stores complete snapshots of the project at each commit.

Tip

In practice, Git is smart: if a file hasn't changed, it doesn't copy it, it reuses the previous version. Storage is therefore optimized, but conceptually, each commit is indeed a complete snapshot.

Step 04 / 16

The 3 work zones

Git organizes your work into 3 distinct zones. Understanding these zones is fundamental to mastering Git:

  • Working Directory — your work folder, where you edit your files
  • Staging Area (Index) — the preparation zone, where you choose what goes into the next commit
  • Repository (.git) — the complete project history (the commits)
1

Working Directory -> git add -> Staging Area -> git commit -> Repository

The analogy

Going back to the photographer analogy: you take photos (Working Directory), you select the best ones on the tray (Staging Area), then you paste them into the notebook (Repository). You don't paste all the photos, only the ones you chose.

Which command moves a file from the Working Directory to the Staging Area?

Step 05 / 16

Git objects

Under the hood, Git uses 4 types of objects to store everything:

  • Blob — the raw content of a file (without the file name)
  • Tree — a directory: a list of blobs and other trees with their names
  • Commit — a snapshot: points to a tree + contains a message, an author, a date, and a link to the parent commit(s)
  • Tag — a named label placed on a commit (e.g., v1.0)

Visually, a commit points to a root tree, which itself points to blobs (files) and other trees (subdirectories). It is a tree-like structure.

Step 06 / 16

How Git concretely stores data

Let's see concretely how Git transforms your files into objects:

$ echo "Hello" > hello.txt git add hello.txt

# Git creates a blob with the content "Hello"

$ git commit -m "Add hello.txt"

[main abc1234] Add hello.txt
 1 file changed, 1 insertion(+)

You can inspect the created objects with git cat-file:

$ git cat-file -p <blob-hash>

Hello

$ git cat-file -p <tree-hash>

100644 blob ce013625030ba8dba906f756967f9e9ca394464a    hello.txt

$ git cat-file -p <commit-hash>

tree 4b825dc642cb6eb9a060e54bf899d69f7b0fbb15
author Dev <dev@example.com> 1234567890 +0100
committer Dev <dev@example.com> 1234567890 +0100

Add hello.txt

Step 07 / 16

The hash: an object's ID card

Concept

SHA (Secure Hash Algorithm): a unique fingerprint of 40 hexadecimal characters that identifies each Git object.

Every Git object (blob, tree, commit, tag) is identified by a SHA hash. It's like a unique barcode: two different contents always produce two different hashes.

$ git log --oneline

a1b2c3d Add hello.txt
e4f5g6h Initialize project

The first 7 characters are usually enough to uniquely identify a commit. The hash guarantees integrity: if a file is modified, its hash changes, and Git detects it immediately.

Step 08 / 16

Branches: bookmarks

Concept

Branch: a simple pointer (a name) that points to a commit. It is not a copy of the project.

A branch is just a bookmark placed on a commit. Creating a branch doesn't copy anything — it's instant and takes only 41 bytes on disk (the commit hash + a newline).

Visually, branches can be represented as labels placed on commits in the graph. The main branch and the feature branch each point to a commit. HEAD indicates the active branch.

$ cat .git/refs/heads/main

a1b2c3d4e5f6a7b8c9d0e1f2a3b4c5d6e7f8a9b0

That's it! A branch is literally a 40-character file containing the hash of a commit.

“Creating a branch copies the whole project's code.” True or false?

Step 09 / 16

HEAD: the open page

Concept

HEAD: a special pointer that indicates where you are in the history. Usually, HEAD points to a branch.

When you make a commit, HEAD moves forward with the branch. Before the commit, HEAD points to main which points to commit A. After the commit, main advances to commit B, and HEAD follows.

Warning

DETACHED HEAD: if you checkout directly on a commit (not a branch), HEAD is "detached". You can look around, but if you make commits, they won't be attached to any branch and could be lost. To get back to safety: git checkout main.

Step 10 / 16

The Git graph

Concept

DAG (Directed Acyclic Graph): the directed graph without cycles that links commits together. Each commit points to its parent(s).

Commits form a graph: each commit points to its parent (the previous commit). Branches create forks, merges bring them back together. You can visualize the graph with:

$ git log --graph --oneline --all

* d4e5f6g (HEAD -> feature) Add the sidebar
* c3d4e5f Add the header
| * b2c3d4e (main) Fix the footer
|/
* a1b2c3d Initialize project

Step 11 / 16

Local vs Remote

So far, everything is local (on your machine). To collaborate, you sync with a remote repository:

$ git push

# Sends your local commits to the remote repository

$ git pull

remote: Counting objects: 3, done.
remote: Compressing objects: 100% (2/2), done.
From github.com:user/repo
   a1b2c3d..e4f5g6h  main -> origin/main
Updating a1b2c3d..e4f5g6h
Fast-forward
 index.html | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

git pull is actually a shortcut for git fetch (retrieve remote commits) followed by git merge (merge them with your local commits).

Practice

Best practice: always run git pull before you start working. This avoids unnecessary conflicts.

Step 12 / 16

Summary

1

You work in the Working Directory

2

You prepare your changes in the Staging Area (git add)

3

You save a snapshot with a Commit (git commit)

4

Under the hood, Git creates Objects (blob, tree, commit, tag)

5

Branches are pointers to commits

6

Together they form a Graph (DAG) that you can explore

Step 13 / 16

Common mistakes

  • Confusing Git and GitHub — Git is the tool, GitHub is a platform that uses Git
  • Believing a commit stores differences — a commit is a complete snapshot of the project
  • Thinking branches are copies — a branch is a simple pointer (40 characters)
  • Being afraid of DETACHED HEAD — it's not an error, it's just HEAD pointing directly to a commit instead of a branch
  • Ignoring the Staging Area — it exists to let you choose precisely what you commit

Step 14 / 16

Going further

  • Git Glossary — all terms explained simply
  • Git vs GitHub — understand the difference in detail
  • Why learn Git — the concrete reasons
  • Git for beginners — step-by-step guide

Step 15 / 16

Going further (advanced)

  • SHA-1 vs SHA-256 — Git is gradually migrating to SHA-256 for better security
  • Garbage collection — Git automatically cleans up orphaned objects with git gc
  • Pro Git book — the free reference book (git-scm.com/book)
  • Official documentation — git-scm.com/docs

Step 16 / 16

Test your knowledge

Before moving on, check that you understood the concepts. Click the answer that looks right.

Quiz — Chapter 1

1. A Git blob contains...

2. How does Git save your files?

3. What is a Git branch?

4. When HEAD is detached and you make a commit...

Your feedback matters

Help us improve this course by sharing your thoughts.

So we can follow up if needed.