Làm chủ Claude Code trong một tuần (Phần 4): Checkpoints & Rewind
Đây là phần thứ tư trong series "Làm chủ Claude Code trong một tuần". Trong bài này, chúng ta sẽ tìm hiểu Checkpoints — tính năng cho phép lưu và khôi phục trạng thái session.
Checkpoints là gì?
Checkpoints là session snapshots — điểm lưu trạng thái của conversation và code tại một thời điểm cụ thể. Bạn có thể "rewind" (quay lại) checkpoint bất kỳ để thử nghiệm cách tiếp cận khác mà không mất progress.
┌──────────────────────────────────────────────────────────────────┐
│ CHECKPOINTS │
├──────────────────────────────────────────────────────────────────┤
│ │
│ Timeline: │
│ │
│ [Start] ──► [CP1] ──► [CP2] ──► [CP3] ──► [Current] │
│ │ │ │ │
│ │ │ └── Rewind here? │
│ │ │ ↓ │
│ │ │ Try approach B │
│ │ │ │
│ │ └── Or rewind here? │
│ │ ↓ │
│ │ Try completely different solution │
│ │ │
│ └── Safe experimentation from any point! │
│ │
└──────────────────────────────────────────────────────────────────┘
Đặc điểm chính
| Tiêu chí | Giá trị |
|---|---|
| Tạo | Automatic (mỗi user prompt) |
| Persistence | Session-based |
| Lưu gì | Conversation state + Code changes |
| Dùng cho | Safe experimentation, recovery |
Cách Checkpoints hoạt động
Tự động tạo Checkpoint
Mỗi khi bạn gửi prompt, Claude Code tự động tạo checkpoint:
User: "Add user authentication"
↓
[Checkpoint A created]
↓
Claude makes changes...
↓
User: "Use JWT instead of sessions"
↓
[Checkpoint B created]
↓
Claude refactors...
↓
User: "Actually, let's try Passport"
↓
[Checkpoint C created]
Rewind về Checkpoint
┌────────────────────────────────────────────┐
│ REWIND OPTIONS │
├────────────────────────────────────────────┤
│ │
│ Nhấn Esc hai lần hoặc gõ /rewind │
│ │
│ ┌─────────────────────────────────────┐ │
│ │ Choose what to restore: │ │
│ │ │ │
│ │ 1. Restore code and conversation │ │
│ │ 2. Restore conversation only │ │
│ │ 3. Restore code only │ │
│ │ 4. Summarize from here │ │
│ │ 5. Never mind │ │
│ └─────────────────────────────────────┘ │
│ │
└────────────────────────────────────────────┘
Sử dụng Checkpoints
Cách 1: Phím tắt
Nhấn Esc hai lần liên tiếp để mở rewind menu.
Cách 2: Slash command
/rewind
Các tùy chọn Rewind
1. Restore code and conversation
Quay lại trạng thái hoàn toàn — cả code và conversation.
Use case: Muốn thử nghiệm hướng hoàn toàn khác từ một điểm nhất định.
2. Restore conversation only
Chỉ quay lại conversation, giữ nguyên code hiện tại.
Use case: Code đã tốt nhưng muốn giải thích lại hoặc điều chỉnh conversation flow.
3. Restore code only
Giữ nguyên conversation, quay lại code trước đó.
Use case: Conversation vẫn relevant nhưng code changes gần đây không ổn.
4. Summarize from here
Tạo summary từ checkpoint đến hiện tại.
Use case: Tạo overview về những gì đã làm từ một điểm nhất định.
5. Never mind
Hủy thao tác, quay về trạng thái hiện tại.
Use Cases thực tế
1. Thử nghiệm nhiều approaches
Prompt: "Implement caching for API responses"
[Checkpoint created]
↓
Approach A: File-based cache
↓
❌ Too slow for production
↓
/rewind → Restore code and conversation
↓
Approach B: Redis cache
↓
✅ Works great!
2. A/B Testing implementations
Prompt: "Create user notification system"
[Checkpoint created]
↓
Implementation A: Database polling
- Measure performance
- Note pros/cons
↓
/rewind → Restore code and conversation
↓
Implementation B: WebSocket push
- Measure performance
- Note pros/cons
↓
Compare và chọn solution tốt nhất
3. Recover từ mistakes
Prompt: "Refactor authentication module"
[Checkpoint created]
↓
Making changes...
↓
Oops! Accidentally broke everything
↓
/rewind → Restore code only
↓
Code reverted, continue with conversation context
4. Safe large-scale refactoring
Prompt: "Migrate from Laravel 10 to Laravel 11"
[Checkpoint created at stable state]
↓
Step 1: Update composer.json
[Checkpoint]
↓
Step 2: Update config files
[Checkpoint]
↓
Step 3: Update middleware
[Checkpoint]
↓
Tests fail at step 3?
/rewind to step 2
↓
Fix và continue
Branch Points
Checkpoints cho phép tạo branch points — từ một checkpoint, bạn có thể explore multiple paths:
┌── Approach A (JWT)
│
[Checkpoint] ───────┼── Approach B (OAuth)
│
└── Approach C (Sessions)
Workflow suggested
- Create stable checkpoint: Đảm bảo code đang hoạt động
- Try approach A: Implement và test
- Document findings: Ghi note kết quả
- Rewind: Quay về checkpoint
- Try approach B: Implement và test
- Compare: Chọn solution tốt nhất
- Commit: Proceed với chosen solution
Best Practices
1. Checkpoint before risky changes
User: "Before making any changes, confirm this is the current
stable state. I want to be able to rewind here."
Claude: "Confirmed. Current checkpoint created. You can safely
rewind to this point anytime."
User: "Now, let's try rewriting the entire service layer..."
2. Name your checkpoints mentally
Dù checkpoints không có tên thực sự, bạn nên note trong conversation:
User: "This is CHECKPOINT-STABLE-AUTH. The authentication
system is working. Let's experiment from here."
3. Regular rewind points for long sessions
Session:
├── [0min] Start
├── [15min] Basic structure done - safe rewind point
├── [30min] Core logic implemented - safe rewind point
├── [45min] Tests added - safe rewind point
└── [60min] Documentation - final state
4. Use Summarize for complex sessions
/rewind
Choose: 4. Summarize from here
Output:
"From checkpoint to current state:
- Added UserService with CRUD operations
- Created validation layer
- Implemented caching
- Added 15 unit tests
- Fixed 3 edge cases
Total: 12 files changed, 450 lines added"
Checkpoints vs Git
| Aspect | Checkpoints | Git |
|---|---|---|
| Scope | Session | Permanent |
| Includes | Code + Conversation | Code only |
| Speed | Instant | Requires commit |
| Persistence | Session only | Forever |
| Collaboration | Individual | Team |
Khi nào dùng cái nào?
Dùng Checkpoints khi:
- Thử nghiệm nhanh nhiều approaches
- Exploration phase
- Không chắc chắn về direction
- Cần rollback conversation context
Dùng Git khi:
- Code đã stable và tested
- Cần lưu permanent
- Cần share với team
- Milestone reached
Suggested workflow
┌─────────────────────────────────────────────┐
│ CHECKPOINT + GIT WORKFLOW │
├─────────────────────────────────────────────┤
│ │
│ ┌─────────────┐ │
│ │ Git Commit │ ◄── Stable point │
│ └──────┬──────┘ │
│ │ │
│ ▼ │
│ ┌─────────────┐ │
│ │ Checkpoint │ ◄── Start experimenting │
│ └──────┬──────┘ │
│ │ │
│ Try A ──► Fail ──► Rewind │
│ │ │
│ Try B ──► Success! │
│ │ │
│ ▼ │
│ ┌─────────────┐ │
│ │ Git Commit │ ◄── New stable point │
│ └─────────────┘ │
│ │
└─────────────────────────────────────────────┘
Monitoring Context với Checkpoints
Claude-howto giới thiệu cc-context-stats — tool để monitor context usage:
# Install cc-context-stats
npm install -g cc-context-stats
# Monitor context zones
cc-context-stats watch
# Output:
# Context Usage:
# ├── Green Zone: 0-50% (Safe to add more)
# ├── Yellow Zone: 50-75% (Consider summarizing)
# └── Red Zone: 75-100% (Summarize or rewind soon)
Khi context đầy
Context at 85%!
↓
Options:
1. /rewind → Summarize from checkpoint
2. Start new session with summary
3. Continue (risk truncation)
Ví dụ Workflow hoàn chỉnh
Scenario: Implement Search Feature
# Step 1: Create checkpoint at stable state
User: "Current state is stable. Authentication and basic CRUD
are working. Let's implement search feature."
[CHECKPOINT-PRE-SEARCH created]
# Step 2: Try approach A - Elasticsearch
User: "Let's try using Elasticsearch for search"
Claude: [Implements Elasticsearch integration]
User: "Test it"
Claude: [Tests show it's complex and requires separate server]
# Step 3: Rewind and try approach B
User: /rewind
Select: 1. Restore code and conversation
User: "Let's try using Meilisearch instead - simpler to set up"
Claude: [Implements Meilisearch integration]
User: "Test it"
Claude: [Tests show it's fast and simple]
# Step 4: Happy with result, commit to git
User: "Great! Let's commit this"
Claude: [Prepares commit message]
git commit -m "feat: add product search with Meilisearch"
Troubleshooting
Không thể Rewind
Nguyên nhân có thể:
- Session quá mới (chưa có checkpoint)
- Session đã bị clear
- Technical issue
Giải pháp:
User: "Can you show me the available checkpoints?"
Claude: [Lists available rewind points]
Rewind không restore đúng
Nguyên nhân:
- Chọn sai option (code only vs conversation only)
- External changes ngoài Claude session
Giải pháp:
- Chọn đúng restore option
- Check git status cho external changes
Tổng kết
Checkpoints là safety net cho experimentation:
- ✅ Tự động tạo mỗi prompt
- ✅ Restore code và/hoặc conversation
- ✅ Safe experimentation
- ✅ Compare multiple approaches
- ✅ Recovery từ mistakes
Tiếp theo
Trong phần tiếp theo, chúng ta sẽ tìm hiểu CLI Reference — cách sử dụng Claude Code từ command line để automation và scripting.
Tài liệu tham khảo
Series này được dịch và mở rộng từ claude-howto — MIT License.