67

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

IntegerOverlord
  • 1,477
  • 1
  • 14
  • 33
  • Is the file in the correct location? What is "build files and other stuff" exactly? If you cd to the repository in a command prompt or shell with access to git, and do a `git status` does it also show the files? – stijn Jun 28 '18 at 08:20
  • We're going to need more details. Please update the question (too easy to loose details in comments). Have you customised that `.gitignore`? What are the (relative) paths of the files that are not being ignored? What does command line git show (https://gitforwindows.org/)? – Richard Jun 28 '18 at 08:21
  • 1
    Also note that the file needs to be named `.gitignore`, not `VisualStudio.gitignore` – petrpulc Jun 28 '18 at 08:39
  • @stijn Updated my question – IntegerOverlord Jun 28 '18 at 08:44
  • 2
    Possible duplicate of [git still shows files as modified after adding to .gitignore](https://stackoverflow.com/questions/9750606/git-still-shows-files-as-modified-after-adding-to-gitignore) – CodeCaster Jun 28 '18 at 08:56

8 Answers8

110

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.

Ceyhun
  • 1,354
  • 2
  • 10
  • 10
  • 3
    This should be the accepted answer, however you should add the explanation about why the files will not disappear (i.e. they are already tracked) and maybe add an explanation what the commands do. – Mathias Mamsch Nov 26 '20 at 16:14
  • 2
    `git rm -r --cached . ` will delete your current folder. Beware copy-pasting that. – jimh Aug 04 '22 at 04:53
57

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
Honza P.
  • 1,165
  • 1
  • 11
  • 12
  • 8
    This worked vs recreating the repo from scratch which is an insane suggestion above. This is the way to go. Once you remove the gitignore works properly when readded. – Chris Go Mar 06 '19 at 15:12
  • 1
    I did not have to restart VS. After updating my .gitignore, I just went to my solution and deleted the files, that i did not want GIT to track, and checked in... worked like a charm – Bjørn Jul 23 '19 at 19:55
  • 1
    This was perfect! Syncing the remote repo to make the local and remote consistent worked for me. – Cory May 26 '20 at 15:42
  • 3
    This should be the accepted answer - easy fix for the problem – Shoom Kloom Sep 03 '20 at 07:41
  • This approach is only suitable when the repo does not have significant history, as that history will be lost by creating a new repo. – tdog Jan 02 '21 at 16:41
  • 1
    This worked for me too. Delete all bin and obj folders, commit and push, restart vs and pull. now its working. – Mark Feb 22 '22 at 08:33
32

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.

stijn
  • 34,664
  • 13
  • 111
  • 163
  • 19
    Check out Honza P.'s answer instead of this one... This one is... overkill... to say the least – Bjørn Jul 23 '19 at 19:54
  • 2
    I appreciate the first sentence of this answer for helping me understand the problem or the behavior I was experiencing. I would not recommend this answer in actually solving the problem. – Travis Jul 08 '20 at 22:06
18

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

"

VinceL
  • 299
  • 1
  • 3
  • 13
Bryanttv
  • 201
  • 2
  • 4
13

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.

gpro
  • 579
  • 8
  • 17
1

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

Andrea Scarcella
  • 3,233
  • 2
  • 22
  • 26
0

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.

Insane but the following worked for me.

Created new solutions again.

Before adding anything I added .gitignore and .gitattribute file.

Then those files never appeared in git changes in team explorer. It was painful to redo but learn lessons.

How to add .gitignore from vs.

From menu Git> settings > git repository settings > next to ignore file click add . That will add you .gitignore same as for the attributes file. > click ok.

Hope this help.

Sagar Darekar
  • 982
  • 9
  • 14
-2

As of VS Code 1.56.0, this worked for me for having VS Code ignore files which were not yet tracked, but showing as Updated (green U beside file):

  • make edits to your .gitignore to ignore files that the Source Control sidebar is listing as Updated
  • right click on one of the files in Source Control sidebar you want to ignore (it should already be covered by your hand-coded changes in previous step) and choose Add to .gitignore from the context pop-up menu
  • this causes VS Code to re-read .gitignore and refresh the list of changed files in Source Control sidebar
  • all previously untracked files that were showing as "updated" that are covered in .gitignore should now be gone from Source Control sidebar
Baker
  • 24,730
  • 11
  • 100
  • 106