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.

Optionally, ...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

  1. Print the list of files which will be removed (Like a dry run, always perform this!)
  2. Delete the files from the repository
  3. Delete the directories from the repository
  4. Delete the ignored files from the repository
  5. Delete the ignored and non-ignored files from the repository
  6. Options details

Print the list of files which will be removed (Like a dry run, always perform this!)

1
git clean -n

Note: Below commands will delete files

Delete the files from the repository

1
git clean -f

Delete the directories from the repository

1
2
3
4
5
git clean -f -d 

or

git clean -fd

Delete the ignored files from the repository

1
2
3
4
5
git clean -f -X 

or

git clean -fX

Delete the ignored and non-ignored files from the repository

1
2
3
4
5
git clean -f -x 

or

git clean -fx

Note: case sensitive 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


Options details

Command       Description
-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.