Why version control

Five copies of one file. Which one is live, and who changed it?

Without version control: copiesdeploy.pydeploy_v2.pydeploy_v2_fixed.pydeploy_final.pydeploy_final_ana.py✗Which one is running in production?✗What changed between two of them?✗Who changed it, and why?With Git: one file, and its history$ git log --format="%h %an %ad %s" --date=shorta37f3fc ana 2026-03-09 Fix timeout: 30 s was too short for cold starts6de83fd ben 2026-03-05 Wait for health check before switching traffic0708ed2 ana 2026-03-02 Add deploy script✓Every saved version is kept, and any one can be brought back✓One line each: who, when, and why in their own words✓Two people can work at once without overwriting each otherVersion control keeps every saved version of a project, with who made it, when and why.Copies of files answer none of the questions on the left. A version control system records each saved state, called acommit, in order. You can compare any two, return to any one, and see who to ask. Git is the system nearly every teamuses; it keeps this history in a hidden folder beside your files. The next page shows what one commit holds.

Version control records every saved state of a project as a commit: who, when and why. Git is the system most teams use.

Commits

Every commit is a complete snapshot of the project

commit A"first version"app.py v1README.md v1commit B"fix the port"parentapp.py v2README.md v1commit C"add a test"parentapp.py v2README.md v1test.py v1Purple: stored by this commit. Grey: unchanged, so the snapshot points at the copy already stored.Every commit is a complete snapshot of the project, and an unchanged file is stored only once.These three snapshots list 7 files between them, yet Git stored only 4 file versions. Each commit alsorecords its parent, author, date and message, and is named by a hash of all of that: 40 characters,usually shortened to 7, such as 9f3c2e1. Change anything and the name changes.

A commit is a snapshot, a parent and a hash that names it.

The three areas

A change passes two gates: add, then commit

Working directorythe files you editStaging areawhat the next commit will holdRepositoryevery commit so farapp.py · editedREADME.md · editedgit addapp.pygit commitcommit Cwith the new app.pyA change passes two gates before it is history: add, then commit.The staging area lets you choose exactly what the next commit holds, so one commit is one logicalchange. README.md was edited too, but it was never added, so commit C holds its previous version.git status shows which files are in which state, and git diff shows the edits themselves.

Working directory, staging area, repository. You choose what moves on.

git status · Going deeper · 1 of 2

git status sorts every file into staged, not staged or untracked

git 2.43, blank lines removed$ git statusOn branch mainChanges to be committed:1 (use "git restore --staged <file>..." to unstage) modified: app.pyChanges not staged for commit:2 (use "git add <file>..." to update what will be committed) (use "git restore <file>..." to discard changes in working directory) modified: README.mdUntracked files:3 (use "git add <file>..." to include in what will be committed) notes.txt1Stagedgoes into the next commit, as it was at git add2Edited, not stagedstays out unless you add it3Untrackeda new file Git has never been told about$ git status -s M README.mdM app.py?? notes.txtgit status sorts every changed file into three lists, and prints the command for each possible move.In the short form the first column is the staging area and the second the working directory; ?? is untracked. Onefile can be in two lists at once: staged with one edit, and edited again since. git diff shows what is not staged;git diff --staged shows exactly what the next commit will hold.

git status shows which gate each file has passed, and the command for the next one.

.gitignore · Going deeper · 2 of 2

.gitignore hides new files; it does not untrack or erase old ones

.gitignore.env__pycache__/*.log/build/!keep.logthis name, in any foldera trailing slash: folders only* matches any run of charactersa leading slash: only at the top! brings a file back.env was committed before the rule was added$ git status -s M .env1$ git rm --cached .envrm '.env'$ git commit -qm "Stop tracking .env"$ git status -s2$ git show HEAD~2:.envKEY=abc12331Already committed, so still trackedthe rule came after the first commit, so Git keeps following the file2Untracked, and now ignoredrm --cached removes it from Git and leaves it on disk; a later edit shows nothing3Still in historyevery earlier commit keeps its copy, for anyone who clones.gitignore only stops new files being added. It does not untrack a file, and it never rewrites history.Treat a secret that was ever committed as leaked: change the key first, tidy the history second. Write .gitignorebefore the first commit. git check-ignore -v app.log names the rule that hides a file. Ignore what is generated orprivate: build output, caches, installed dependencies, logs, .env files.

A secret that was ever committed stays in history. Change the key.

History

History is what you can reach from a label

ABECDmainfeaturegit log mainE B Agit log featureD C B ADelete the label feature, and nothingreaches C or D any more.History is whatever you can reach from a label by following the arrows.Each commit points at its parent, so a commit knows its past and not its future. git log starts atHEAD and walks backwards. A commit that no branch or tag can reach is invisible, and Git removes itafter a grace period, so give new work a branch name before you move away from it.

git log walks backwards from where you are, parent by parent.

History · Going deeper · 1 of 2

Detached HEAD

ABCmainHEADgit switch --detach BHEAD now names a commit directly, not a branch.A commit made here belongs to no branch.Switch away, and nothing reaches it.git switch -c rescuegives the work a branch, and the problem is gone.A detached HEAD is HEAD pointing straight at a commit.It happens when you check out a commit id or a tag to look at an old version. Looking is harmless.Committing there is the risk, because no label will follow the new commit.

HEAD on a commit, not a branch. Name the work before you leave.

Searching history · Going deeper · 2 of 2

Ask history a question: which commits, which file, which line, who

$ git log --oneline741c601 Raise timeout to 60 s for cold startsd45f6ce Add a request timeout25e646b Add app and README$ git log --oneline -- README.md25e646b Add app and README$ git log --oneline -S60741c601 Raise timeout to 60 s for cold starts$ git log --oneline --author=bend45f6ce Add a request timeoutgit 2.43$ git blame app.py --date=short^25e646b (ana 2026-03-02 1) PORT = 8080741c6011 (ana 2026-03-09 2) TIMEOUT = 60$ git show --stat --format='%h %an %s' HEAD741c601 ana Raise timeout to 60 s for cold starts app.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)-- pathonly commits that touched this file or folder-S textonly commits that added or removed that text-pprint each commit's diff as wellHistory answers questions: what touched this file, what introduced this text, who last changed this line.blame names the last commit to touch each line (^ marks the very first commit), and git show then prints that commitwith its message and diff. HEAD~1 is the parent of the current commit and HEAD~2 its grandparent. Also: -n 5 for thelast five, --since="2 weeks ago", and --graph to draw the branches. GitHub shows the same as History and Blame.

Filter git log by path, text or author; git blame names the commit behind each line.

Lesson complete

You finished “Version control with Git”

Nicely done.

Open in a new tab