There's no universal undo in Terminal. Unlike most Mac apps, Command + Z doesn't work for commands you've already run.
Some actions can be reversed. Others can't.
What You Can Undo
Undo text edits on the current line
Before pressing Enter, you can undo typing with:
Control + _ (Control + Underscore)
This undoes your most recent edit to the current line.
Undo a file move
If you moved a file, move it back:
mv newlocation/file.txt oldlocation/file.txt
Undo a file rename
Just rename it again:
mv newname.txt oldname.txt
Undo a copy
Delete the copy:
rm copied-file.txt
Undo Git commits
git reset --soft HEAD~1 # Undo last commit, keep changes
git reset --hard HEAD~1 # Undo last commit, discard changes
Undo file changes in Git
git checkout -- file.txt # Restore file to last commit
git restore file.txt # Same thing (newer syntax)
What You Cannot Undo
Deleted files (rm)
rm file.txt # Gone. No undo.
rm doesn't move files to Trash. They're immediately deleted. Recovery requires backup software or Time Machine.
Overwritten files
echo "new content" > file.txt # Old content is gone
cp newfile.txt existing.txt # existing.txt is overwritten
The original content is lost.
Wrong commands that already ran
If you ran something that made changes - created files, modified configs, installed software - you need to manually reverse each change.
How to Protect Yourself
Use confirmation flags
rm -i file.txt # Asks "are you sure?"
mv -i old new # Warns if destination exists
cp -i source dest # Warns if destination exists
Make these your defaults with aliases:
alias rm="rm -i"
alias mv="mv -i"
alias cp="cp -i"
Preview before acting
With wildcards, list first:
ls *.log # See what matches
rm *.log # Then delete
Use version control
Git is your undo button for code projects:
git add .
git commit -m "Before risky change"
# Now you can always go back
Move to Trash instead of deleting
mv file.txt ~/.Trash/
Or install the trash command:
brew install trash
trash file.txt
Use Time Machine
If Time Machine is enabled, you can recover deleted files:
- Open the folder where the file was
- Click the Time Machine icon in menu bar
- Navigate back in time
- Select and restore
Recovering from Common Mistakes
"I deleted the wrong file"
If Time Machine is on, restore from backup. Otherwise, check if it's in iCloud, Dropbox, or any cloud sync.
"I overwrote a file with the wrong content"
Check version control or Time Machine. If neither, the original is likely gone.
"I ran a command and now things are broken"
Check your shell history to see exactly what you ran:
history | tail -20
Then research how to reverse that specific command.
"I broke my .zshrc"
If you have a backup, restore it. If not, you can reset to a minimal working config:
mv ~/.zshrc ~/.zshrc.broken
echo 'export PATH="/usr/local/bin:\$PATH"' > ~/.zshrc
source ~/.zshrc
Then carefully add back customizations.
The "Oops" Workflow
When something goes wrong:
- Stop. Don't run more commands.
- Check history:
history | tail -10 - Assess damage: What did the command actually do?
- Check backups: Time Machine, Git, cloud storage
- Research: Google the command + "undo" or "revert"
- Fix manually: If no backup, reverse the changes by hand
Commands That Are Especially Dangerous
| Command | Risk |
|---|---|
rm -rf |
Deletes recursively, no confirmation |
sudo anything |
Runs with admin privileges, affects system |
> file.txt |
Overwrites file with nothing (empties it) |
dd |
Low-level disk writing, can destroy data |
chmod -R 777 |
Makes everything world-writable |
Keep Learning
Understanding what can and can't be undone helps you use Terminal confidently. The free course teaches you to work carefully and efficiently.
Check it out at Mac Terminal for Humans.