Trying a risky change without risking main
Your weekly report script takes a minute to run. You want the agent to speed it up, and you see two ways to do it: cache results between runs, or read the input file once instead of many times. Either one could break things or spread across files. If the agent works on `main`, half-finished attempts end up in the history you rely on. How do you try both and keep only the winner?
The agent works on whatever is checked out
The agent edits the files in your folder, and every commit lands on the branch you have checked out. Picking a safe branch is your job, and you do it before you type the task. Create a branch from `main`. The attempt's commits then move only that branch's pointer, and `main` stays on your last known-good commit. For two approaches, make two branches from the same `main` commit. Both start from the same place, so you can compare them fairly.
The branch-per-attempt routine
1. On `main`, check that `git status` shows nothing to commit.
2. Run `git switch -c try-<idea>`.
3. Give the task. Commit accepted steps on this branch.
4. Commit before switching. Uncommitted edits belong to no branch and come with you.
5. For a second idea: `git switch main`, then `git switch -c try-<other>`.
6. Compare with `git diff main try-<idea>`. Then switch to each branch and run that version.
7. To keep one: from `main`, `git merge` it. To drop one: `git branch -D` it.
Two attempts at a faster report
$ git switch -c try-cache
▸ You: Speed up `build_report` in `src/report.py` by caching its results between runs.
Review, run the report, commit.
Then, from `main` again:
$ git switch main
$ git switch -c try-batch
▸ You: Speed up `build_report` in `src/report.py` by reading the input file once, not many times.
Review, run, commit.
Run each version twice. Outputs match `main`, but a cache only helps repeat runs, so `try-cache` stays slow the first time. `--stat` counts changed lines per file: `try-batch` is smaller too. Keep it.
$ git diff --stat main try-cache
src/cache.py | 58 ++++++++++++++++
src/report.py | 39 +++++++++---
2 files changed, 88 insertions(+), 9 deletions(-)
$ git diff --stat main try-batch
src/report.py | 12 ++++++------
1 file changed, 6 insertions(+), 6 deletions(-)
Merge the winner, delete the other
You merge from `main` because a merge brings commits into the branch you are on. `main` has not moved since `try-batch` started, so git simply moves `main`'s pointer forward to `try-batch`'s latest commit. `Updating` shows the old and new commit. This is a fast-forward. `-d` deletes only a branch that is already merged, so it works for `try-batch`. Git would refuse `-d` for the unmerged `try-cache` to protect you, so dropping that attempt on purpose takes `-D`.
$ git switch main
Switched to branch 'main'
$ git merge try-batch
Updating 1a2b3c4..9e8d7f6
Fast-forward
src/report.py | 12 ++++++------
1 file changed, 6 insertions(+), 6 deletions(-)
$ git branch -d try-batch
Deleted branch try-batch (was 9e8d7f6).
$ git branch -D try-cache
Deleted branch try-cache (was 3f2a9c1).
One branch per attempt, main stays clean
Before a risky or uncertain task, start from a clean `main` and give the attempt its own branch. Use one branch per approach, and commit before every switch. Compare each branch against `main`, merge the winner from `main`, and delete the rest with `git branch -D`.
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.