snapshots.mdx 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. ---
  2. title: "Undo"
  3. description: ""
  4. ---
  5. OpenCode snapshots let the default interactive TUI roll back conversation history and related file changes. They are a
  6. convenience for revising recent work, not a replacement for Git commits or backups.
  7. ## Configuration
  8. Snapshots are enabled by default. Set `snapshots` to `false` in your [configuration](/config#snapshots) to stop capturing
  9. filesystem state:
  10. ```jsonc title="opencode.jsonc"
  11. {
  12. "$schema": "https://opencode.ai/config.json",
  13. "snapshots": false
  14. }
  15. ```
  16. Filesystem snapshots require a Git repository. With snapshots disabled, unavailable, or missing, undo can still stage a
  17. conversation rollback, but it has no captured file state to restore. Disabling snapshots does not delete snapshots that
  18. were already stored.
  19. ## What is captured
  20. For each model step, OpenCode attempts to capture the worktree immediately before the model call and after a cleanly
  21. completed step. It records the paths changed between those two points on the assistant message.
  22. Snapshots use a separate internal Git object database in the OpenCode data directory. They do not create commits, move
  23. branches, or intentionally modify your repository's Git index. Capture is limited to the session's active directory, which
  24. may be a subdirectory of the repository.
  25. Within that directory, snapshots include tracked files and untracked files that are not ignored by Git. An individual
  26. untracked file larger than 2 MiB is excluded. Ignored files, files outside the active directory, and changes to Git
  27. metadata are not captured.
  28. During undo, OpenCode does not check out an entire tree. It restores only paths attributed to cleanly completed assistant
  29. steps after the selected conversation boundary. Each path is restored to its state before the first affected step.
  30. ## Undo
  31. Wait for the session to become idle, then run:
  32. ```text
  33. /undo
  34. ```
  35. The TUI finds the latest non-empty user message and stages a revert at that message:
  36. - The selected user message and every later message are hidden, but not deleted yet.
  37. - The selected message's text, attachments, and agent mentions are placed in the composer for revision.
  38. - Captured files changed by the affected assistant steps are restored to their earlier contents. Files created by those
  39. steps are removed when they did not exist in the earlier snapshot.
  40. - A summary shows the staged message count and restored paths.
  41. Running `/undo` again moves the staged boundary to an earlier user message. OpenCode keeps the filesystem state from
  42. immediately before the first undo as the redo baseline, so repeated undos form one wider staged revert rather than a redo
  43. stack.
  44. <Warning>
  45. Sending a new prompt while an undo is staged commits the revert. The hidden message range is removed from the active
  46. session history, the currently reverted files are kept, and redo is no longer available.
  47. </Warning>
  48. ## Redo
  49. While a revert is staged, run:
  50. ```text
  51. /redo
  52. ```
  53. Redo clears the staged boundary, makes the hidden messages visible again, and restores affected files to their exact state
  54. immediately before the first undo. It does not rerun the model. After multiple undos, one redo restores the whole staged
  55. range; there is no step-by-step redo stack.
  56. ## Revert a message
  57. The TUI's **Message Actions** menu also provides **Revert**. It stages the selected message as the conversation boundary
  58. and uses the same file restoration and redo behavior, but it does not copy that message into the composer.
  59. For a conversation-and-files rollback, select a user message. If an assistant message is selected, that message is hidden,
  60. but only file changes attributed to later assistant steps are restored; the selected assistant message's own file changes
  61. are not included.
  62. ## Limitations and safety
  63. - Capture is best effort. A failed capture is logged and the model step continues, so conversation rollback may have no
  64. matching file rollback.
  65. - Interrupted or failed steps do not receive a completed end snapshot. File changes made before the failure may remain.
  66. - Shell commands can change databases, services, processes, network resources, Git state, ignored build output, or files
  67. outside the active directory. Undo and redo do not reverse those side effects.
  68. - Undo overwrites the current contents of affected paths with older contents. Redo likewise overwrites those paths with
  69. the pre-undo state, including edits made after running undo.
  70. - Other processes can edit the worktree between capture and restore. The server rejects revert operations while the
  71. session is actively running, but it cannot protect against external editors or commands.
  72. - Snapshot objects can contain complete contents of tracked and non-ignored untracked files. They are stored locally in
  73. the OpenCode data directory; do not treat snapshots as secret-free metadata.
  74. - Undo is not secure erasure. Committing a revert removes messages from the active projection, not from durable session
  75. history or existing snapshot storage.
  76. Review the staged file summary and your Git diff before continuing. Commit or back up important work independently before
  77. using undo on a dirty worktree.
  78. <Note>
  79. `/undo` and `/redo` are interactive TUI commands. The non-interactive `run` command does not provide them.
  80. </Note>