Recover Code Deleted by GitHub Copilot Agent Mode
In this article
Yes, code deleted by GitHub Copilot agent mode can often be recovered using your editor's local history, Git version control, or the trash/backup features of your operating system. The exact method depends on where the code lived and how your project is set up, but acting quickly and checking these sources in order gives you the best chance of getting it back.
What GitHub Copilot Agent Mode Is and How It Works
GitHub Copilot agent mode is an experimental feature in VS Code and Visual Studio that lets the AI autonomously perform multi-step coding tasks. Instead of just suggesting a snippet, the agent can edit multiple files, run commands, and even execute tests to complete a goal you describe. It uses the same underlying models as Copilot but with a broader set of tools and permissions.
Because the agent can modify files without you manually saving each change, it can accidentally overwrite or delete code you wanted to keep. This usually happens when the agent misinterprets instructions, makes a broad refactor, or cleans up code it thinks is unused. The deletion may be immediate and silent, so you might not notice until you run your tests or review the diff.
First Steps: Check Your Editor's Local History
Your first stop should be the local history feature built into your editor. VS Code and Visual Studio keep a rolling record of file changes, even if you never saved a version to Git. This is often the fastest way to restore a deleted file or a specific chunk of code.
- VS Code: Right-click on the file in the Explorer or open the file, then select Open Timeline from the editor title bar. The Timeline panel shows a list of saved snapshots. Click any entry to see the diff and use the Restore button to bring back that version.
- Visual Studio: Go to File > Revert File or use the Track Changes feature in the editor. You can also right-click the file in Solution Explorer and choose History to see local versions.
- JetBrains IDEs: Right-click the file and select Local History > Show History. You can browse snapshots and revert to any point.
If the file was deleted entirely, open the folder containing it, right-click, and choose Local History (in JetBrains) or check the Timeline in the parent folder (in VS Code). The editor usually keeps a snapshot of the deleted file.
Use Git to Undo the Agent's Changes
If your project is under Git, you have several powerful options. The agent's edits are just changes to your working tree, so you can use Git commands to revert them.
- Undo uncommitted changes: If you haven't committed the agent's work yet, run
git checkout -- <file>to discard changes to a specific file, orgit restore .to revert everything in the current directory. Be careful: this permanently discards uncommitted changes, so only use it if you're sure you don't need them. - Restore a deleted file: Use
git checkout HEAD -- <path/to/file>to bring back a file that was deleted but was present in your last commit. - Find lost commits: If the agent created a commit that you later reset or deleted, use
git reflogto see a list of all HEAD movements. Find the commit hash before the deletion and usegit reset --hard <hash>to return to that state. - Use the Git history panel: In VS Code, open the Source Control view, click the ... menu, and select Commit to see a visual history. You can right-click any commit and choose Revert Changes or Checkout to explore it.
Git is the most reliable method because it stores every commit and often tracks reflog entries for days or weeks. If you haven't committed recently, the local history methods above are your best bet.
Check Your Operating System's Trash and Backup Tools
If the file was deleted from disk (not just overwritten), it may be in your system's trash or recycle bin. This is less common because Copilot usually edits files in place, but it can happen if the agent removes a file entirely.
- Windows: Look in the Recycle Bin. If the file isn't there, use File History (if enabled) by right-clicking the parent folder and selecting Restore previous versions.
- macOS: Check the Trash. You can also use Time Machine if you have it set up—navigate to the folder and use the timeline to restore an older version.
- Linux: Check your desktop environment's trash folder (usually
~/.local/share/Trash). If you use a backup tool like Deja Dup or rsync, restore from there.
These methods only work if the file was actually deleted and not just overwritten. For overwritten content, you need a version history system like local history or Git.
Prevent Future Loss: Best Practices
Once you've recovered your code, take steps to avoid this situation again. The most important habit is to commit your work frequently—before you even start a Copilot agent session. This gives you a clean baseline to revert to.
- Commit before every agent run: Make a commit with a message like "before copilot agent" so you have a known good state.
- Use branches: Create a new branch for each agent task. If the agent messes up, you can simply delete the branch and go back to your main branch.
- Enable autosave and local history: In VS Code, ensure Files: Auto Save is on (e.g., afterDelay) and that the timeline is enabled (it is by default). This ensures you have snapshots even without manual saves.
- Review the diff before accepting: After the agent finishes, always check the Source Control diff to see exactly what changed. If something looks wrong, revert immediately before the changes propagate.
- Use Copilot's built-in undo: In the chat panel, you can often click the Undo button on a message that made changes. This reverts the edits from that specific message, but it may not work if you've made further changes since.
What to Do If Nothing Works
If none of the above methods recover your code, consider these last resorts:
- File recovery software: Tools like Recuva (Windows) or PhotoRec (cross-platform) can sometimes recover deleted files from disk, but they work best if the disk hasn't been written to much since deletion. This is a long shot for code that was overwritten, not deleted.
- Cloud sync services: If your project folder is synced to Dropbox, OneDrive, or Google Drive, check their version history. These services often keep previous versions of files for 30 days or more.
- Copilot's session history: In some cases, the agent's conversation in the Chat panel may contain the code it generated. You can copy it back manually, but this only works for code the agent wrote, not code it deleted.
If you still can't recover the code, you may need to rewrite it. To minimize the loss, keep your code modular and well-documented so you can recreate it more easily.
Frequently Asked Questions
Can I undo a Copilot agent action after I've closed the editor? Yes, if you have local history or Git. Local history persists across sessions, and Git reflog works even after closing the editor. Just reopen the project and follow the steps above.
Does Copilot agent mode have its own undo button? Yes, in the Chat panel, each message that made changes has an Undo button. Clicking it reverts the changes from that specific message. However, this only works if you haven't made further edits that conflict with the revert.
Will Git reflog always have the deleted code? No. Reflog only records changes to HEAD (the current branch pointer). If the agent's changes were never committed and you didn't stage them, reflog won't help. You need to rely on local history or file recovery in that case.
Is there a way to make Copilot agent mode safer? Yes, you can restrict its permissions in the settings. In VS Code, you can disable the "Run in Terminal" permission or set it to ask before executing commands. You can also review and approve each file change if you use the "Read-only" or "Suggest" modes instead of full agent mode.
What if I accidentally committed the agent's bad changes? You can still recover. Use git revert <commit-hash> to create a new commit that undoes the changes, or use git reset --hard <previous-hash> to move the branch back. The latter rewrites history, so only do it if you're working alone or on a feature branch.