To revert changes made to your working copy, do this:

git checkout .

Or equivalently, for git version >= 2.23:

git restore .

To revert changes made to the index (i.e., that you have added), do this. Warning this will reset all of your unpushed commits to master!:

git reset

To revert a change that you have committed:

git revert <commit 1> <commit 2>

To remove untracked files (e.g., new files, generated files):

git clean -f

Or untracked directories (e.g., new or automatically generated directories):

git clean -fd
Answer from 1800 INFORMATION on Stack Overflow
Top answer
1 of 16
10109

git-clean - Remove untracked files from the working tree

Synopsis

git clean [-d] [-f] [-i] [-n] [-q] [-e <pattern>] [-x | -X] [--] <path>…​

Description

Cleans the working tree by recursively removing files that are not under version control, starting from the current directory.

Normally, only files unknown to Git are removed, but if the -x option is specified, ignored files are also removed. This can, for example, be useful to remove all build products.

If any optional <path>... arguments are given, only those paths are affected.


Step 1 is to show what will be deleted by using the -n option:

# Print out the list of files and directories which will be removed (dry run)
git clean -n -d

Clean Step - beware: this will delete files:

# Delete the files from the repository
git clean -f
  • To remove directories, run git clean -f -d or git clean -fd
  • To remove ignored files, run git clean -f -X or git clean -fX
  • To remove ignored and non-ignored files, run git clean -f -x or git clean -fx

Note the case difference on the X for the two latter commands.

If clean.requireForce is set to "true" (the default) in your configuration, one needs to specify -f otherwise nothing will actually happen.

Again see the git-clean docs for more information.


Options

-f, --force

If the Git configuration variable clean.requireForce is not set to false, git clean will refuse to run unless given -f, -n or -i.

-x

Don’t use the standard ignore rules read from .gitignore (per directory) and $GIT_DIR/info/exclude, but do still use the ignore rules given with -e options. This allows removing all untracked files, including build products. This can be used (possibly in conjunction with git reset) to create a pristine working directory to test a clean build.

-X

Remove only files ignored by Git. This may be useful to rebuild everything from scratch, but keep manually created files.

-n, --dry-run

Don’t actually remove anything, just show what would be done.

-d

Remove untracked directories in addition to untracked files. If an untracked directory is managed by a different Git repository, it is not removed by default. Use -f option twice if you really want to remove such a directory.

2 of 16
1335

Use git clean -f -d to make sure that directories are also removed.

  1. Don’t actually remove anything, just show what would be done.

    git clean -n
    

    or

    git clean --dry-run
    
  2. Remove untracked directories in addition to untracked files. If an untracked directory is managed by a different Git repository, it is not removed by default. Use the -f option twice if you really want to remove such a directory.

    git clean -fd
    

You can then check if your files are really gone with git status.

Discussions

