4

I have a file in my branch that I would like ignored. I added it to .gitignore but, when I change it, it still shows under deltas in git status. gitignore does work for previously not versioned files.

Is the reason gitignore doesn't work in this instance the fact that the file in question has already been versioned? How do I circumvent this and ignore it on a going forward basis?

janos
  • 120,954
  • 29
  • 226
  • 236
amphibient
  • 29,770
  • 54
  • 146
  • 240
  • @janamargaret if the file is already tracked, then the gitignore will have no bearing on it. The file needs to first become untracked – Jonathan.Brink Aug 20 '15 at 19:27
  • @janamargaret no you don't need to commit `.gitignore`, it takes effect when it exists, committed or not. That being said, it's a recommended practice to add `.gitignore` to version control. – janos Dec 27 '16 at 06:06

2 Answers2

4

Yes, this is not working because the file is already being tracked by Git.

An approach would be to first delete the file and commit.

Then re-add the file (along with the entry to .gitignore) and commit again.

Another potential approach: How to make Git "forget" about a file that was tracked but is now in .gitignore?

Community
  • 1
  • 1
Jonathan.Brink
  • 23,757
  • 20
  • 73
  • 115
3

The files which have already been added to version control are not ignored even if they are under .gitignore

To start ignoring these files, they should first be removed from version control. They can be removed using git rm --cached <file> command. The --cached option removes files from version control while preserving them on disk. Now, the <file> should stop appearing in list of updated files.

Aditya Kadakia
  • 1,359
  • 9
  • 10