Undoing Git Changes | git clean
The git clean command
git clean deletes untracked files from the working directory. You can use git status to check untracked files and remove them manually, so this command mainly exists for convenience. Like the regular rm command, git clean cannot be undone, so confirm that you really want to delete the untracked files before running it.
git clean is often combined with git reset --hard. As explained earlier, reset only affects tracked files, so another command is needed to clean up untracked files. Using these two commands together lets you return the working directory to exactly the same state as a specific commit.
Usage
git clean -n
Runs a dry run of git clean. It shows the files that would be deleted, but does not actually delete them.
git clean -f
Deletes untracked files from the current directory. Unless the clean.requireForce option is set to false, which is not the default, the -f force flag is required. This command does not delete untracked directories or files ignored by .gitignore.
git clean -f <path>
Deletes untracked files, but limits the scope to the specified path.
git clean -df
Deletes untracked files and untracked directories from the current directory.
git clean -xf
Deletes untracked files and files that Git would normally ignore from the current directory.
Additional notes
git reset --hard and git clean -f are often used together when you want to discard the results of local work and start over. After running both commands, the working directory returns to the state of the previous commit, and you can restart from there.
git clean is also useful for cleaning the working directory after a build. For example, it can easily remove .o files or .exe binaries created by a C compiler. This may be necessary when packaging a project for release. The -x option is useful for this purpose.
Remember that git clean, together with git reset, is one of the few Git commands that can permanently delete content. Because users often lose important work with it, Git commonly requires the -f flag by default. This helps prevent accidental data loss from a simple git clean command.
Example
The following example discards all changes in the working directory, including newly created files. Assume that several snapshots have already been committed and that you are in the middle of a new experimental change.
# Edit some existing files
# Add some new files
# Realize you have no idea what you're doing
# Undo changes in tracked files
git reset --hard
# Remove untracked files
git clean -df
After this sequence of reset and clean commands, the working directory and staging area match the previous commit exactly. Running git status shows that the working directory is clean, so you can start again from that state.
Unlike the second example in the git reset section, note that the newly created files were never staged in the repository. Because those files are untracked, they are not affected by git reset --hard; deleting them requires git clean.