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
  • 9,673
  • 6
  • 48
  • 67
Yishu Fang
  • 9,448
  • 21
  • 65
  • 102
  • 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

2 Answers2

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
  • 118,548
  • 33
  • 236
  • 227
4

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
  • 22,820
  • 3
  • 34
  • 45