Who changed the price list?
On Monday the shop's website shows tea at 2.50. On Tuesday it shows 2.80, and nobody remembers changing it.
Somebody changed a file. Which change was it, who made it, and what did the file say before?
Almost every technical team answers those questions with a tool called Git.
By the end of this lesson
Note
I can save my work as commits and see its history.
Key words
| Word | What it means | Example |
|---|---|---|
| Repository | a folder whose history Git keeps | |
| Commit | a saved snapshot of your changes, with a message | |
| Staging area | the list of changes for the next commit | |
| Log | the list of past commits | git log --oneline |
| Diff | the lines that changed between two versions | |
| Branch | a separate line of commits |
The cells students fill in are left blank in this preview.
A commit saves a snapshot
Saving a file only keeps its newest version. A commit keeps every version you choose to save.
It takes two steps. git add prices.csv puts the change in the staging area. git commit -m "message" saves everything staged as one commit.
Each commit has an id, an author, a date and your message.
What Git prints as you save
$ git status
On branch main
Changes not staged for commit:
modified: prices.csv
$ git add prices.csv
$ git status
On branch main
Changes to be committed:
modified: prices.csv
$ git commit -m "Raise tea price to 2.80"
[main 4f2a9c1] Raise tea price to 2.80
1 file changed, 1 insertion(+), 1 deletion(-)
A common mistake
Warning
Many people think git add saves their work in the history. It does not: it only stages the change. Nothing is in the history until you run git commit. Run git status to see which step you are on.
log and diff read the history
git log --oneline lists the commits, newest first, one a line.
git diff shows what changed. A line starting with - was taken out. A line starting with + was put in.
A good message such as "Raise tea price to 2.80" tells the next person why.
Reading the price list's history
$ git log --oneline
4f2a9c1 Raise tea price to 2.80
b81d307 Add cake to the menu
2c5e6aa First price list
$ git diff b81d307 4f2a9c1
--- a/prices.csv
+++ b/prices.csv
@@ -1,3 +1,3 @@
item,price
-tea,2.50
+tea,2.80
cake,3.20
A branch keeps work apart
Every repository starts with one branch, often called main. The live website is usually built from it.
To try a change safely, you make a new branch. Your commits go on it, and main stays as it was.
When the change is ready, the team joins it back into main. You learn that step in a later module.
The commands you use every day
| Command | What it does |
|---|---|
| git init | Starts a new repository in this folder |
| git status | Shows what changed and what is staged |
| git add file | Stages a change |
| git commit -m "why" | Saves the staged changes as a commit |
| git log --oneline | Lists past commits |
| git diff | Shows the lines that changed |
| git switch -c name | Makes a new branch and moves to it |
The cells students fill in are left blank in this preview.