Bulk Replace
Troubleshooting

How to Undo a Find and Replace That Changed Multiple Files

Made a bad replace across many files? How to recover with Previous Versions, version control or backups, and how to make every future run undoable.

Updated 5 minute read The Bulk Replace team

If a find and replace has gone wrong across multiple files, your options depend on where the files are. If they are still open in your editor, Ctrl+Z may undo each one. If they were changed on disk, recover them from version control (git checkout or git restore), from Windows Previous Versions or File History, or from a backup. Reversing the replace by swapping the find and replace text only works in narrow cases. Stop editing the files until you have chosen a route, so you do not overwrite anything you could still recover.

Below is each recovery method in order of reliability, followed by how to make every future bulk replace reversible.

First: stop and assess

  1. Do not run another replace to "fix" it until you know exactly what changed.
  2. Do not close your editor if the files are open there. Its undo history disappears when you close it.
  3. Work out the scope. Search for the replacement text to see how many files and matches are affected.
  4. Copy the current state of the folder somewhere safe. If a recovery attempt goes wrong, you can at least get back to this point.

Option 1: undo in the editor (files still open)

Editor undo only covers documents the editor has open in memory.

  • Notepad++: Replace All in All Opened Documents changes open tabs without saving them, so Ctrl+Z in each tab works. Replace in Files writes files that are not open straight to disk, and those cannot be undone from the editor.
  • VS Code: a Replace All from the Search view can often be undone with Ctrl+Z straight afterwards, while you have not made other edits. Once you have moved on, or closed the window, use Git or a backup.

Try undo first if it applies, then check a few files to be sure it worked everywhere.

Option 2: version control

If the folder is a Git repository, this is the cleanest recovery. In a terminal in the repository:

  1. See what changed: git status and git diff --stat.
  2. Throw away all uncommitted changes to tracked files: git restore . (or git checkout -- . on older Git versions).
  3. To restore only some files: git restore path/to/file.html.
  4. If you already committed the bad replace, undo that commit with git revert <commit-id>, which creates a new commit that reverses it.

If you had other, wanted changes mixed in, use git diff to review and restore file by file instead.

Option 3: Windows Previous Versions and File History

Windows can keep earlier copies of files, but only if something was set up to make them.

  1. In File Explorer, right-click the folder and choose Properties.
  2. Open the Previous Versions tab. On Windows 11 you may need to choose Show more options first.
  3. If versions are listed, select one from before the replace and click Open to look inside.
  4. Copy the files you need to a new location and compare them, rather than using Restore straight away, which overwrites the current folder.

Versions appear here only if File History was backing up that folder, or restore points or shadow copies cover the drive. If the list is empty, this option is not available for this job.

Option 4: a backup or synced copy

  • Cloud sync folders such as OneDrive, Dropbox and Google Drive usually keep version history for each file. Check the web interface for "Version history" or "Previous versions".
  • Server or hosting backups: if the files are a website, your host may have daily backups you can restore.
  • Deployed copies: the live site, a staging server or a colleague's copy may still have the old version.

Option 5: reverse the replace (with care)

Swapping the find and replace text can work, but only when the replacement text did not already exist in the files before the run. Consider:

Original replaceReverse it?Why
Acme Widgets → Zenith ToolsUsually safeThe new text is unlikely to have existed before
e-mail → email in proseRiskySome files may already have used the new spelling, and reversing would change those too
cat → dog without whole wordNocategory became dogegory, and existing dog would also change
Anything → empty (deleted text)NoThere is nothing left to search for
Regex with groupsSometimesYou need a reverse pattern that exactly undoes the forward one

Before reversing, search for the replacement text and check that every match came from your run. If you cannot be sure, use a backup instead.

Option 6: repair the damage manually

If no backup exists and the change cannot be reversed cleanly, narrow the repair down:

  1. Search for the patterns the bad replace created, such as dogegory, and fix each kind of damage with a targeted rule.
  2. Use whole-word and case-sensitive matching so the repair does not cause new problems.
  3. Check modified dates in File Explorer to find which files were touched.

Make every future replace undoable

The best fix is not needing one. Before any bulk replace:

  • Commit or copy the folder first, every time.
  • Turn on File History in Windows Settings for folders you edit often, so Previous Versions has something to show.
  • Preview before writing. Most bad replaces would have been obvious from the match count or a glance at the results.
  • Use a tool that backs up automatically. Bulk Replace backs up every file it touches before changing it, with timestamped backups per run. If the result is not right, one click restores the whole job, whether it touched ten files or ten thousand.

Bulk Replace also shows a before/after diff of every match before anything is written, so you can untick the ones you do not want. That combination of preview first and undo afterwards turns a risky job into a routine one.

Write down what happened

Once the files are recovered, note the pattern, the folder and what went wrong. Most bad replaces come from the same few causes, and a short record turns this incident into a checklist item for the next job.

Most bad replaces come from partial-word matches or a regex that matched more than expected. See find and replace whole word only to avoid the first, and regex replace not working for the second. For the full process of a safe bulk change, start with how to find and replace text in multiple files.

Frequently asked questions

Can I press Ctrl+Z to undo replace in files?

Only in files the editor still has open. Files changed and saved on disk by a replace-in-files command are out of reach of Ctrl+Z.

Does Windows keep old versions of files?

Only if File History or restore points covering that drive were turned on. Right-click the folder, choose Properties and open the Previous Versions tab to see what is available.

Can I reverse a replace by swapping the find and replace text?

Only if the replacement text did not already exist in the files before the run. Otherwise the reverse replace will also change text you never touched.

Get three guides like this every month, plus software update notices.Subscribe →