1

Here's the content of file .gitignore (located in the project's main folder)

File /Volumes/drive2/my-project/.gitignore

.project

/config/file-im-trying-to-ignore.txt
config/file-im-trying-to-ignore.txt
file-im-trying-to-ignore.txt

These are the steps I used when making the changes:

  • edit .gitignore
  • commit
  • edit file-I'm-trying-to-ignore.txt

What am I doing wrong?

I've committed my repository after making changes to .gitignore and then made changes to file-I'm-trying-to-ignore.txt

I used these two sites as reference:

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
mrjayviper
  • 2,258
  • 11
  • 46
  • 82
  • 1
    May be a long shot, but try investigating encoding of a file - I had trouble for long time with this - it simply did not work as it should. It turned out I had some wild encoding (othher than any UTF or Unicode), which was not correctly understood by git. Make sure it is set to UTF-8 – Michał Turczyn Jan 11 '21 at 06:43
  • 2
    Does [this answer](/a/36713835/4265352) help? – axiac Jan 11 '21 at 06:45
  • @axiac that fixed it for me. Can you please leave that as an answer so I can accept it? Thank you. – mrjayviper Jan 11 '21 at 06:49

3 Answers3

1

Is your file already indexed? .gitignore affects only files that are not indexed by Git (staged or committed).

If your file just staged and never committed before - you should unstage file with git reset HEAD your_file. Docs.

Or, if your file was committed previously, thus indexed in Git, you should remove it from index with git rm --cached your_file, then commit. Then .gitignore takes effect. Docs. Clean the file from .gitignore while removing it from cache, then add it back there.

Som-1
  • 601
  • 7
  • 16
  • Git reset might be used in both cases with appropriate flag (`--soft` `--medium` or `--hard`) – Michał Turczyn Jan 11 '21 at 06:52
  • Well, probably. Have not used it this way, but as I remember, it will reset the whole commit, and from the latest back, not the single file. Flags just specify what changes to keep or delete in the working tree. – Som-1 Jan 11 '21 at 07:05
  • Yes, you are correct :) Forgot about that. – Michał Turczyn Jan 11 '21 at 08:09
1

If the file is already committed and pushed to your repository, you must untrack the file where you will add to .gitignore, and then .gitignore only works on untracked files. To untrack files, run following command:

# Untrack all files
git rm -r --cache

# Untrack one specific file
git rm --cache filename.txt

After untracking the file, you can add (git add .), commit (git commit -m ".gitignore now ignores the file") and push (git push) it to your repository.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
SwissCodeMen
  • 4,222
  • 8
  • 24
  • 34
-1

Try using git reset to untrack the file.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131