Skip to content
New — orchestrate a team of Mel agents from your terminal. Learn more about orchestrating Mel agents
← All guides

Guides · git

How to discard local changes to a file in git

By Lalla Asmaa Alaoui, founder of Mel · Updated

Run git restore path/to/file to throw away your uncommitted changes and put the file back to its last committed state. If the change is staged, run git restore --staged path/to/file first to unstage it.

This is the undo button for an unwanted edit, whether you made it or an AI agent did. Be careful: discarded changes that were never committed or stashed cannot be brought back. git restore arrived in git 2.23; older versions use git checkout -- path/to/file. The commands are the same on Mac, Linux and Windows.

Steps

  1. See what changed.
git status
git diff path/to/file

Read the diff before you discard it. Once it is gone, it is gone.

  1. Not sure? Stash instead. A stash puts your changes aside where you can get them back.
git stash push path/to/file

git stash list shows your stashes and git stash pop brings the latest one back. If you are still unsure after a day, you can drop it then.

  1. Discard changes to one file.
git restore path/to/file

The file now matches your last commit.

  1. Unstage a file you already added. If git status lists the file under "Changes to be committed":
git restore --staged path/to/file

This only unstages it; your edits are still in the file. Run step 3 to discard them too. To do both at once:

git restore --staged --worktree path/to/file
  1. Discard changes to every tracked file.
git restore .

Run this from the top of the repo to cover everything, or from a subfolder to cover only that folder.

  1. Remove new, untracked files. git restore does not touch files git has never tracked. To delete them, preview first:
git clean -n

The -n flag lists what would be deleted without deleting anything. When the list is right:

git clean -f

Add -d to include new folders. Files listed in .gitignore are kept unless you add -x, which also removes build output and local config such as .env. Use -x with care.

  1. Restore a file from an older commit.
git restore --source=HEAD~1 path/to/file

This sets the file to how it was one commit back. Review it and commit if that is what you want.

Older git versions

If git restore is not found, your git is older than 2.23. Use:

git checkout -- path/to/file
git reset HEAD path/to/file

The first discards changes in the file. The second unstages it. Better still, update git.

Windows notes

All commands above work in Command Prompt, PowerShell and Git Bash. Paths can use \ or /. In PowerShell, quote paths that contain spaces: git restore "docs/my notes.md".

Doing it with Mel

You can run git restore at Mel's prompt like in any terminal. Mel also gives you two other ways to undo an unwanted edit.

From the code review panel. Press ctrl+shift+= (cmd+shift+= on macOS) to see every uncommitted change as a card per file. Each card has a Discard button, which runs git restore -- <path> for a tracked file. For a new untracked file, it deletes the file. Discard all in the header runs git restore ., which reverts tracked changes only and never bulk-deletes untracked files.

Before the change happens. Mel's approvals let you decide which edits and commands need your yes. With approvals on, each file edit an agent wants to make shows on an approval card, and you can deny it. Edits you allow show as diff cards in the conversation, so you can see each one and discard what you do not want.

The safest habit with any agent: commit or stash your own work before a task. Then a discard only ever removes the agent's changes, never yours.

FAQ

How do I undo changes to a file I have not committed? Run git restore path/to/file. It puts the file back to its last committed version.

How do I unstage a file without losing my changes? Run git restore --staged path/to/file. The file leaves the staging area and your edits stay.

Can I recover changes after git restore? Not through git, because they were never saved in a commit or stash. Your editor's local history may still have them. Use git stash when you are unsure.

How do I discard all local changes? Run git restore . for tracked files. To also remove new files, run git clean -n to preview and then git clean -fd.

What does git checkout -- file do? It is the older way to discard changes to a file, the same as git restore file.

Facts checked on Oct 25, 2026.

Git is a trademark of its owner. Mel is not affiliated with or endorsed by Git.

Mel – the agentic terminal · openmel.dev. A multi-agent, multi-model AI terminal for Mac, Windows, Linux and VS Code. Free with your own API key.

Lalla Asmaa Alaoui
Written by
Lalla Asmaa Alaoui
Founder & CEO of Mel

Lalla is a hands-on tech founder, agentic infrastructure architect and the vision behind Mel.

Follow on LinkedIn

Get started

Try the agentic terminal.

The Mel terminal is free to download, no account needed. The AI agent is free on your own provider key — 1,500 hosted-AI credits a month on Pro.