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
-xoption 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 -dorgit clean -fd - To remove ignored files, run
git clean -f -Xorgit clean -fX - To remove ignored and non-ignored files, run
git clean -f -xorgit 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,--forceIf the Git configuration variable clean.requireForce is not set to false, git clean will refuse to run unless given
-f,-nor-i.
-xDon’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-eoptions. 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.
-XRemove only files ignored by Git. This may be useful to rebuild everything from scratch, but keep manually created files.
-n,--dry-runDon’t actually remove anything, just show what would be done.
-dRemove 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
-foption twice if you really want to remove such a directory.
git checkout force deletes tracked directory containing untracked files
Reset and Checkout differ from git's handling of untracked files
Lost untracked files
"The following untracked working tree files would be overwritten by checkout"
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
-xoption 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 -dorgit clean -fd - To remove ignored files, run
git clean -f -Xorgit clean -fX - To remove ignored and non-ignored files, run
git clean -f -xorgit 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,--forceIf the Git configuration variable clean.requireForce is not set to false, git clean will refuse to run unless given
-f,-nor-i.
-xDon’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-eoptions. 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.
-XRemove only files ignored by Git. This may be useful to rebuild everything from scratch, but keep manually created files.
-n,--dry-runDon’t actually remove anything, just show what would be done.
-dRemove 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
-foption twice if you really want to remove such a directory.
Use git clean -f -d to make sure that directories are also removed.
Don’t actually remove anything, just show what would be done.
git clean -nor
git clean --dry-runRemove 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
-foption 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.
I believe I have lost forever a folder with some data that I had gitignored. This folder had never been tracked by git, so there is no way of retrieving it I believe.
This folder disappeared after a branch checkout, and is now gone from all branches. Looking for a solution I found that having multiple different .gitignore files could cause this issue, but all .gitignore files are the same in every branch as I have not modified it in some time.
I believe this is not the intended behavior of git, what could I have done wrong to lose this folder? Is there a way to protect gitignored files from being deleted by git?
Thanks in advance.
I don't understand this message. What it's saying seems to indicate to me that the files being listed should show up when I do git status, but nothing does. Why is that? I must have a fundamental misunderstanding here.
The problem is that you are not tracking the files locally but identical files are tracked remotely so in order to "pull" your system would be forced to overwrite the local files which are not version controlled.
Try running
git add *
git stash
git pull
This will track all files, remove all of your local changes to those files, and then get the files from the server.
⚠️ Be aware
git cleancan delete the untracked files which can be valuable! Andgitwon't be able to help to restore those files!
You can try command to clear the untracked files from the local
Git 2.11 and newer versions:
git clean -d -f .
Older versions of Git:
git clean -d -f ""
Where -d can be replaced with the following:
-xignored files are also removed as well as files unknown to Git.-dremove untracked directories in addition to untracked files.-fis required to force it to run.
Here is the link that can be helpful as well.
Context of the problem:
I created a new feature branch (breaking change feature) and created bunch of files in it (not ready for commit yet).
Now I have need to go back to my main and do some not related hotfix.
But when I checkout to my main branch all new files from feature branch are still there, and because its a breaking change I can't run npm run dev without tons of errors.
I want to see my main branch as it is in my github repo without bringing any changes from.
Commands I've used:git stash // on feature branchgit checkout main // on feature branchnpm run dev // results in errors, due to breaking changes in new files
Research I have completed prior to requesting assistance:
Googled the title of this post.
Looked at stack overflow solutions telling to use git stash / git switch, but that didn't help.
----------
Solved.
Changed branches via GitHub Desktop app leaving all changes on feature branch.Why terminal git commands didn't work for me - I have no idea.