How to preserve all ignored files in git clean -fd?

When I have .gitignore data/* and run git clean -fd, the data folder and all its content files are deleted.

What I want is to delete all unrevisioned files in a git repo while excluding all ignored files (i.e. DON’T delete gitignored files). What could I do?

  • Cannot rebuild/deploy service fabric project after git clean with empty .gitignore file
  • How can I recover files after accidentally running “git clean -df”?
  • Exclude files from git clean by default
  • How to use git clean remove all file list in .gitignore?
  • git clean -d -f removes directories containing files which are ignored. Can I keep the ignored files and still run git clean -d
  • git clean exclude with regex
  • Are all my files deleted when issuing git clean -f?
  • Difference between git reset --hard and git clean
  • One Solution collect form web for “How to preserve all ignored files in git clean -fd?”

    Git normally don’t clean ignored files unless you specify the -x flag, but strangely it cleans out when configured as you did (folder/*).

    As @VonC commented it out, you should change your .gitignore to ignore the directory (data/) rather than what’s in it (data/*).

    It’s a subtle difference, but it matters to git.

    Git Baby is a git and github fan, let's start git clone.