18

I'm trying to ignore the bin folder of a web project, but include the .refresh files that are in that bin folder.

Here's what I have in my .gitignore:

[Bb]in/
#Allow .refresh files for web sites
![Bb]in/*.refresh

But this isn't working. What am I doing wrong? I've also tried:

!*.refresh

With the same results.

Update:

Here's the file structure if that helps diagnose this:

\rootfolder
    .gitignore
    \project1
         \bin
              file1.dll
              file1.dll.refresh

You can see where our gitignore file is located. We don't want the .dll but we do want the .refresh file.

Nicholas Head
  • 3,716
  • 5
  • 27
  • 39
  • 2
    possible duplicate of [How do negated patterns work in .gitignore?](http://stackoverflow.com/questions/2820255/how-do-negated-patterns-work-in-gitignore) – Cascabel Nov 18 '11 at 00:07
  • I don't agree this is a dupe-- I already searched for someone asking a similar question but couldn't find one. The one you reference is talking about including a FOLDER. I want to include a wildcard. – Nicholas Head Nov 18 '11 at 00:34
  • 2
    It is a dupe. The two answers here do exactly the same thing as in the other question, with the subdirectory changed to a wildcarded path in the directory. Of course, they're also incomplete: you also need to ignore `[Bb]in/.*`, since `*` doesn't match hidden file. (And the fact that you didn't find it doesn't mean it's not a dupe.) – Cascabel Nov 18 '11 at 00:41
  • @Jefromi is right. But wonder how you manage to find these dupes. – manojlds Nov 18 '11 at 00:43
  • Accepted an answer. Still disagree this is a dupe, since it's an issue with where the .gitignore file is, like @manojlds pointed out. And I don't see anyone else asking about this common scenario (under .NET development). – Nicholas Head Nov 18 '11 at 00:49
  • @manjlds: Well, that one I cheated: I remembered I'd answered at least one of these (quite possibly more - as you say, they're hard to find) so I just cheated and searched `is:answer user:me gitignore`. – Cascabel Nov 18 '11 at 00:49
  • @Nicholas: Okay, with your additional information it's not a dupe, I suppose. (Though probably between that question, and all the variations on questions about gitignores and subdirectries, the pieces are probably there.) Moral of the story: be careful about "simplifying" away details! – Cascabel Nov 18 '11 at 00:53

6 Answers6

15

This way not worked for me:

[Bb]in/*
![Bb]in/*.refresh

I have used the syntax bellow on .gitignore file and worked fine.

Try it:

**/[Bb]in/*
!**/[Bb]in/*.refresh
Leandro Sá
  • 529
  • 5
  • 4
  • If having trouble with manojlds's solution, try this. The last bit was what I needed to finally get this working. – NightOwl888 Nov 28 '15 at 17:55
  • 3
    This should be the correct answer. It's only solution that works for asp.net refresh file requirements. – Erick T Apr 20 '16 at 17:25
  • How about when there is a subfolder in the Bin directory? Like "roslyn" when using the Microsoft.CodeDom.Providers.DotNetCompilerPlatform.2.0.0 package? Is there a way to exclude all the .refresh files from any subdirectories in Bin? While Bin itself may be a nested subdirectory? – Mihai Caracostea Oct 12 '18 at 11:16
  • This one works nice with VisualStudio 2017, thanks @Leandro Sá – gReX Feb 13 '19 at 09:30
9

Try doing below:

[Bb]in/*
![Bb]in/*.refresh

this is because when you just do bin or bin/ it will not descend into the folder at all to look for file. You will have to explicitly say, go to folder, but ignore everything and then say dont ignore *.refresh.

Edit for OP's structure:

Then you will have to either

1) Include project1 in the path in your .gitignore:

project1/bin/*
project1/bin/*.refresh

or

2) Add a .gitignore inside project1 ( I recommend this) with the previous content.

manojlds
  • 290,304
  • 63
  • 469
  • 417
  • Doesn't work, unfortunately. And now it's trying to add all the .dll/etc files in bin/ -- see my edit, maybe this is a sub-directory issue? – Nicholas Head Nov 18 '11 at 00:32
  • I took your change, updated the .gitignore file and did a "git add ." -- and it proceeded to add all of my bin folders/.DLLs to pending changes. I'm a git newbie, I'm afraid, but it seems like something isn't working correctly. Maybe a bug in Windows-git ? – Nicholas Head Nov 18 '11 at 00:36
  • @Nicholas - Updated answer for your structure – manojlds Nov 18 '11 at 00:39
  • This solution doesn't work, unfortunately. What does work is commenting out the `[Bb]in/` line, adding only the .refresh files to the repo, and then re-commenting it. Not a solution, but a workaround. Fortunately, project references don't usually change that often. – NightOwl888 Aug 07 '14 at 16:42
5

I know it's an old post, but I was still having this problem, and using MsysGit 1.8.0, I was not able to use the accepted answer by @manojlds.

It seems the only patterns that exclude all bin folders at any depth are [Bb]in and [Bb]in/. According to the gitignore man page, the one with the trailing slash is most correct, since it will only match directories, and not files named "bin". As soon as I try to add additional levels to the pattern, such as [Bb]in/*, this ceases to match at any depth and becomes relative to the location of the .gitignore file, as per the man page.

Now, assuming that I have a [Bb]in/ entry in my global .gitignore file, the best way I found to unignore a specific pattern of files (like .refresh files) in a specific bin folder is to create another .gitignore file one directory above the bin folder (i.e., the project's directory) and in it place the following entries:

![Bb]in/
[Bb]in/*
![Bb]in/*.refresh

Our web site project bin folders never contain sub folders like Debug or Release, but I tested and this still ignores subfolders in the bin folder, as desired.

If I only use the second and third entries, as seemed suggested by the accepted answer, it doesn't work. An intuitive explanation is that the first entry removes this specific bin folder from the global glob, so its contents will be included, then the second entry works as desired since it is relative to the .gitignore file in this case, and the third entry removes the desired patterns from the glob created by the second entry.

BlueD
  • 171
  • 1
  • 4
3

Another solution could be to force include .refresh files from the commandline.

git add *.dll.refresh --force

--force
Allow adding otherwise ignored files.

Than you can just exclude everything in the bin folder in your .gitignore file. If you add a new package you have to force include the new .refresh file again.

AAD
  • 380
  • 3
  • 9
3

With Roslyn ("Enable latest C# and VB features" for a website project), there will now be a subdirectory inside 'bin' which will have .refresh files you'll need. This gets to be a headache because of git's default behavior that once you exclude a folder, any exceptions within it will never be found (/directory/* vs /directory/**/.)

So we needed bin to be as follows:

**/[Bb]in/**/*.*

otherwise the entire roslyn directory would be ignored, and then

!**/*.refresh

Mark Sowul
  • 10,244
  • 1
  • 45
  • 51
  • Did the build actually work for you? `Microsoft.CodeDom.Providers.DotNetCompilerPlatform.dll` is restored, but the files in the `roslyn` directory aren't restored so the build fails. – user247702 Jun 19 '18 at 10:58
  • I think it broke again at some point, because the .refresh files point up one directory instead of two. At that point I think we just included all the roslyn files into Git. – Mark Sowul Jul 02 '18 at 15:20
3

The following works for me:

[Bb]in/*
![Bb]in/*.refresh

Note that the first line gained a trailing asterisk to match all contents of the bin directory, not the directory itself. This way you can add an exception from the glob for *.refresh.

Dominik Honnef
  • 17,937
  • 7
  • 41
  • 43