git bash - Various ways to remove local Git changes - Stack Overflow
I just cloned a git repository and checked out a branch. I worked on it, and then decided to remove all my local changes, as I wanted the original copy. In short, I had to do the following two com... More on stackoverflow.com
🌐 stackoverflow.com
command line - git undo all uncommitted or unsaved changes - Stack Overflow
I'm trying to undo all changes since my last commit. I tried git reset --hard and git reset --hard HEAD after viewing this post. I responds with head is now at 18c3773... but when I look at my local More on stackoverflow.com
🌐 stackoverflow.com
How to discard all changes so I can switch branch
I've tried: git reset --hard HEAD but that doesn't work What does it do, that isn't "work"? More on reddit.com
🌐 r/git
15
3
October 3, 2019
How to reset back to zero changes
I've tried git reset --hard but it does not delete files I've created. Right. git reset is a helpful tool, but it does a specific thing. With Git, your files are divided into two categories: tracked and untracked. Tracked files are that Git is keeping track of for you. When you make a Git commit, there's a snapshot of your entire tree of files, and they get included in this snapshot. And if you switch to an older or newer version, a different branch, etc., these files get updated to match. Untracked files are other files in your tree of files that you haven't asked Git to track. When you use git add to include file in a Git commit that you're in the process of creating, a side effect is that the file changes from untracked to tracked. If you never do this (and nobody else does either), then a file stays untracked. Untracked files are useful for stuff like temporary files (that you don't care about) or build output (that you can create by running build commands). As I'm sure you've guessed by now, git reset works on tracked files but leaves untracked files alone (with some very small exceptions). If you want to delete all untracked files, you can use git clean. In your case, you may want to use git clean -d to make it less conservative about what it deletes. Obviously, you want to be careful with git clean since its purpose is to delete stuff. In many cases, you might want to use both git reset and git clean if you really want a clean slate. One more thing: if you've created any commits and you want to get rid of those, you can also use git reset for that. Like a lot of Git commands, it can do a few different things which are only kinda related. It can reset your files to the latest commit on your current branch, or it can change what actually is the latest commit on your current branch. Whether you need to worry about that right now depends on whether you did any commits or not. More on reddit.com
🌐 r/git
2
1
October 31, 2023
🌐
TheServerSide
theserverside.com › blog › Coffee-Talk-Java-News-Stories-and-Opinions › remove-revert-discard-local-uncommitted-changes-Git-how-to
How to discard local changes in Git
To use the git reset command to discard all local changes, simply type in the following command in a terminal window: discard@changes:~/git-example$ git reset --hard HEAD is now at ebbbca3 Discard local changes example · Use the soft flag with ...
🌐
DEV Community
dev.to › cicube › how-to-discard-local-changes-in-git-43na
How to Discard Local Changes in Git - DEV Community
November 6, 2024 - This command will delete all untracked files, which means those that have not been staged or committed yet. Use it carefully, because these changes cannot be reverted afterwards. If you would like to clean up entire untracked directories, you can extend the command with: ... Here, the -d flag tells Git to remove untracked directories as well as untracked files.
Find elsewhere
🌐
Git Tower
git-tower.com › learn › git faq › how to discard changes in git with git restore
How to Discard Changes in Git with git restore | Learn Version Control with Git
6 days ago - Discard uncommitted changes with git restore. Covers discarding working directory changes, unstaging files, and restoring from specific commits.
🌐
GeeksforGeeks
geeksforgeeks.org › git › how-to-discard-all-changes-in-git
How to Discard all Changes in Git? - GeeksforGeeks
July 23, 2025 - ... Note: Here after using git reset --hard the deleted files(example.py,dummy2.py) is back as these changes were uncommitted. To remove untracked files and directories, use the following commands:
🌐
30 Seconds of Code
30secondsofcode.org › home › git › branch › discard changes
Discard uncommitted or untracked changes in Git - 30 seconds of code
April 12, 2024 - # Usage: git reset --hard HEAD git reset --hard HEAD # Discards all uncommitted changes · Similarly, if you have untracked files in your working directory that you want to discard, you can use the git clean -f -d command.
🌐
GeeksforGeeks
geeksforgeeks.org › git › how-to-remove-local-untracked-files-from-current-git-working-tree
Remove Local Untracked Files From Current Git Working Tree - GeeksforGeeks
May 7, 2026 - By running the git clean command without any flags, a fatal error is generated and explicitly asks us to mention a flag ... To have a look at all the untracked files that will be removed, we can use the git clean command with the -n flag.
🌐
Git Tower
git-tower.com › learn › git faq › removing untracked files with git clean
Removing Untracked Files with Git Clean | Learn Version Control with Git
6 days ago - To get rid of new / untracked files, including those in your local directories or cloud-synced folders, you'll have to use git clean! ... Whatever options and parameters you provide: using git clean will only affect untracked files in the repository ...
Top answer
1 of 10
567

It all depends on exactly what you are trying to undo/revert. Start out by reading the post in Ube's link. But to attempt an answer:

Hard reset

git reset --hard [HEAD]

completely remove all staged and unstaged changes to tracked files.

I find myself often using hard resetting, when I'm like "just undo everything like if I had done a complete re-clone from the remote". In your case, where you just want your repo pristine, this would work.

Clean

git clean [-f]

Remove files that are not tracked.

For removing temporary files, but keep staged and unstaged changes to already tracked files. Most times, I would probably end up making an ignore-rule instead of repeatedly cleaning - e.g. for the bin/obj folders in a C# project, which you would usually want to exclude from your repo to save space, or something like that.

The -f (force) option will also remove files, that are not tracked and are also being ignored by git though ignore-rule. In the case above, with an ignore-rule to never track the bin/obj folders, even though these folders are being ignored by git, using the force-option will remove them from your file system. I've sporadically seen a use for this, e.g. when scripting deployment, and you want to clean your code before deploying, zipping or whatever.

Git clean will not touch files, that are already being tracked.

Checkout "dot"

git checkout .

I had actually never seen this notation before reading your post. I'm having a hard time finding documentation for this (maybe someone can help), but from playing around a bit, it looks like it means:

"undo all changes in my working tree".

I.e. undo unstaged changes in tracked files. It apparently doesn't touch staged changes and leaves untracked files alone.

Stashing

Some answers mention stashing. As the wording implies, you would probably use stashing when you are in the middle of something (not ready for a commit), and you have to temporarily switch branches or somehow work on another state of your code, later to return to your "messy desk". I don't see this applies to your question, but it's definitely handy.

