KevsRobots Learning Platform
By , Minute
In most programs you just press Save. Git splits saving into two steps:
git addgit commitThat sounds like extra work, but it lets you choose exactly what goes into each snapshot. If you’ve fixed a bug and started a new feature, you can commit the bug fix on its own, with its own message.
It helps to picture three places your changes can be:
flowchart LR
W["Working folder<br/>(your files)"] -- "git add" --> S["Staging area<br/>(the next commit)"]
S -- "git commit" --> R["Repository<br/>(saved history)"]
.gitThink of the staging area as a loading bay: you pile up the changes you want, then git commit ships them all together as one snapshot.
In your pico-bot folder from the last module, stage main.py:
git add main.py
git status
On branch main
No commits yet
Changes to be committed:
(use "git rm --cached <file>..." to unstage)
new file: main.py
main.py has moved to “Changes to be committed”. Now commit it with a message:
git commit -m "Add start-up code that blinks the LED"
[main (root-commit) 3f9c2a1] Add start-up code that blinks the LED
1 file changed, 10 insertions(+)
create mode 100644 main.py
You’ve made your first commit. The 3f9c2a1 is the start of the commit’s unique ID, called a hash. Yours will be different. Run git status and you’ll see nothing to commit, working tree clean, which means everything is saved.
From now on, working with Git is a loop:
git status to see what changedgit add the changes you wantgit commit -m "Say what you did"Let’s go round it once. Add a motor pin to main.py, just under the led line:
left_motor = Pin(14, Pin.OUT)
Git now sees a change to a tracked file:
git status --short
M main.py
The M means modified. Stage it and commit:
git add main.py
git commit -m "Set up the left motor pin"
You don’t have to add files one by one:
git add main.py README.md # add specific files
git add . # add every change in this folder and below
git add . is handy, but check git status first so you don’t commit something by accident (module 7 shows how to make Git ignore files you never want to commit).
Your commit messages are the diary of your project. Six months from now, “stuff” or “fixed it” won’t help you at all. A good message:
| Good | Not so good |
|---|---|
Add ultrasonic distance sensor |
sensor |
Fix robot turning the wrong way |
fixed bug |
Slow motors to 60% to stop wheel slip |
changes |
For a longer explanation, leave out -m. Git opens your editor; write a short first line, a blank line, then as much detail as you like. Save and close the editor to finish the commit.
A commit should be one logical change. Small commits are easier to understand and much easier to undo. A good rule: if you can’t describe the commit in one short sentence, it’s probably two commits.
README.md (if you haven’t) and commit it with the message Add README.main.py and README.md. Then stage and commit only README.md. Run git status to check that main.py is still waiting.main.py with a good message.nothing added to commit but untracked files presentgit add <file> before git commit.Why: git commit only saves what’s in the staging area.
Author identity unknown *** Please tell me who you are.git config --global user.name and user.email (see module 2).Why: Every commit has to record who made it.
git commit without -m and an editor openedEsc then type :wq and press Enter.-m, Git asks for the message in your editor.Quick quiz 1 of 3
You edited main.py and ran git commit -m "Update" straight away. Git says no changes added to commit. Why?
git add.Quick quiz 2 of 3
Which commit message is best?
Quick quiz 3 of 3
What is the staging area for?
You can use the arrows ← → on your keyboard to navigate between lessons.
Comments