How to make .gitignore ignore files?

Viewed 914

I have a .gitignore file with the following contents:

.vs/

I run the following commands

git add .gitignore
git commit -m 'adding ignore file'

In my .vs folder, I have a text file named test.txt.

When I type git status, I see the following output

On branch master
Untracked files:
  (use "git add <file>..." to include in what will be committed)

        .vs/

nothing added to commit but untracked files present (use "git add" to track)

What am I missing here?

4 Answers

Found my solution here (Visual Studio was adding Chinese characters to my .gitignore also)

My .gitignore was saved with unicode encoding.

This is because I created my .gitignore file via PowerShell like so

".vs/" > .gitignore

I did this because explorer doesn't let you create a text file with a leading period. My solution:

".vs/" | Out-File -Encoding ascii -FilePath .gitignore
  1. Use correct patterns. .vs must be in same directory as .gitignore, if not, correct the path.
  2. Do commit .gitignore, so that others benefit from it.
  3. If something is already included (tracked) by git, and you later add it to .gitignore, it won't cause git to forget it. For this:
    1. Use git rm --cached <filePath> for file(s)
    2. Use git rm -r --cached <dirPath> for a directory

First, you don't need to add .gitignore to the index and commit it for said file to be effective.

A git status done after creating/updating a .gitignore will work.

Second, make sure your .gitignore is at the same level (or above) as .vs/ (not inside it)

Third, check its eol (end of line, make sure it is linux-style)

What I think from the logs is that you have added the folder in .gitignore but still since it was committed earlier you want to remove it.

If is present in earlier commits and you want to remove it. You can use :-

git rm --cached .vs/

this will mark the folder as deleted from index but it will be unchanged in local.

Then use:

git commit -m "<Msg showing removing file from git tracking.>"

I have taken a certain assumption, Please clarify if it's not what you need.

Related