Showing and cleaning ignored files

Ignoring files is useful to filter noise from the output of git status. But, sometimes it is required to check which files are ignored. This example will show you how to do that.

Getting ready

We'll continue in the repository from the last example.

How to do it...

To show the files we have ignored, we can use the clean command. Normally, the clean command will remove the untracked files from the working directory but it is possible to run this in a dry-run mode, -n, where it just shows what will happen.

$ git clean -Xnd
Would remove bin/foobar
Would remove test.test
Would remove test.txt.bak

The options used in the preceding command specify the following:

  • -n, --dry-run: Only lists that will be removed
  • -X: Removes only the files ignored by Git
  • -d: Removes the untracked directories in addition to the untracked files

The ignored files can also be listed with the ls-files command:

$ git ls-files -o -i --exclude-standard
bin/foobar
test.test
test.txt.bak

Where the option -o, --others shows the untracked files, the option -i, --ignored shows only the ignored files, and --exclude-standard, use the standard exclusion files .git/info/exclude and .gitignore in each directory, and the user's global exclusion file.

There's more…

If we need to remove the ignored files, we can of course use git clean to do this; instead of the dry-run option, we pass the force option, -f:

$ git clean -Xfd
Removing bin/foobar
Removing test.test
Removing test.txt.bak

To remove all the untracked files and not just the ignored files, use git clean -xfd. The lower case x means we don't use the ignore rules, we just remove everything that is not tracked by Git.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset