1

Probably a repeat, but I can't find an answer.

So I have a .gitignore file:

transactions.csv
transactions_test.csv
**/test.gnucash*
test
**/test.gnucash.2*

And the following directory:

LICENSE    test.gnucash                         transactions_test.csv
main.py    test.gnucash.20180615182021.gnucash  transactions_testPUBLIC.csv
README.md  test.gnucash.20180618121545.gnucash  translations.json
test/      transactions.csv

However when I run git ls-tree -r master --name-only I get:

.gitignore
.vscode/settings.json
LICENSE
README.md
main.py
test.gnucash.20180615182021.gnucash
test.gnucash.20180618121545.gnucash
transactions_testPUBLIC.csv
translations.json

Even weirder than that, if I run this through a glob tester, it works correctly. (see hyperlink)

Any ideas as to what's happening? I'm running Git for Windows on Windows 10.

$ git --version
git version 2.17.0.windows.1
James Wright
  • 1,293
  • 2
  • 16
  • 32
  • 1
    Possible duplicate of [Gitignore not working](https://stackoverflow.com/questions/25436312/gitignore-not-working) – phd Jun 18 '18 at 19:17
  • @phd yes, it's a duplicate of that post. I just didn't see it when I was looking at it before. – James Wright Jun 18 '18 at 23:01

2 Answers2

4

You have already added these files to git, haven't you? .gitignore does not affect files already known to git; it only excludes untracked files from consideration for addition.

So if you don't want to see a file, you first need to remove it from being tracked with git rm --cached [FILE TO BE REMOVED] (where --cached will prevent git from actually deleting files). As noted by @Bo-G, this will not completely remove a file from a repository's history; it will still be part of earlier revisions or other branches.

alexis
  • 48,685
  • 16
  • 101
  • 161
  • That was the issue! For future stragglers (if there are any), you can use `git rm --cached [FILE TO BE REMOVED]` to remove the file from tracking and add it to `.gitignore`. – James Wright Jun 18 '18 at 17:04
0

Old post but important, even if you use git rm --cached [FILE TO BE REMOVED] it will still exist commit history on github, if you published secrets by mistake

Bo G
  • 21
  • FYI, this would be more appropriate as a comment (hence the downvotes right now), but seeing as you don't have enough reputation to make a comment, you're kinda stuck in a catch22. I'll add your comment onto the accepted answer. – James Wright Dec 29 '21 at 16:00
  • 1
    The [GitHub documentation](https://docs.github.com/en/authentication/keeping-your-account-and-data-secure/removing-sensitive-data-from-a-repository) recommends that if you need to *COMPLETELY* delete Git objects (sensitive data, large files, etc.), use `git filter-branch` or [BFG Repo Cleaner](https://rtyley.github.io/bfg-repo-cleaner/). – FoggyDay Dec 29 '21 at 23:37