Skip to main content
v2026.11,858 entries · CC-BY 4.0

Discard Local Changes in Git: restore, reset hard, clean and stash

Discard local changes in Git with restore, reset hard, clean, stash or revert: pick the command by state, preview first, keep work you may need.

Written and maintained by CASRAI Editorial Board

Last updated

To discard local changes in Git, the command depends on the state of the change. For an unstaged edit to a tracked file use git restore <file>; for a staged edit use git restore --staged <file> first; for untracked files preview with git clean -n -d and then run git clean -f -d; to throw away everything tracked and staged at once use git reset --hard. Most of these commands cannot be undone, so the safest habit is to look at what would be lost and, when in doubt, stash instead.

For researchers this matters because analysis scripts, manuscripts and configuration files often live in the same repository as generated output. Discarding the wrong thing can remove an uncommitted fix to a script or a result file you cannot easily regenerate. This guide is organised by what state your change is in, since that is what decides the command. It covers the common phrasings of the task: discard, reset, revert and undo local changes.

What it is used for in research

Version control records the history of code, analysis scripts and text so that a result can be traced to the exact files that produced it (see computational reproducibility and reproducibility infrastructure: workflows, containers and code sharing). Discarding local changes is the routine clean-up step in that workflow: an experiment with a model specification did not work, a debugging edit should not be kept, or a working folder needs to match the last recorded version before a re-run. Git is also one of several tools compared in Git vs GitHub vs GitLab; every command below is plain Git and works with any of those hosts.

Install or access (official source only)

Git and its documentation are published at git-scm.com. The command reference is at git-scm.com/docs and the free Pro Git book is at git-scm.com/book. Run the commands below from inside the repository folder in a terminal. If you do not yet have a copy of someone else’s repository on a hosting service, see Fork Git: what a fork is and how to make one.

Which command for which state

Find the row that matches your situation before typing anything. Each row follows the Git documentation for that command.

State of the change Command What it does What it leaves alone
Edited a tracked file, not staged git restore <file> Restores the working tree file from the index. By default, “the working tree is restored.” Other files and the index
Edited and staged (git add done) git restore --staged <file> Restores the index from HEAD, so the file is no longer staged. The edit itself stays in your working tree
Staged and want the edit gone entirely git restore --staged --worktree <file> Restores both the index and the working tree. Other files
New files Git does not track git clean -n -d, then git clean -f -d Removes “files that are not under version control”, including untracked directories with -d. Tracked files and, without -x, ignored files
Everything tracked and staged, back to the last commit git reset --hard HEAD Overwrites files and the index with the version from the commit. Untracked files, except that it “may overwrite untracked files” that collide with the commit
A commit already made git revert <commit> Records a new commit that undoes the earlier one. History is kept, nothing is deleted

Discard unstaged changes to one file

Use git restore to discard edits you have not staged.

  1. Run git status to see which files are modified.
  2. Run git restore analysis.R, replacing the file name with yours. The file returns to the version in the index.
  3. Run git status again to confirm it no longer appears as modified.

Older tutorials show the same operation as git checkout -- <file>. The Pro Git book, in its “Undoing Things” chapter, calls both git restore <file> and the older git checkout -- <file> “a dangerous command”, because the local changes you made to that file are overwritten. Do not run either unless you are sure you do not want those unsaved changes.

Unstage a change without losing it

If you ran git add by mistake, git restore --staged <file> removes the file from the staging area and leaves your edit in the working tree. Older guides use git reset HEAD <file> for the same purpose. To also throw the edit away, run git restore <file> afterwards, or pass both --staged and --worktree in one command.

Remove untracked files safely

git restore and git reset do not delete new files Git has never tracked. That is the job of git clean. Preview first:

  1. Run git clean -n -d. The -n (--dry-run) flag does not remove anything, it only shows what would be done, and -d includes untracked directories.
  2. Read the list. Check that no data file or result you need is on it.
  3. Run git clean -f -d to delete them. The -f flag is required when the clean.requireForce setting is true, which is its default.

By default only files unknown to Git are removed. Adding -x also removes files that are ignored, and in a research repository ignored files are often build products, caches or locally stored outputs. Do not add -x without reading the dry-run list.

Reset everything with git reset --hard

When you want the working folder and the staging area to match the last commit exactly, run git reset --hard HEAD. The Git documentation describes --hard as overwriting all files and directories with the version from the commit, removing tracked files that are not in that commit, and updating the index to match. The other modes are gentler, and the difference is worth knowing:

  • --soft leaves your working tree files and the index unchanged.
  • --mixed (the default) leaves the working directory unchanged and updates the index to match the new HEAD, so nothing is staged.
  • --hard overwrites the working tree, which is why it discards local changes.

This is what people usually mean by “git reset local changes”: git reset --hard. Pair it with git clean -n -d if untracked files are also in the way.

Undo a commit with git revert

“Git revert local changes” and “git revert changes” are common searches, but git revert does something different from discarding edits. According to the documentation it reverts the changes that earlier commits introduced by recording new commits, and it requires a clean working tree with no modifications from the HEAD commit. In other words, it undoes a commit that already exists. Use it for work you have committed or shared, because it adds history instead of rewriting it. For uncommitted edits use git restore or git reset --hard.

If you want the undone changes applied to your working tree without an immediate commit, git revert -n <commit> (--no-commit) applies them to the working tree and index only.

