What is version control?
Have you ever had files like these?
report.docx
report_final.docx
report_final_v2.docx
report_final_v2_REALLY_FINAL.docxVersion control solves this properly. It records every change to your files over time, so you can see what changed, who changed it, when and why, and go back to any earlier version.
Git is by far the most popular version control system: almost every software company and open-source project uses it.
What Git gives you
- Unlimited undo. Return to any saved point in your project's history.
- Experiment safely. Try ideas on a separate branch without breaking working code.
- Collaboration. Many people work on the same code at once, and Git merges their work.
- History and blame. Find exactly which change introduced a bug.
- Backup. Every copy of a Git repository contains the full history.
Git vs GitHub
| Git | GitHub (or GitLab, Bitbucket) |
|---|---|
| A tool installed on your computer | A website that hosts Git repositories |
| Works fully offline | Adds collaboration: pull requests, issues, code review, CI |
| Created by Linus Torvalds (2005) for Linux | Owned by Microsoft (2018) |
You can use Git without GitHub, but you can't use GitHub without Git.
The three areas
This is the most important idea in Git:
Working directory ──git add──▶ Staging area ──git commit──▶ Repository
(your files as you (changes you've (permanent
edit them) chosen for the next history of
snapshot) snapshots)- Working directory: the files you're editing right now.
- Staging area (also called the "index"): a draft of your next snapshot. You choose exactly which changes go in.
- Repository: saved snapshots, called commits, stored in a hidden
.gitfolder.
A commit is a snapshot of your whole project at a moment in time, with a message describing it.
Try it: the built-in Git terminal
Below is a Git simulator. Press ▶ Run to execute the commands, then keep typing your own commands in the terminal. The commit graph under it draws your history as you go.
Read the output of each command carefully:
- After
echo,hello.txtis untracked: Git sees it but isn't saving it. - After
git add, it's staged: "Changes to be committed". - After
git commit, it's saved permanently in history.
The simulator supports the most common Git commands plus a mini shell (
ls,cat,echo "text" > file). Typehelpin it to see everything. For the real thing, install Git (below) and use your own terminal.
Installing real Git
- Mac: run
git --versionin Terminal; macOS offers to install it. Or usebrew install git. - Windows: install Git for Windows from git-scm.com. It includes "Git Bash".
- Linux:
sudo apt install gitorsudo dnf install git.
Then tell Git who you are. This is recorded in every commit:
git config --global user.name "Your Name"
git config --global user.email "you@example.com"
git config --global init.defaultBranch main
git --version1.What is the staging area for?
2.What's the difference between Git and GitHub?