Git Clean command
Git clean command cleans the working tree by recursively removing files that are not under version control, starting from the current directory(Ideally i recommend to be at the root directory of the Git repo).
Normally, only files unknown to Git are removed, but if the -x option is specified, ignored files are also removed. Be double sure before using -x option.
arguments are given, only those paths are affected.
If clean.requireForce is set to “true” (the default) in your configuration, one needs to specify -f otherwise nothing will actually happen.
Table of Contents
- Print the list of files which will be removed (Like a dry run, always perform this!)
- Delete the files from the repository
- Delete the directories from the repository
- Delete the ignored files from the repository
- Delete the ignored and non-ignored files from the repository
- Options details
git clean -n
Note: Below commands will delete files
git clean -f
git clean -f -d
git clean -f -X
git clean -f -x
You can even combine arguments, ex:
- dry run to clean directories run
git clean -n -d
- dry run to clean files run
git clean -n -d
- clean files from root of git repo
git clean -f :/
refer official documentation for more information - Git - git-clean Documentation
|-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.|