AI: Auto commit while programming with Opencode
By Eric Downing. Filed in Uncategorized |I recently was editing a few different web applications and mistakenly ran an update that affected the wrong site and all my sidebar navigation was gone. This was not evident for a few days when I went to update my music practice app and all navigation had disappeared. I had not been reliably committing to get so I looked into how to Auto commit to git so that this would not happen again.
I started with a Background plugin approach and while this would save myself from trouble I had caused, the commit messages were only time stamped.
BackGround Plugin Method
Make the plugins directory either in the project root or in the ~/.config/opencode/plugins.
mkdir -p .opencode/plugins
import type { Plugin } from "@opencode-ai/plugin"
export const AutoCommitPlugin: Plugin = async ({ $ }) => {
return {
event: async ({ event }) => {
// Fires when the agent finishes processing a prompt
if (event.type === "session.idle") {
try {
// Check if there are any modified or untracked files
const status = await $`git status --porcelain`.text()
if (!status.trim()) return
// Stage all changes and commit with a timestamp
await $`git add -A`
const timestamp = new Date().toISOString().replace("T", " ").slice(0, 19)
await $`git commit -m "opencode: auto-commit (${timestamp})"`
} catch (err) {
// Silently ignore if not in a git repo or nothing to commit
}
}
},
}
}
Verification: Start opencode in your project directory, ask it to make a small edit (e.g., adding a comment to a file), and once it finishes, run git log -n 1 in a separate terminal to confirm the new commit appears.


