Remove Un-Tracked files in Git

May 30, 2020

Git-Untracked Files

Untracked Files

Untracked files are any files in your working directory that were not in your last snapshot(version) and are not in your staging area(state before commit ).

When you create a new file in your repository, then the file will be listed as untracked file.

git clean

To remove Un-Tracked files you need to use git clean, it Cleans the working tree by recursively removing files that are not under version control, starting from the current directory.

1. Listing un-tracked Files

To only the un-tracked files which will be deleted, you need to use n option with the git clean command.

git clean -n

-n is short form for --dry-run.

Interactive Mode

  • i options used to set interactive mode , you will get a quick overview of what is going to be deleted offering you the possibility to include/exclude the affected files.
git clean -i

2.Delete un-tracked Files

To delete un-tracked files , you -f options.

git clean -f

3.Delete un-tracked files and directories(folders)

Use -f option tp remove un-tracked directory

git clean -f -d

You can combine both -f and -d

git clean -fd

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

You will get Message like

clean.requireForce defaults to true and neither -i, -n, nor -f given; refusing to clean

4.To remove only Ignored files

-X option is used to remove Ignored files in git

git clean -f -X

A gitignore file specifies intentionally untracked files that Git should ignore.

5.To remove Ignored and non-ignored files

-x option is used to remove Ignored files in git

git clean -f -x

Conclusion

git clean Removes un-tracked Files

  • -n list untracked files
  • -i list untracked files in interactive mode
  • -f Remove untracked files
  • -fd Remove untracked directories
  • -fX Remove only ignored files
  • -fx Remove ignored and other files(un-tracked files )

Please donate here, for making more tutorials.