Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is visual studio committing files in my .gitignore file?

I'm using Visual Studio 2013 Express using Git on a project.

I created my repository and copied the project files into it from another directory. I then checked my .gitignore and .gitattributes files. .gitignore was pre-configured and looked to cover everything I needed to ignore for commits at this point.

I selected the Changes tab in Visual Studio under the "Team Explorer" tab and saw all my files were untracked, as expected. I then clicked "Add All". I assumed VS would only add files not matching those in my .gitignore to the "Included Changes" list. Instead, all the files were added to the "Included Changes" list.

I then thought that maybe the .gitignore file would be used when committing and that only files not matching those in .gitignore would be committed. So I added a commit message and clicked "Commit".

When I select "View History" from the "Actions" drop down in the "Changes" tab I am presented with a pane to the left of my "Team Explorer" view that shows all the commits I've made. There is only 1 commit. When I double click that commit, the "Team Explorer" tab displays Commit 8db34b1c and all the files in my project are listed there in the tree view with [add] after their name.

For example, App_Data/Movies.mdf and App_Data/Movies.ldf are listed even though my .gitignore includes the lines

# SQL Server files App_Data/*.mdf App_Data/*.ldf 

I don't understand how the .gitignore file is being used here or what the "Included Changes", "Excluded Changes" or "Untracked Files" sections mean.

An explanation of the above behavior and sections would be appreciated.

Edit--

I deleted my .git directory, created a new repo, copied my project files in again and then took screenshots of what I see.

Here are images of my .gitignore file and my "Changes" tab.

enter image description hereenter image description here

There's this SO question and this msdn question which shows that other people seem to be having issues as well. I tried deleting the .xml file added by VS in my .git directory (as noted in the msdn question) but that did not solve the issue for me.

Edit 2 --

This is my current directory structure :

/MvcMovie/     |--.git/     |     |---MvcMovie/     |       |--App_Data/     |       |     |--Movies.mdf     |       |     |--Movies.ldf     |       |          |       |--App_Start/     |       |--etc...     |     |--packages/     |--.gitignore     |--.gitattributes     |--MvcMovie.sln     |MvcMovie.v12.suo 
like image 246
seangwright Avatar asked Mar 09 '14 23:03

seangwright


People also ask

Do Gitignore files get committed?

gitignore file must be edited and committed by hand when you have new files that you wish to ignore. . gitignore files contain patterns that are matched against file names in your repository to determine whether or not they should be ignored.

How do I ignore files in Git using Visual Studio?

Visual Studio GitIn the Git Changes window, right-click any changed file that you want Git to ignore and choose Ignore this local item or Ignore this extension.

How do I stop Git from ignoring files?

If you already git add ed some files, their changes will still be tracked. To remove those files from your repository (but not from your file system) use git rm --cached on them. git rm --cached file_name. ext wroks fine for me to update gitignore for one file.


2 Answers

@dustinmoris is right in his answer, but the fix is not as immediately obvious as the procedure would lead you to believe. So let me explicitly walk you through the process to make sure the files in your .gitignore file are obeyed by Visual Studio.

1. Create Your Solution & Add It To Source Control Visual Studio 2013 Solution Creation

I created a sample MVVM Light XAML Solution and you can see that all the currently untracked files that should ideally be added to the next commit show up with a little green plus next to their icons.

Solution directory listing

The problem is that the MvvmLight.Win81_TemporaryKey.pfx file is listed to be added even though all files of type .pfx are explicitly called out to be ignored in my .gitignore.

gitignore

This is even more obvious if I select the solution in the Team Explorer panel and then select the Changes button. I see a list of all files pending inclusion on the next commit.

Team Explorer Changes

This list contains every file in the Solution, not just the subset specified by the .gitignore file.

2. Close Visual Studio & Delete the ms-persist.xml File

It's in a hidden directory so it's kind of hard to find. It should be located at the following path

Solution Directory\.git\ms-persist.xml

ms-persist.xml file

3. Re-Open The Solution and Commit Files

You'll notice that it appears as if your solution is no longer under source control because all the icons have lost their green plus signs, but don't worry it's fine.

Solution directory listing after deleting ms-persist.xml

Head on over to the Team Explorer panel and then select the Changes button. Again, you'll see that the project is still being tracked by source control, as well as a list of all files to be added on the next commit. More importantly, you'll notice that your .gitignore list was properly adhered to this time and all files specified as ignored are no longer listed as pending changes to be added to the next commit. As you can see below, the MvvmLight.Win81_TemporaryKey.pfx file is no longer scheduled for the next commit!

Corrected commit changes

Hope that helps someone. After all that hassle here is my honest advice:

Learn and use the command line interface since it provides the full range of functionality git offers and has the same interface on all platforms.

like image 125
Joel B Avatar answered Sep 21 '22 09:09

Joel B


If anyone ever wonders how to fix it. This is the solution: Go to your .git folder (might be hidden) and open up the ms-persist.xml and you can see the tracked files, excluded items, etc.

Just manually fix what VS has messed up and restart the IDE and you should be good again!

Don't delete the whole .git folder like it is often mentioned on other SO threads!

Thanks to Eric Nelson's blog post: https://ericnelson.wordpress.com/2014/06/21/is-visual-studio-2013-ignoring-your-gitignore-file/

like image 42
dustinmoris Avatar answered Sep 21 '22 09:09

dustinmoris