Adam Drewery
Adam Drewery

Reputation: 1546

.gitignore being ignored by Visual Studio?

I have a git project I work on using Visual Studio 2013 and Git.

I have noticed many, if not all, files listed in my .gitignore file are still being listed as pending changes in the Team Explorer window.

However, when doing a git status using bash, I don't see the files (as would be expected). Why is this happening, and more importantly, how can I have Visual Studio treat my .gitignore file the same as git bash?

.gitignore file:

GitIgnore file

Pending Changes Window (Team Explorer):

Pending Changes Window

Upvotes: 7

Views: 3442

Answers (4)

Chris H
Chris H

Reputation: 962

This is an old question but still relevant. In my case on Visual Studio Community 2015, .gitignore is being processed BUT it interprets the rules incorrectly.

For example, this .gitignore file works in the CLI git client - it ignores everything but the subtree in /Assets/Scripts, and the .gitignore file.

# Ignore everything
*
# But descend into directories
!*/
# Recursively allow files under subtree
!/Assets/Scripts/**
#don't ignore this file
!.gitignore

When VS parses the above .gitignore file, it seems to incorrectly interpret an allowed folder as: Allow this folder, plus all files and folders within it recursively - so every subtree incorrectly shows up in the repository. To achieve the same result as the code above from within Visual Studio, I had to change it to this: Note the following code works in VS, but no longer works in the Git CLI.

# Ignore everything
*
#But don't ignore the /assets folder
!/Assets/
#Ignore everything inside the assets folder
/Assets/*
#but don't ignore the Assets/Scripts subtree
!/Assets/Scripts
#don't ignore this file
!.gitignore

Upvotes: 0

Spencer Williams
Spencer Williams

Reputation: 851

I want to share my very similar experience in Visual Studio 2017, even though in my case git on the command line was not acknowledging the files listed in my .gitignore.

I had used Powershell to create my .gitignore file with the command echo '' > .gitignore. This created a file encoded in UCS-2 LE BOM instead of UTF-8. As a result, git would list the file in git status but none of the specified files were ignored until I converted it to UTF-8. So... be careful about creating an empty file with such a Powershell command, as much as I would love to get on the UTF-16 bandwagon.

As referenced in another answer, I should have used a command like echo '' | Out-File .\.gitignore -encoding Utf8 to initially create the file, although that creates a UTF-8 file with the BOM.

Also, the following command, as I learned from yet another answer, will copy the content from a file and create the new file with just regular UTF-8 without the BOM:

Get-Content -Encoding utf8 .\.gitignore.bak | Out-File -Encoding utf8 .\.gitignore

Upvotes: 1

Amged Rustom
Amged Rustom

Reputation: 1868

To make Visual Studio 2013 include changes you made to the .gitignore file, delete the file ms-persist.xml in your solution's .git folder.

Upvotes: 4

Jim Skerritt
Jim Skerritt

Reputation: 4596

I was having the same problem with linked files. Team Explorer claimed I was adding the linked files, so I added them all to my .gitignore. git status was now clean, but Team Explorer continued to show these files as being added.

Right clicking the list of files in Team Explorer changes and selecting Undo seemed to work. The files themselves were not deleted, and they were removed from my list of changes.

Seems like Team Explorer just doesn't immediately pick up on changes to .gitignore. Hope this helps.

Upvotes: 4

Related Questions