
Git: commit to it
An agent can change twenty files in a minute. Git is your undo button, and it only works if you press save first. So the one habit:
Commit before you give the agent a task. Then everything it does is easy to look at, keep or throw away.
Use Git every day? Make the branch below and move on.
Do everything on this page in your project folder: FridgeLister or your own code. Make sure that you have your codebase ready.
Once: tell Git who you are
git config --global user.name "Your Name"
git config --global user.email "you@example.com"
In a codespace, GitHub has done this for you.
Make your course branch
A branch is your own line of work that leaves main alone. Do the
whole course on one called course:
git switch -c course
Later: git switch course to get back to it, and
git branch --show-current to see where you are.
Look, then save
git status # which files changed since the last commit
git diff # the changes, line by line (q to quit)
Run them after every agent task. It's the quickest way to see what it touched.
git add .
git commit -m "Add best-before date to items"
add picks up the changes, commit saves a snapshot. Write the message as
what the change does.
Undo: try it now
Make a mistake on purpose, then make it vanish. Open README.md in your
editor, type a line at the end, such as "a mistake", and save. Then:
git status
git diff
git restore README.md
git status
macOS / Linux
You can also make the mistake from the terminal, as the video does:
echo "a mistake" >> README.md.
Windows
In Git Bash you can also make the mistake from the terminal, as the video
does: echo "a mistake" >> README.md. Not in PowerShell, though: its
echo writes the line in a format Git can't show as text, and git diff
only says "Binary files differ".
The last git status no longer lists README.md: the mistake is gone.
git restore . does the same for every changed file at once. That's the
move whenever an agent goes the wrong way.
Two more cases:
- New files the agent created are left alone by
git restore.git statuslists them as "Untracked files"; delete the ones you don't want. - Already committed?
git revert HEADmakes a new commit that undoes the last one. Safe even after you've pushed.
Push
git push -u origin course
Sends your branch to GitHub. You need it for the CI exercises in modules 4
and 6, and to keep codespace work safe. After the first time, plain
git push does it. In a codespace it just works; elsewhere Git asks you to
sign in through the browser. Permission denied (publickey) means the
address starts with git@; clone the https:// one instead.
Want more?
Pro Git is free. Chapters 2 and 3 cover all this and a bit more.
Next: ready, set, agent.
Kept in this browser. Sign in to keep it on every device.