Git commits as snapshots you can return to
Explain a repository, the staging area and a commit as a saved snapshot of the whole project with a message. Explain why a recent commit works as an undo button for anything an agent does afterwards.
Builds on: Terminal basics: where you are and running things
The agent changed six files. Now what?
You're about to let a coding agent work in your project folder for the first time. It may edit several files, create new ones and even delete some. Your editor's undo only knows about changes typed in that editor, one file at a time. If the result is broken, how do you get the *whole folder* back to exactly how it was? You need a saved copy of everything, made beforehand.
A commit is a photo of the whole project
**git** is a program that keeps the history of a folder. Running `git init` in your project root turns that folder into a **repository**. git adds a hidden `.git` folder where it stores the history. A **commit** is a saved snapshot of every file git tracks. It also records a message, an author, a time and a unique id, usually shown in short form like `3f2a9c1`. Once made, a commit never changes. Later edits can only become new commits.
Staging area: choosing what goes in the photo
A snapshot takes two steps:
1. `git add <file>` copies the file's current contents into the **staging area**, the draft of the next commit. `git add .` stages every change in the current folder and below.
2. `git commit -m "<message>"` saves the snapshot.
A tracked file you changed but didn't add keeps its *previous* version in the new commit. A new file you never added is not in it at all.
Saving a snapshot before handing over
Sam's budget app works right now. Sam moves into the project root and runs two git commands to save that state. git confirms the new commit:
- `main` is the name of the project's line of history (ignore it for now).
- `3f2a9c1` is the commit's short id.
- The counts are files changed and lines added.
Then `git log --oneline` lists the saved snapshots, newest first, one per line: short id and message. Sam can now let the agent work, knowing `3f2a9c1` is safe.
$ cd ~/budget-app
$ git add .
$ git commit -m "Report page works"
[main 3f2a9c1] Report page works
3 files changed, 42 insertions(+)
$ git log --oneline
3f2a9c1 Report page works
a81d07e First version of budget app
Why a recent commit works as an undo button
The agent edits `report.py`, adds `charts.py` and deletes `old_utils.py`. Now the report page crashes. Commit `3f2a9c1` is untouched. git can put every file from that snapshot back exactly as it was, `old_utils.py` included. `charts.py` was never in the snapshot, so it shows up as a new file you can delete. A later lesson shows the exact commands.
Limits: a commit protects only the files it contains, as they were at that moment. Files outside the project root and files never added are not saved. The snapshot lives in `.git`, so if `.git` is deleted, your undo is gone too.
Remember
A commit is a permanent snapshot of every tracked file in your project, built from what you staged with `git add`. Commit right before an agent starts. Whatever it then does to those files, you can return to that exact state, as long as `.git` survives.
On Bytewise this lesson comes in small steps with a check at the end, and the app brings it back right before you would forget it.