Undo and Redo Commands
Overview
The /undo and /redo commands in OpenCode provide powerful capabilities for managing code modifications and conversation history. These commands allow you to backtrack and restore changes, providing flexibility and control over your development workflow.
Command Reference
/undo
Undo the last message or code modification in the conversation. This requires a Git repository.
Shortcut: Ctrl+X U
/redo
Redo a previously undone message or code modification. This requires a Git repository.
Shortcut: Ctrl+X R
How It Works
OpenCode uses Git’s version control capabilities to track changes made during your conversation. When you use /undo, it:
- Records the current state of your conversation
- Reverts to the previous state
- Updates the Git history to reflect this change
Similarly, when you use /redo, it:
- Checks the Git history for previously undone changes
- Restores the most recent undone state
- Updates the conversation to reflect this restoration
Operation Demonstration
Basic Undo/Redo Workflow
# Start a conversation
> Explain how to implement a binary search algorithm
# Receive response with code implementation
AI: Here's a binary search implementation in Python...
# Realize you want to ask for a different language
> /undo
# Ask for implementation in JavaScript instead
> Explain how to implement a binary search algorithm in JavaScript
# Receive JavaScript implementation
AI: Here's a binary search implementation in JavaScript...
# Decide you preferred the Python version after all
> /undo
> /redo
# Now back to the JavaScript implementation
Code Modification Workflow
# Start with a file
> @src/utils.js
# Ask AI to refactor the code
> Refactor this code to use arrow functions
# AI modifies the file
AI: I've refactored the code to use arrow functions...
# Review the changes and decide you don't like them
> /undo
# Ask for a different refactoring approach
> Refactor this code to use more descriptive variable names
# AI applies new changes
AI: I've updated the variable names to be more descriptive...
Common Scenarios
1. Correcting Mistakes
Scenario: You accidentally ask the wrong question or provide incorrect information.
Solution:
> What's the syntax for a for loop in Python?
AI: Here's how to write a for loop in Python...
> /undo
> What's the syntax for a for loop in Java?
2. Exploring Different Approaches
Scenario: You want to explore multiple solutions to a problem.
Solution:
> How to sort an array in JavaScript?
AI: Here's how to sort an array using Array.sort()...
> /undo
> How to sort an array in JavaScript without using built-in methods?
AI: Here's how to implement a bubble sort algorithm...
> /undo
> /redo
# Now back to the first solution
3. Managing Code Changes
Scenario: You want to experiment with different code modifications without losing your original code.
Solution:
> @src/app.js
> Optimize this code for performance
AI: I've optimized the code by...
> /undo
> Refactor this code for readability
AI: I've refactored the code to be more readable by...
> /undo
> Combine both optimizations
4. Recovering from Bad Suggestions
Scenario: AI provides a solution that doesn’t work or introduces bugs.
Solution:
> Fix this bug in my code: @src/error.js
AI: I've fixed the bug by...
> !node test.js
Error: ...
> /undo
> Fix this bug in a different way
Importance of Undo Functionality
The undo functionality is crucial for several reasons:
- Safety Net: Provides a way to revert changes if they don’t work as expected
- Experimentation: Encourages trying different approaches without fear of breaking things
- Learning: Allows you to compare different solutions side-by-side
- Efficiency: Saves time by avoiding manual rework when changes don’t meet expectations
- Confidence: Gives you the confidence to explore complex code modifications
Best Practices
When to Use Undo/Redo
- Before making major changes: Undo previous changes to start with a clean slate
- After receiving unexpected results: Undo to revert to a known good state
- When exploring multiple options: Use undo/redo to compare different solutions
- During code review: Undo changes to see the original code and then redo to see the modifications
Tips for Effective Use
- Use keyboard shortcuts:
Ctrl+X Ufor undo andCtrl+X Rfor redo to save time - Combine with other commands: Use
/undowith/compactto manage long conversations - Be aware of Git requirements: Ensure your project is in a Git repository before using these commands
- Check Git status: Sometimes it’s helpful to check
!git statusto understand what changes are being tracked
Troubleshooting
Common Issues
- Git not initialized: If you get an error about Git, initialize a repository with
!git init - No changes to undo: If
/undodoesn’t work, you may have reached the beginning of the conversation - No changes to redo: If
/redodoesn’t work, you may have reached the most recent state - Git conflicts: In rare cases, Git conflicts may occur - resolve them using standard Git tools
Error Messages
- “Git repository not found”: Initialize a Git repository in your project directory
- “No changes to undo”: You’ve reached the start of the conversation
- “No changes to redo”: You’ve reached the most recent state
Advanced Usage
Combining with Git Commands
You can combine OpenCode’s undo/redo commands with Git commands for more powerful version control:
# Use OpenCode to make changes
> @src/main.js
> Add error handling to this function
# Check Git status
> !git status
# Undo if needed
> /undo
# Commit changes when satisfied
> !git add src/main.js
> !git commit -m "Add error handling"
Undo/Redo in Plan Mode
When using Plan Mode, undo/redo commands work similarly but also affect the plan state:
> /plan
> Task 1: Implement user authentication
> Task 2: Add database integration
> Task 3: Create API endpoints
> /plan finalize
# Undo to modify the plan
> /undo
> Task 3: Create RESTful API endpoints
> /plan finalize
Conclusion
The /undo and /redo commands are essential tools for managing code modifications and conversation history in OpenCode. By providing a safety net for experimentation and a way to compare different approaches, these commands empower you to work more confidently and efficiently. Remember to use them in conjunction with Git for the best results, and don’t hesitate to experiment with different solutions knowing you can always backtrack if needed.