Recovering Lost SQL Queries with DataGrip's Local History
Stop losing complex SQL queries. Learn how to use DataGrip's Local History to recover unsaved console buffers and overwritten scripts without needing a Git commit.
06 Jan 2026, 20:26 UTC

You spend thirty minutes perfecting a complex CTE (Common Table Expression) in a database console, run it to verify the results, and then accidentally close the tab or overwrite the buffer with a new query. Because the code was never saved to a formal .sql file or committed to Git, it feels like that hour of engineering work is gone forever.
DataGrip provides a safety net for this exact scenario called Local History. Unlike version control which requires manual commits, Local History automatically tracks every change you make to SQL files and console buffers in the background. It allows you to travel back in time to recover specific blocks of code without ever having initialized a repository.
How Local History Differs from Git
Local History is an automated cache stored on your local machine. Every time you modify a file, save it, or execute a query in a console, DataGrip takes a snapshot of the state. This is particularly useful for 'scratchpad' work—the messy queries used to debug data that do not belong in a project repository.
- Automatic: The IDE tracks changes silently; no manual commits are required.
- Contextual: It captures changes in console buffers that have never been saved to disk.
- Local-only: The history lives on your workstation. It does not persist across different machines or survive a total hard drive failure.
Recovering a Deleted Query
If you have lost code in a console or accidentally overwritten a script, follow these steps to retrieve the previous version:
- Right-click anywhere inside the SQL editor or the specific console tab where the loss occurred.
- Select Local History > Show History....
- In the tool window, select a timestamp from the list on the left that predates the error.
- DataGrip will open a visual diff viewer, comparing your current code with the version from that timestamp.
- Identify the code block you need and click the arrow icon (>>) to move only those specific lines back into your active editor.
Practical Example: Recovering an Overwritten Join
Imagine you were writing a complex join across four tables and accidentally deleted a JOIN clause before saving the file. You can use the diff viewer to isolate the missing logic.
Current (Broken) State:
SELECT u.name, o.order_date
FROM users u
WHERE u.status = 'active'; -- Missing the joins!
Local History Version (15 minutes ago):
SELECT u.name, o.order_date
FROM users u
JOIN orders o ON u.id = o.user_id
JOIN order_items oi ON o.id = oi.order_id
WHERE u.status = 'active';
By using the visual diff viewer, you can copy just the missing JOIN lines from the history pane and merge them into your current script without losing the updated WHERE clause.
Limitations and Configuration
Local History is not a substitute for a Version Control System (VCS). Because it stores snapshots locally, it has a finite retention period. Once the history limit is reached, the oldest entries are permanently purged.
You can adjust how long DataGrip keeps these snapshots by navigating to:
- Settings (or
Cmd+,on macOS) > Appearance & Behavior > Local History. - Modify the Keep X days value. Increasing this provides a longer safety window but may impact performance when comparing extremely large scripts.
Local History acts as the ultimate 'undo' button for the chaotic moments of database development. Use it to recover lost work in the moment, but continue to use Git for any code that requires collaboration or remote backup.
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.