14

I have tracked many many files early, but I don't want Git to track them any more from now on.

Can I untrack those files according to a .gitignore file?

There are too many files and they are separated in many different directories, so it is not practical to remove them one-by-one, instead, I hope they can be untracked according to patterns in a .gitignore file.

Scott Weldon
  • 8,912
  • 6
  • 47
  • 64
Yishu Fang
  • 8,880
  • 20
  • 59
  • 100
  • 1
    Possible duplicate of [How to make Git "forget" about a file that was tracked but is now in .gitignore?](https://stackoverflow.com/questions/1274057/how-to-make-git-forget-about-a-file-that-was-tracked-but-is-now-in-gitignore) – Senseful Feb 14 '19 at 20:51

3 Answers3

16

You need to remove the files from the index.

git rm -r --cached . 

and then add

git add .

Finally commit:

git commit -a -m "Untrack ignored files!"
devnull
  • 111,086
  • 29
  • 224
  • 214
3

You can stop tracking already-tracked files by staging a deletion of the file in the index:

git rm --cached path/to/ignored/file

... and committing.

With --cached this won't actually delete the file in your working copy. (I'm always paranoid and copy them somewhere safe first, anyway.)

Ash Wilson
  • 20,348
  • 3
  • 33
  • 45
-2

Use git rm to delete the files that you no longer wish to track, and then add those files to .gitignore and they should no longer show up in your list of unstaged changes.

chooban
  • 8,606
  • 2
  • 19
  • 35