4

If I understand right, in order to "ignore" a file so it won't come up when I type git status or git add -A I need to add it to the .gitignore file.

My goal is to ignore a certain folder and file when committing changes to Git, permanently.

I tried editing the .gitignore file in my editor:

config/
src/package.xml

I saved the file, committed and pushed to Git, refreshed my project from another repository, typed git status and still saw the xml file and the folder.

Also tried using GItHub Desktop by choosing "Discard Changes", it automatically added to .gitignore the following lines:

*.sublime-workspace
config/.local_store
*.sublime-workspace
src/package.xml

but same result...

Project Structure:

          Full
    ________|____________
   |        |            |
config  .gitignore      src
   |                     |
  ...                package.xml
Json
  • 655
  • 10
  • 27
  • 3
    If the file had already been added to the repo, it will need to be removed before the .gitignore will ignore it. – circusdei Aug 22 '17 at 20:42
  • If you downvote a question, add a comment to explain why – Json Aug 22 '17 at 20:43
  • Possible duplicate of [Ignore files that have already been committed to a Git repository](https://stackoverflow.com/questions/1139762/ignore-files-that-have-already-been-committed-to-a-git-repository) – phd Aug 22 '17 at 22:29

2 Answers2

8

.gitignore doesn't apply retroactively. It is read by git add only when a file is added for the first time to the repo.

If the file is already in the repo, listing it in gitignore is not enough. You also need to remove it from the repo (git rm --cached src/package.xml).

Don't forget to git add .gitignore and git commit, in order to persist the changes.

axiac
  • 68,258
  • 9
  • 99
  • 134
  • Thank you! I didn't know that I have to delete the folder/file from Git in order to ignore them. just one thing, I had to add -r to the remove command: git rm -r --cached src/package.xml – Json Aug 23 '17 at 09:39
  • It is explained in the first paragraph of the documentation of [`.gitignore`](https://git-scm.com/docs/gitignore): *"A `gitignore` file specifies intentionally untracked files that Git should ignore. Files already tracked by Git are not affected."*. Also available on your computer using `git help gitignore`. – axiac Aug 23 '17 at 09:41
0

If your XML file is already tracked and you want to keep it, you can ignore the new modifications with the command described in this post. Otherwise you will have to remove it as mentioned in the comments.

Note that an alternative to .gitignore is to use .git/info/exclude.

piarston
  • 1,685
  • 1
  • 13
  • 25