---
title: "How do I discard local changes to a file in Git?"
description: "Throw away uncommitted edits with git restore, the older git checkout -- form, or git clean for untracked files, and learn which commands cannot be undone."
url: https://www.danny.engineering/answer/git/how-do-i-discard-local-changes-to-a-file-in-git
author: Danny
published: 2026-11-27T00:00:00.000Z
updated: 2026-11-27T18:58:00.000Z
tags: ["Git"]
---

# How do I discard local changes to a file in Git?

## The short answer


Use `git restore` with the file name to put a tracked file back to how it was in the last commit. For new files Git is not tracking yet, use `git clean`, and always run it with `-n` first to see what it will delete.


```bash
git restore src/app.ts         # discard edits to one file
git restore .                  # discard edits to every tracked file
git clean -n                   # preview untracked files to delete
git clean -f                   # actually delete them
```


## Why it happens


Git tracks three versions of a file: the one in the last commit (`HEAD`), the one in the index (staged), and the one on disk. "Discarding changes" means copying an older version back over the copy on disk.


Which command you need depends on where the change is:

- Edited but not staged: restore the working tree from the index.
- Edited and staged: restore both the index and the working tree from `HEAD`.
- A brand new file that has never been added: Git has no older version, so the only option is to delete it, which is what `git clean` does.

The important thing to know is that uncommitted work that you discard is gone. Git cannot bring it back because it was never stored in a commit.


## How to fix it


### Discard unstaged edits with git restore


```bash
git status
# Changes not staged for commit:
#         modified:   src/app.ts

git restore src/app.ts
```


This copies the staged version (which is the committed version if you have not staged anything) back over the file. You can pass several paths, a directory, or `.` for everything. Add `-p` to pick individual hunks to throw away and keep the rest.


### Discard staged and unstaged edits together


If you already ran `git add`, plain `git restore` leaves the staged version in place. Restore both from the last commit:


```bash
git restore --staged --worktree src/app.ts
```


You can also restore a file from any other commit or branch with `--source`:


```bash
git restore --source=main src/app.ts       # take the version on main
git restore --source=HEAD~2 src/app.ts     # take the version from two commits ago
```


### The older git checkout form


Before Git 2.23, the usual command was `git checkout` with a double dash. It still works and you will see it in many older answers:


```bash
git checkout -- src/app.ts
git checkout HEAD -- src/app.ts   # also resets the staged copy
```


The `--` tells Git that what follows is a path, not a branch name. Without it, a file and a branch with the same name are ambiguous.


### Remove untracked files with git clean, carefully


`git clean` deletes files that Git does not track. It refuses to run without `-f` by default, which is a good thing.


```bash
git clean -n          # dry run: list what would be deleted
git clean -f          # delete untracked files
git clean -fd         # also delete untracked directories
git clean -i          # interactive: choose what to delete
```


By default it leaves ignored files alone. Adding `-x` also deletes ignored files such as `node_modules` and `.env`, which is rarely what you want.


### Keep a safety net with git stash


If you are not completely sure you will never want the changes, stash them instead of deleting them. You can drop the stash later.


```bash
git stash push -u -m "experiment I might want back"
```


## Common pitfalls

- Running `git clean -fdx` in a project with a local `.env` or other ignored config. Those files are deleted too and are not in Git to recover.
- Expecting `git restore file` to unstage. It changes the working tree; use `git restore --staged file` to unstage only.
- Using `git reset --hard` when you meant one file. It discards every uncommitted change in every tracked file.
- Discarding work that was never committed or stashed. Git has no copy, so it cannot be undone.
- Forgetting the `--` in `git checkout -- name` when a branch has the same name as the file.

## Related reading

- [Git documentation: git restore](https://git-scm.com/docs/git-restore)
- [Git documentation: git clean](https://git-scm.com/docs/git-clean)
