Git Commands an AI Agent Should Know
The ~15 git commands an agent uses in 95% of work, the failure modes for each, and the discipline that keeps an agent's git history clean.

The 15-command subset
An AI agent doesn’t need git fluency — it needs fluency on ~15 commands. Anything else is a surface; ask the operator.
git status— the read-everything command. Run before any other git action. Habit: every session start.git diff [-- path]— what’s about to be committed. Read before commit.git log --oneline [-N]— recent commits. Usegit log --oneline -10for the last 10.git log --all --oneline --graph— branch structure (lightweight).git add <path>— stage changes. Better thangit add -Abecause selective adds are auditable.git rm <path>— stage deletion. Use instead of deleting files in the worktree then trying to commit.git commit -m "..."— commit staged changes. The body should follow the operator-readable form (see the rules-of-engagement guide).git restore <path>— unstage + revert to HEAD. Reverse ofgit add.git restore --staged <path>— unstage without reverting.git stash [push/apply/pop]— set aside uncommitted work. Avoid where possible — most stash usage is a sign the agent should have committed first.git branch [-a]— list / create branches. Use-ato include remote-tracking.git checkout <branch>— switch branch. Be careful: it overwrites uncommitted changes in tracked files.git switch <branch>— switch branch (newer, safer; doesn’t touch tracked files).git reset [--soft|--mixed|--hard]— undo commits (with care).--hardloses uncommitted work; never run without explicit operator confirmation.git push [-u origin <branch>]— push commits upstream. Never force-push without operator confirmation.git fetch [origin] [github]— pull remote refs without merging. Cheap, safe.git ls-remote <remote> <ref>— read a SHA from a remote without a local copy. The three-SHA check uses this.
That’s the working set. Everything else (rebase, cherry-pick, reflog) is occasional; ask the operator when you need it.
The failure modes
For each commonly-misused command, the specific failure mode:
git add -Astages deletions and modifications indiscriminately. The audit trail becomes unclear when the same commit carries unrelated changes. Usegit add <path>for selective staging.git reset --hard <sha>destroys uncommitted work. There’s no recovery. Never run this withoutgit stashor commit first.git checkout <branch>when there are uncommitted tracked changes silently overwrites them. Usegit switchinstead.git push --forcerewrites the upstream history. Anyone else who pulled since is now stuck. Avoid unless operator explicitly demands.git stashon Windows can mangle line endings. Verify line-ending config (core.autocrlf) before stashing across platforms.git log -pwithout-ndumps the entire history. Use-n Nto limit.
The discipline
Three rules an agent’s git history follows:
- One logical change per commit. A multi-purpose commit (“bumped X, fixed Y, refactored Z”) is hard to revert. The operator’s reverting rule applies per commit.
- Operator-readable commit messages. Subject + body that read like a changelog. The commit-message pattern from the rules-of-engagement guide is the canonical form.
- Three-SHA check before push. Local + origin + github must agree. The three-SHA guide covers this.
What the agent does NOT do with git
- The agent doesn’t run
git filter-branchorgit filter-repo. Those rewrite history destructively. Use only at operator’s explicit request. - The agent doesn’t
git push --mirror. That pushes ALL refs including branches that should stay local. - The agent doesn’t
git config --globalwithout confirming the env. The global config affects every repo; the local config is scoped. - The agent doesn’t
git inita new repo without operator confirmation. The deploy chain assumes the repo exists.
Read-vs-write discipline
Most git commands have a read form and a write form:
- Read-only (no —write semantics):
git status,diff,log,ls-files,branch -a,fetch,ls-remote. Safe to run. - Write (modifies state):
add,commit,restore,rm,stash,reset,push. Surface before running on someone else’s branch.
For non-trivial writes, the agent confirms the target (which branch, which remote, which files) before running.
Verification checklist
| # | Question | Rule |
|---|---|---|
| 1 | Did I run git status before this action? | Yes |
| 2 | Did I run git diff to verify what’s staged? | Yes |
| 3 | Are the changes coherent (one logical change per commit)? | Yes |
| 4 | Will the commit message format follow the operator-readable form? | Yes |
| 5 | Will I push only after the three-SHA check? | Yes |
The reflog — the agent’s recovery cord
git reflog is the second-tier safety net behind every command above. It logs every HEAD update — commits, checkouts, reset, merges — and gives the agent a window to recover from mistakes that would otherwise be unrecoverable.
The two common reflog recoveries:
git reset --hard <sha>accidentally. The reflog still has the pre-reset HEAD. Rungit reflog, find the SHA before the reset,git reset --hard <that-sha>. The “lost” commits come back.git checkoutoverwriting local changes. Same pattern — reflog has the prior HEAD; switch back. Untracked files persist; tracked-file changes are gone, but you can re-apply viagit stash applyif you stashed first.
The reflog is local-only (not pushed), so it survives even when the upstream is fine. It expires after 90 days by default, so a fix needs to happen within that window. The agent should run git reflog --stat once per session if any nontrivial reset has happened — it makes recovery a one-step lookup instead of archaeology.
Sources
- Git reference
- ABS companion: Three-SHA Check Before Pushing
- ABS companion: Search the Codebase Before Fixing Anything
Last verified: 2026-07-14 against the git CLI in active use on the ABS site repo at /srv/abs-site.



Submit a take
Have a different read on this? Drop a comment below — your email isn't published, and I read every one. Nothing leaves the site until I approve it.