To sum up

Generally, if you are confident you have committed and maybe pushed to a remote important changes, if you are just playing around or the like, using git reset --hard HEAD followed by git clean -f will definitively cleanse your code to the state, it would be in, had it just been cloned and checked out from a branch. It's really important to emphasize, that the resetting will also remove staged, but uncommitted changes. It will wipe everything that has not been committed (except untracked files, in which case, use clean).

All the other commands are there to facilitate more complex scenarios, where a granularity of "undoing stuff" is needed :)

I feel, your question #1 is covered, but lastly, to conclude on #2: the reason you never found the need to use git reset --hard was that you had never staged anything. Had you staged a change, neither git checkout . nor git clean -f would have reverted that.

Hope this covers.

2 of 10
37

Reason for adding an answer at this moment:

So far I was adding the conclusion and ‘answers’ to my initial question itself, making the question very lengthy, hence moving to separate answer.

I have also added more frequently used git commands that helps me on git, to help someone else too.

Basically to clean all local commits $ git reset --hard and $ git clean -d -f


First step before you do any commits is to configure your username and email that appears along with your commit.

#Sets the name you want attached to your commit transactions

$ git config --global user.name "[name]"

#Sets the email you want atached to your commit transactions

$ git config --global user.email "[email address]"

#List the global config

$ git config --list

#List the remote URL

$ git remote show origin

#check status

git status

#List all local and remote branches

git branch -a

#create a new local branch and start working on this branch

git checkout -b "branchname" 

or, it can be done as a two step process

create branch: git branch branchname work on this branch: git checkout branchname

#commit local changes [two step process:- Add the file to the index, that means adding to the staging area. Then commit the files that are present in this staging area]

git add <path to file>

git commit -m "commit message"

#checkout some other local branch

git checkout "local branch name"

#remove all changes in local branch [Suppose you made some changes in local branch like adding new file or modifying existing file, or making a local commit, but no longer need that] git clean -d -f and git reset --hard [clean all local changes made to the local branch except if local commit]

git stash -u also removes all changes

Note: It's clear that we can use either (1) combination of git clean –d –f and git reset --hard OR (2) git stash -u to achieve the desired result.

Note 1: Stashing, as the word means 'Store (something) safely and secretly in a specified place.' This can always be retreived using git stash pop. So choosing between the above two options is developer's call.

Note 2: git reset --hard will delete working directory changes. Be sure to stash any local changes you want to keep before running this command.

# Switch to the master branch and make sure you are up to date.

git checkout master

git fetch [this may be necessary (depending on your git config) to receive updates on origin/master ]

git pull

# Merge the feature branch into the master branch.

git merge feature_branch

# Reset the master branch to origin's state.

git reset origin/master

#Accidentally deleted a file from local , how to retrieve it back? Do a git status to get the complete filepath of the deleted resource

git checkout branchname <file path name>

that's it!

#Merge master branch with someotherbranch

git checkout master
git merge someotherbranchname

#rename local branch

git branch -m old-branch-name new-branch-name

#delete local branch

git branch -D branch-name

#delete remote branch

git push origin --delete branchname

or

git push origin :branch-name

#revert a commit already pushed to a remote repository

git revert hgytyz4567

#branch from a previous commit using GIT

git branch branchname <sha1-of-commit>

#Change commit message of the most recent commit that's already been pushed to remote

git commit --amend -m "new commit message"
git push --force origin <branch-name>

# Discarding all local commits on this branch [Removing local commits]

In order to discard all local commits on this branch, to make the local branch identical to the "upstream" of this branch, simply run

git reset --hard @{u}

Reference: http://sethrobertson.github.io/GitFixUm/fixup.html or do git reset --hard origin/master [if local branch is master]

# Revert a commit already pushed to a remote repository?

$ git revert ab12cd15

#Delete a previous commit from local branch and remote branch

Use-Case: You just commited a change to your local branch and immediately pushed to the remote branch, Suddenly realized , Oh no! I dont need this change. Now do what?

git reset --hard HEAD~1 [for deleting that commit from local branch. 1 denotes the ONE commit you made]

git push origin HEAD --force [both the commands must be executed. For deleting from remote branch]. Currently checked out branch will be referred as the branch where you are making this operation.

#Delete some of recent commits from local and remote repo and preserve to the commit that you want. ( a kind of reverting commits from local and remote)

Let's assume you have 3 commits that you've pushed to remote branch named 'develop'

commitid-1 done at 9am
commitid-2 done at 10am
commitid-3 done at 11am. // latest commit. HEAD is current here.

To revert to old commit ( to change the state of branch)

