Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.gitignore for visual studio project is not working

I am using standard Visual Studio .gitignore file, but Git for Windows still includes build files and other stuff. How to fix this?

Git ignore file I am using: https://github.com/github/gitignore/blob/master/VisualStudio.gitignore Visual Studio generates the same thing automatically

It is named correctly, and it is in the right place

enter image description here

When I enter git status command it shows build files

enter image description here

like image 558
IntegerOverlord Avatar asked Jun 28 '18 08:06

IntegerOverlord


People also ask

Why is my .gitignore file not working?

Check the file you're ignoring Take a good look at your structure, and make sure you're trying to ignore the file that isn't already committed to your repository. If it is, remove the file from the repository and try again. This should fix the Gitignore not working issue.

How do I use Gitignore in Visual Studio?

Open Visual Studio and the solution needing an ignore file. From the top menu select Git > Settings. The above will open Visual Studio's Options with Source Control > Git Global Settings selected. From the list on the left select Git Repository Settings and then click the Add button for Ignore file.

Why is Gitignore not ignoring my files?

gitignore ignores only untracked files. Your files are marked as modified - meaning they were committed in the past, and git now tracks them. To ignore them, you first need to delete them, git rm them, commit and then ignore them.


6 Answers

To stop tracking the files in the gitignore file;
Open a command prompt and navigate to the directory that contains your solution file (.sln), then run the following commands (first two commands contains dot at last):

  1. Remove all of the items from the Git index (not from the working directory or local repository):
    git rm -r --cached . 
  1. Update the Git index according to gitignore:
    git add .
  1. Commit changes:
    git commit -am "Remove ignored files"

Then open your Visual Studio and sync your repo. That seemed to do the trick for me. I found the git commands here.

like image 195
Ceyhun Avatar answered Oct 19 '22 03:10

Ceyhun


For us worked this approach:

  1. Check .gitignore file - seemed ok (bin/Debug folder content ignored)
  2. Check GIT repo for these files (bin/Debug folder content) - they were there => inconsistence with ignore file
  3. Locally delete these files and commit and push (narrow the GIT files with .gitignore definitions)
  4. (Optional) Restart Visual studio and perform Pull
  5. Repository and VS seems now to be in consistent state
like image 33
Honza P. Avatar answered Oct 19 '22 04:10

Honza P.


The files are modified, meaning they were already in the git repository before you added the .gitignore file (or you added them explicitly) so you cannot 'ignore' changes to them anymore now.

If possible, just start over again and create the repository from scratch but now make a first commit which just adds the .gitignore file (which is always a good idea btw), then a second commit adding all source files; the build files will then be ignored.

Alternatively you could rewrite the history using interactive rebasing, by modifying the commit in which you add those files, and adding the .gitignore in an earlier commit would also not be bad. Or you could use branch filtering to get rid of all traces of those files. And there's probably other options.

like image 26
stijn Avatar answered Oct 19 '22 04:10

stijn


It is possible to stop tracking any changes for any checked in file. You need to tell git to remove the file from its index first by calling the following in the given repository > git rm --cached <file>. Then update your .gitignore file to exclude the file. Finally commit the removal of the file.

Source from Microsoft Git guide.

like image 13
gpro Avatar answered Oct 19 '22 05:10

gpro


Finally Solved

This little trick helped me. It removes all files that have been cached as being 'tracked', then re-adds files based on the .gitignore file

First:

remove (git rm) all files recursively (-r) that are cached as being tracked

git rm -r --cached .

Then:

Add all (git add .) files that have changed/aren't accounted for in the cache back; this will re-add the files as they were, so there won't be a need to 're-save' the files, they'll just disappear from git changes that would've removed the file (and all other files due to the first command)- result is that "changes" or git status should just have the files that are now going to be ignored by .gitignore if they were not before the .gitignore change.

git add .

Resource:

https://sigalambigha.home.blog/2020/03/11/how-to-refresh-gitignore/

This was a long struggle for me, finally I have my dll's being quiet when I just want to merge my changes back into main!

my .gitignore for a dotnet core project:

"

\# file types<br>
*.cache<br>
*.dtbcache.v2<br>
*.dll<br>
*.pdb<br>
*.suo<br>
.suo<br>
*.testlog<br>
*.user<br>

\# directories<br>
**/Release

"

like image 11
Bryanttv Avatar answered Oct 19 '22 04:10

Bryanttv


I had this problem with .cache files and the following steps solved it for me:

  1. add .gitignore
  2. stage and commit it
  3. manually delete *.cache (find in windows explorer)
  4. refresh changes in VS - Team Explorer - Changes
  5. undo pending changes (underlying assumption, you have committed ALL changes you intend to preserve!)
  6. *.cache files are not listed anymore among changes

HTH

like image 1
Andrea Scarcella Avatar answered Oct 19 '22 03:10

Andrea Scarcella