Everyone Can Learn Program
中文 Start learning
Core · GitLesson 1 of 6Free to read

Commits · Git and GitHub

How do you save versions of your work?

Course contents

Why you need it

"final.doc, final2.doc, final-really.doc" is version control done by hand. Git does it properly: every saved version (a commit) has a message, an author and a time, and you can always go back. Every software team uses it, and your GitHub profile is built from it.

The intuition

Think of three places:

  1. Working folder: the files you edit.
  2. Staging area: a tray where you put the changes for the next snapshot (git add).
  3. History: the snapshots themselves (git commit).

git status tells you what is in each place. git log lists the snapshots, newest first.

The tray lets you choose: you changed three files but only two belong together, so you add those two and commit them with one clear message.

Definition

Commit

A snapshot of the staged files with a message. It gets an id (a hash like 1abf82a) and points to the commit before it.

Repository

A folder Git tracks, with its whole history stored in a hidden .git folder. `git init` turns a folder into one.

Examples

The first commit

Run it in the lesson

Start tracking a folder, stage both files and save the first snapshot.

git init
git status
git add app.py README.md
git commit -m "first version"
git status
Step by step
  1. L1The folder is now a repository.
  2. L2Both files are untracked: Git sees them but isn't saving them.
  3. L3Put both on the tray.
  4. L4-m gives the message. The snapshot is saved.
  5. L5Nothing left to commit: the working tree is clean.

Choose what goes in

Run it in the lesson

Two changes, two commits, each with its own message.

echo "print('welcome')" >> app.py
echo "Run with python app.py" >> README.md
git add README.md
git commit -m "explain how to run"
git status
git commit -am "add welcome message"
git log --oneline
Step by step
  1. L3Only the README goes on the tray.
  2. L5app.py is still modified, not yet committed.
  3. L6-a stages every tracked file that changed, then commits.
  4. L7Three commits, newest first.

Common mistakes

Messages like "update"

In six months "update" tells you nothing. Say what changed and why: "fix total when cart is empty".

One giant commit

Mixing a bug fix, a new feature and reformatting in one commit makes it impossible to undo just one of them. Commit each idea separately.

Quick quiz

1. You edited a file and ran `git commit -m "fix"` without `git add`. What happens?

  1. AThe change is committed
  2. BNothing is committed: the change isn't staged
  3. CGit deletes the change
Show the answer

Answer: B

commit saves what is on the tray. Use git add first, or commit -a for tracked files.

2. What does the last command print?

git init
git add app.py
git commit -m "add app"
git status
  1. A
    On branch main
    Untracked files:
      README.md
  2. B
    On branch main
    nothing to commit, working tree clean
  3. C
    On branch main
    Changes to be committed:
      new file:   README.md
Show the answer

Answer: A

Only app.py was added, so README.md is still untracked.

Practice

3 exercises in three steps: predict what a program prints, change a program to meet a new requirement, then write one from scratch. Your code runs in the browser and is checked the moment you submit.

  1. PredictWhat is staged?

    After these commands, which statement is true?

  2. ModifyNothing to commit?

    This tries to make the first commit but fails. Fix it so both files are committed and the working tree is clean.

  3. WriteTwo clean commits

    Start a repository. Commit README.md alone with the message add readme, then commit app.py with the message add app.

Practise this lesson

Open the interactive lesson to edit and run these examples and solve the exercises with instant feedback. Free to start, no sign-up.

Terms in this lesson

TermChineseDefinition
Version control版本控制Keeping every saved version of files so you can compare and go back.
Commit提交One saved snapshot in Git's history, with a message.
Staging area暂存区The changes chosen for the next commit.