129

Yesterday I spent several hours debugging a problem with my git repo that wasn't fixed by git reset HEAD --hard because the files causing the problem were ignored by .gitignore. Is there a way to "flush" or "clean" a git repo of all files that are being ignored, so that only the files tracked by git are present?

I finally fixed my problem by deleting the repo and cloning it from github again, but in the future, I would like to immediately remove all potentially problematic files (those that are being ignored).

steamer25
  • 9,278
  • 1
  • 33
  • 38
Patrick Kenny
  • 4,515
  • 7
  • 47
  • 76
  • 2
    Try playing around with [`git clean`](https://git-scm.com/docs/git-clean) – Ferrybig Sep 18 '17 at 06:47
  • 11
    The marked issue is not a duplicate. This talks about ignored files, the other one talks about untracked files. – Paul Jul 15 '18 at 13:59
  • @Patrick Please fix the answer or the title! The title says "all ignored" and not just "untracked" – Aaaaaaaa Sep 01 '23 at 06:44

2 Answers2

261
git clean -dfX

git-clean - Remove untracked files from the working tree
-d for removing directories
-f remove forcefully
-n Don’t actually remove anything, just show what would be done.
-X Remove only files ignored by Git. This may be useful to rebuild everything from scratch, but keep manually created files.

If the ignored files are already added to the index/staging, you must remove the files from the tracking index before using the above clean command.

git rm -rf --cached .

Then add the files except the ones included in the .gitignore file

git add . and create a commit with git commit -m "message"

Samuel Robert
  • 10,106
  • 7
  • 39
  • 60
  • 1
    In my case, because of the way the repo is configured to build projects with composer, I also needed to add the `-x` flag; `-df` alone didn't give me anything to remove. – Patrick Kenny Sep 18 '17 at 06:56
  • @PatrickKenny That's a good one.. including it in the answer – Samuel Robert Sep 18 '17 at 06:58
  • 11
    this is completely incorrect answer! it talks about `untracked`, not `ignored` files. – Alex Skrypnyk Feb 11 '19 at 03:12
  • 5
    You can also use the `x` flag instead of `X` to delete ignored *and* untracked files. – Eric Ferreira Mar 07 '19 at 18:26
  • 2
    Doesnt remove ignored files. – wilmol Dec 07 '20 at 01:09
  • 1
    @wilmol If the files are already added to the staging area, then the clean command cannot remove it. So you must remove the files from the staging first by `git rm -rf --cached .` and add the files except the ignored files in the staging area by `git add .` – Samuel Robert Dec 08 '20 at 16:50
32

There is a single command solution:

git ls-files --ignored --exclude-standard | sed 's/.*/"&"/' | xargs git rm -r --cached

What it does is:

  • List all ignored files
  • Handle paths with spaces to avoid failure
  • Call git rm -r --cached to remove all the ignored files from index (without removing them from your local machine)
Lidia Parrilla
  • 549
  • 3
  • 13