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?

  • How to get certain commit from GitHub project
  • Phabricator arc land won't work because that there are multiple revisions on feature branch
  • git subdirectory which is ignored
  • Git rebase one branch on top of another branch
  • How to search branch names in git?
  • git push sporadically fails on Jenkins with “Device not configured” error
  • Word-by-word blame/annotate in version control?
  • Should I merge master into a feature branch to bring it up to date? Would this be considered bad practice?
  • Git merge, then revert, then revert the revert
  • How to copy the last commit as a new commit in Git?
  • find difference between 2 remote branches
  • Why do files in the working directory get the modified status on switching between Git branches?
  • 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.