# OpenCode commands: /safe-git-commit and /clear-safe-git — safe git commits with automatic backup snapshots

> Source: <https://gist.github.com/bendtherules/4e21b9aaf17b6ae0f959ece72f2ad12c>
> Published: 2026-06-15 13:41:23+00:00

| description |
Safely commit changes with automatic backup snapshot |
| agent |
build |

!`BRANCH=$(git rev-parse --abbrev-ref HEAD) && BASE=$(git rev-parse HEAD) && REF_NAME="$BRANCH-$(date +%s)-$$" && ORIG_INDEX=$(git write-tree) && git add -A && STASH=$(git stash create -u) && git read-tree $ORIG_INDEX && if [ -n "$STASH" ]; then git update-ref "refs/safe-git/$REF_NAME" $STASH && printf 'BACKUP CREATED\nBase: %s\nStash: %s\nRef: refs/safe-git/%s\nRecovery: "git reset --hard %s && git stash apply --index refs/safe-git/%s && git update-ref -d refs/safe-git/%s"\nCleanup: "git update-ref -d refs/safe-git/%s"\n' "$BASE" "$STASH" "$REF_NAME" "$BASE" "$REF_NAME" "$REF_NAME" "$REF_NAME"; else echo 'NO CHANGES — working tree is clean. Nothing to snapshot.'; fi`

If the output above says NO CHANGES, tell the user "Nothing to snapshot — working tree is clean" and stop. Do nothing else.

If the output says BACKUP CREATED, follow this procedure:

-
**Echo the recovery and cleanup commands to the user BEFORE making any changes.** Copy the exact Recovery and Cleanup lines from the output above and show them to the user. This ensures they can recover or clean up manually if needed.

-
Process the current changes and split them into logical, well-described commits. Stage hunks, make individual commits with meaningful messages.

-
**If anything goes wrong** — a bad commit, incorrect staging, validation failure, or any error — run the recovery commands shown above to automatically restore the working directory and staging area (to the state before any changes were made). Then stop and report what happened.

-
**Do NOT clean up the backup ref automatically** after committing. Leave the backup ref in place. Only delete it if the user explicitly asks with a command like "clean up the backup" or by running the Cleanup command themselves.