Keep the changes instead: stash and branch

If you may want the work back, do not destroy it. The Pro Git book suggests stashing or branching to keep changes you are not ready to commit.

Goal Command Behaviour according to the documentation
Set work aside and return to a clean tree git stash push Saves modifications and rolls the working directory back to HEAD.
Include new, untracked files git stash push -u -u (--include-untracked) also stashes untracked files.
Bring the work back git stash pop Applies the stashed changes and removes the entry from the stash.
Throw one stash entry away git stash drop Removes a single stash entry.
Restore a file from a tagged or older version git restore --source=<tag> <path> --source picks another commit, branch or tag as the restore source.

The last row is useful when a result changed unexpectedly: restore the script as it was at a tagged release, re-run it, and compare. For a data-heavy variant of the same problem see DVC: data version control for research datasets and ML pipelines.

Delete a local tag

Deleting a tag is a separate operation from discarding changes. git tag -d <tagname> (long form --delete) deletes the existing local tag with that name. It does not touch your files.

Output and how to read it

Most of the commands above print nothing when they succeed, so check the result with git status. The one command designed to print a list is the dry run: git clean -n -d shows what would be removed without removing anything. Read that list line by line before you run the forced version.

Troubleshooting

git clean refuses to run

If git clean will not run without force, that is the clean.requireForce safeguard, which is true by default. Run the dry run first, then add -f deliberately.

Untracked directories are left behind

Without -d, git clean does not recurse into untracked directories. Add -d and keep the dry-run flag until you have checked the list.

git revert will not start

The documentation says git revert requires a clean working tree. Commit or stash your local edits first, or discard them with the commands above.

Ignored files are still there

Ignored files are removed only with git clean -x. Treat that flag with care in research repositories.

I discarded something I needed

git restore <file> overwrites the working copy with the index version, and Git has no record of uncommitted, unstaged edits to bring back (the Pro Git book calls both this command and the older git checkout -- <file> dangerous). This is the reason to stash or commit to a scratch branch before using it on work you might want.

Alternatives to discarding

Git is free software, and every approach in this guide uses only its built-in commands. Choose by what you want to happen to the change:

If you want to… Instead of discarding, use
Keep the work but clear the tree git stash push -u, then git stash pop later
Keep staged work but unstage it git restore --staged <file>
Undo a shared commit without rewriting history git revert <commit>
Preview before deleting untracked files git clean -n -d

FAQ

How do I discard all local changes in Git?

Run git reset --hard HEAD to reset tracked files and the index to the last commit, then git clean -n -d followed by git clean -f -d to remove untracked files. Both are destructive, so stash first if there is any doubt.

What is the difference between git restore and git reset --hard?

git restore works on the paths you name and by default restores the working tree. git reset --hard overwrites all files and the index with the version from a commit.

How do I undo a git change or several changes?

For uncommitted edits use git restore. For commits already made use git revert <commit>, which records a new commit that undoes the earlier one.

Does git revert discard local changes?

No. It undoes earlier commits by creating new ones, and it needs a clean working tree to start.

Can I recover changes after git restore?

Generally no. Edits that git restore <file> overwrites were never committed or stashed, so Git cannot normally bring them back (the Pro Git book calls this command dangerous for that reason). Stash or commit first if you may want them.

How do I delete a local tag?

Run git tag -d <tagname>. It removes the local tag and leaves your files unchanged.

Sources (official docs)

Checked 2026-10-29.

Follow CASRAI

Research-administration guidance, standards updates and independent tool reviews.

Ask CASRAI · free to try

Ask about Discard Local Changes in Git: restore, reset hard, clean and stash

Ask your first 2 questions free below. Subscribers get 150 a day for $29 a month.

An AI assistant specialized in research administration. It cites the sources behind every answer, labels web answers and says when it can't answer.

Answers draw on CASRAI's guides and dictionary plus the federal and funder documents we index: Federal Register, Grants.gov, Regulations.gov and UKRI.

Works on this site and inside Claude, Cursor and the AI tools you already use.

Everything CASRAI publishes — this page, the dictionary, the guides and the news — stays free to read, with no account and no card.

Referenced across the research world

University of Cambridge logoColumbia University logoCrossref logoUniversity of Edinburgh logoHarvard University logoUniversity of Oxford logoPrinceton University logoStanford School of Medicine logoUniversity College London logoORCID logoUniversity of Cambridge logoColumbia University logoCrossref logoUniversity of Edinburgh logoHarvard University logoUniversity of Oxford logoPrinceton University logoStanford School of Medicine logoUniversity College London logoORCID logo
  • University of Cambridge logo
  • Columbia University logo
  • Crossref logo
  • University of Edinburgh logo
  • Harvard University logo
  • University of Oxford logo
  • Princeton University logo
  • Stanford School of Medicine logo
  • University College London logo
  • ORCID logo

View CASRAI adoption →

Ask CASRAI · Regulatory Radar

Research-admin question? Get an answer that links its sources.

An AI assistant specialized in research administration. Every answer links its sources to check before you act. 2 questions free, no account. $29/month after.

  • Answers draw on CASRAI's guides and dictionary plus the federal and funder documents we index: Federal Register, Grants.gov, Regulations.gov and UKRI.
  • Every answer numbers its sources and links each one, so you can check the source yourself.