git log --oneline --decorate --graph // to see all your commitids

git clean -d -f // clean any local changes

git reset --hard commitid-1 // locally reverting to this commitid

git push -u origin +develop // push this state to remote. + to do force push

# Remove local git merge: Case: I am on master branch and merged master branch with a newly working branch phase2

$ git status

On branch master

$ git merge phase2 $ git status

On branch master

Your branch is ahead of 'origin/master' by 8 commits.

Q: How to get rid of this local git merge? Tried git reset --hard and git clean -d -f Both didn't work. The only thing that worked are any of the below ones:

$ git reset --hard origin/master

or

$ git reset --hard HEAD~8

or

$ git reset --hard 9a88396f51e2a068bb7 [sha commit code - this is the one that was present before all your merge commits happened]

#create gitignore file

touch .gitignore // create the file in mac or unix users

sample .gitignore contents:

.project
*.py
.settings

Reference link to GIT cheat sheet: https://services.github.com/on-demand/downloads/github-git-cheat-sheet.pdf

🌐
Git
git-scm.com › docs › git-clean
Git - git-clean Documentation
Specify -d to have it recurse into such directories as well. If a <pathspec> is specified, -d is irrelevant; all untracked files matching the specified paths (with exceptions for nested git directories mentioned under --force) will be removed.
🌐
Better Stack
betterstack.com › community › questions › how-to-remove-local-untracked-files-from-current-git-working-tree
How Do I Remove Local (Untracked) Files from the Current Git Working Tree? | Better Stack Community
June 21, 2024 - To actually remove the untracked files and directories, you can run the git clean command: ... The -f or --force option is used to forcefully remove untracked files and directories.
🌐
Hrekov
hrekov.com › blog › git-discard-local-changes
Git: Discard All Local Changes and Pull the Latest Remote Version | Web Tools, Production APIs & Technical Blog | Hrekov
September 6, 2025 - You can do this by temporarily stashing them before the reset: ... This adds .gitignore to your stash, a temporary storage area. git fetch origin main git reset --hard origin/main ...
Top answer
1 of 16
2625
  • This will unstage all files you might have staged with git add:

      git reset
    
  • This will revert all local uncommitted changes (should be executed in repo root):

      git checkout .
    

    You can also revert uncommitted changes only to particular file or directory:

      git checkout [some_dir|file.txt]
    

    Yet another way to revert all uncommitted changes (longer to type, but works from any subdirectory):

      git reset --hard HEAD
    
  • This will remove all local untracked files, so only git tracked files remain:

      git clean -fdx
    

    WARNING: -x will also remove all ignored files, including ones specified by .gitignore! You may want to use -n for preview of files to be deleted.


To sum it up: executing commands below is basically equivalent to fresh git clone from original source (but it does not re-download anything, so is much faster):

git reset
git checkout .
git clean -fdx

Typical usage for this would be in build scripts, when you must make sure that your tree is absolutely clean - does not have any modifications or locally created object files or build artefacts, and you want to make it work very fast and to not re-clone whole repository every single time.

2 of 16
253

If you wish to "undo" all uncommitted changes simply run:

git stash
git stash drop

If you have any untracked files (check by running git status), these may be removed by running:

Warning: This will remove all non-commited data, even what is in .gitignore

git clean -fdx

git stash creates a new stash which will become stash@{0}. If you wish to check first you can run git stash list to see a list of your stashes. It will look something like:

stash@{0}: WIP on rails-4: 66c8407 remove forem residuals
stash@{1}: WIP on master: 2b8f269 Map qualifications
stash@{2}: WIP on master: 27a7e54 Use non-dynamic finders
stash@{3}: WIP on blogit: c9bd270 some changes

Each stash is named after the previous commit messsage.

🌐
Quora
quora.com › How-do-I-remove-local-untracked-files-from-my-current-Git-branch
How to remove local (untracked) files from my current Git branch - Quora
Answer (1 of 8): As per the Git Documentation git clean > Remove untracked files from the working tree Step 1 is to show what will be deleted by using the [code ]-n[/code] option: [code]git clean -n [/code]Clean Step - beware: this will delete files: [code]git clean -f [/code] * To remove...
🌐
JanBask Training
janbasktraining.com › community › devops › how-do-i-revert-all-local-changes-in-git-managed-project-to-previous-state
How do I revert all local changes in Git managed project to previous state? | JanBask Training Community
August 18, 2025 - Reverting all local changes in a Git-managed project is a common need when you want to discard your modifications and go back to a clean state. Depending on whether your changes are staged, unstaged, or untracked, Git provides different commands to handle each case. ... Be careful—this permanently removes changes that are not committed.