270

Is it possible to set up files/folders to ignore on a per-project basis in TFS source control?

For example, I've a website with an assets folder that I do not want to go in to source control. These assets are maintained by a separate system. Also, I don't want to put several gigabytes of assets into source control, but I need a couple of samples on my dev machine, but I don't want to check those in either.

If I'm working on this website while bound to source control and I refresh the tree, these files will automatically get added again

I want to prevent this from happening.

sergiol
  • 4,122
  • 4
  • 47
  • 81
David
  • 19,389
  • 12
  • 63
  • 87
  • 1
    For local Workspaces you can use the **.tfignore**-Files like Richard Banks points out. For adjusting this file with **Context-Menue** in team-Explorerer, see my Answer. For Server-Workspaces I provided a link in the comments. – gReX Jul 20 '15 at 09:50

6 Answers6

183

If you're using local workspaces (TFS 2012+) you can now use the .tfignore file to exclude local folders and files from being checked in.

If you add that file to source control you can ensure others on your team share the same exclusion settings.

Full details on MSDN - http://msdn.microsoft.com/en-us/library/ms245454.aspx#tfignore

For the lazy:

You can configure which kinds of files are ignored by placing a text file called .tfignore in the folder where you want rules to apply. The effects of the .tfignore file are recursive. However, you can create .tfignore files in sub-folders to override the effects of a .tfignore file in a parent folder.

The following rules apply to a .tfignore file:

  • # begins a comment line
  • The * and ? wildcards are supported.
  • A filespec is recursive unless prefixed by the \ character.
  • ! negates a filespec (files that match the pattern are not ignored)

Example file:

# Ignore .cpp files in the ProjA sub-folder and all its subfolders
ProjA\*.cpp
# 
# Ignore .txt files in this folder 
\*.txt
#
# Ignore .xml files in this folder and all its sub-folders
*.xml
#
# Ignore all files in the Temp sub-folder
\Temp
#
# Do not ignore .dll files in this folder nor in any of its sub-folders
!*.dll
Richard Banks
  • 12,456
  • 3
  • 46
  • 62
  • 45
    i have tried doing that. the only thing getting ignored is the contents of the .tfignore file :( – user1852503 Dec 21 '14 at 07:03
  • 2
    I added this at the Solution level and it did not work...do I have to drill down to the actual location of the file? Based on your answer, it doesn't seem like I'd need to...and it's `.tfignore.txt` right? – Serj Sagan Feb 09 '15 at 18:16
  • 1
    @SerjSagan The file is just .tfignore, no extension. – Richard Banks Feb 10 '15 at 10:57
  • 19
    @SerjSagan my computer didn't allow me to create `.tfignore` so i created `.tfignore.` and it removed the last dot on its own.. – Peter Mar 21 '15 at 13:42
  • 7
    The problem is that it ignores only resources, which were 1) explicitly deleted in **Source Control Explorer** window before check-in and 2) explicitly excluded in the **Pending Changes** window. For example, if you are going to use it to ignore **packages** folder and rely on NuGet Package Restore, you will need to 1) delete **packages** folder in **Source Control** Explorer and then 2) exclude it in **Pending Changes** window after the NuGet Package Restore process. Otherwise it will still check-in the **packages** folder. – Deilan May 22 '15 at 13:32
  • 1
    If Windows won't let you create the `.tfignore` file due to its lack of a proper name, you can create it or rename it via a command line window. – Zarepheth Oct 21 '15 at 21:59
  • 1
    There is [some major problem](http://stackoverflow.com/questions/35471571/tfignore-does-not-work-for-me-in-visual-studio-2015) with .tfignore in VS2015. Folk are [very unhappy](https://github.com/aspnet/Tooling/issues/18), but there doesn't appear to be a clear statement of the issue, or a clear commitment to fix it. – bbsimonbb Apr 26 '16 at 15:54
  • How to recursively ignore all folder by its name and all files in it (for example named "A") ? Folders with name "A" may be at any deep and all of them must be ignored. P.S.: I found solution -http://stackoverflow.com/questions/33264703/how-to-get-tfs-to-ignore-all-folders-with-a-certain-name – 23W Jun 07 '16 at 09:00
  • @peter `echo hello>.tfignore` to create file from command prompt – asdf_enel_hak Oct 17 '16 at 13:43
  • how do you customize tfignore on a per user basis? – Alex Gordon Jan 17 '17 at 04:02
103

For VS2015 and VS2017

Works with TFS (on-prem) or VSO (Visual Studio Online - the Azure-hosted offering)

The NuGet documentation provides instructions on how to accomplish this and I just followed them successfully for Visual Studio 2015 & Visual Studio 2017 against VSTS (Azure-hosted TFS). Everything is fully updated as of Nov 2016 Aug 2018.

I recommend you follow NuGet's instructions but just to recap what I did:

  1. Make sure your packages folder is not committed to TFS. If it is, get it out of there.
  2. Everything else we create below goes into the same folder that your .sln file exists in unless otherwise specified (NuGet's instructions aren't completely clear on this).
  3. Create a .nuget folder. You can use Windows Explorer to name it .nuget. for it to successfully save as .nuget (it automatically removes the last period) but directly trying to name it .nuget may not work (you may get an error or it may change the name, depending on your version of Windows). Or name the directory nuget, and open the parent directory in command line prompt. type. ren nuget .nuget
  4. Inside of that folder, create a NuGet.config file and add the following contents and save it:

NuGet.config:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
    <solution>
        <add key="disableSourceControlIntegration" value="true" />
    </solution>
</configuration>
  1. Go back in your .sln's folder and create a new text file and name it .tfignore (if using Windows Explorer, use the same trick as above and name it .tfignore.)
  2. Put the following content into that file:

.tfignore:

# Ignore the NuGet packages folder in the root of the repository.
# If needed, prefix 'packages' with additional folder names if it's 
# not in the same folder as .tfignore.
packages

# include package target files which may be required for msbuild,
# again prefixing the folder name as needed.
!packages/*.targets
  1. Save all of this, commit it to TFS, then close & re-open Visual Studio and the Team Explorer should no longer identify the packages folder as a pending check-in.
  2. Copy/pasted via Windows Explorer the .tfignore file and .nuget folder to all of my various solutions and committed them and I no longer have the packages folder trying to sneak into my source control repo!

Further Customization

While not mine, I have found this .tfignore template by sirkirby to be handy. The example in my answer covers the Nuget packages folder but this template includes some other things as well as provides additional examples that can be useful if you wish to customize this further.

Community
  • 1
  • 1
Jaxidian
  • 13,081
  • 8
  • 83
  • 125
  • 1
    Followed this on VS 2017 - TFS 2015. Worked like a charm. All of my Nuget packages no longer showed up as pending changes. – RoninEngineer Nov 01 '17 at 18:54
  • I just had to open the detected changes and add the newly created files to the pending changes by hand. Except that everything worked fine. – Machado Mar 13 '18 at 14:17
  • This didn't work for me. VS 2017 - TFS 2015. I cannot check anything in. When I try I get the following error: " D:\Src\MiniCoreWebAPI\packages\Microsoft.IdentityModel.Protocols.5.2.1\lib\net451\Microsoft.IdentityModel.Protocols.dll: Could not find a part of the path" – Rod Jul 17 '18 at 20:17
  • 1
    I didn't have my packages folder checked in, but it was already detected as a pending change. After applying these instructions, I had to "Undo Pending Changes" on the packages folder, then everything was fine when I rebuilt and NuGet restored the packages. – CodeHxr Aug 30 '19 at 15:19
  • 1
    Looks like your answer has hardly anything to do with the question. The question was not about NuGet, but how to exclude a file from TFS. – Jens Mander Sep 03 '22 at 07:29
74

It does seem a little cumbersome to ignore files (and folders) in Team Foundation Server. I've found a couple ways to do this (using TFS / Team Explorer / Visual Studio 2008). These methods work with the web site ASP project type, too.

One way is to add a new or existing item to a project (e.g. right click on project, Add Existing Item or drag and drop from Windows explorer into the solution explorer), let TFS process the file(s) or folder, then undo pending changes on the item(s). TFS will unmark them as having a pending add change, and the files will sit quietly in the project and stay out of TFS.

Another way is with the Add Items to Folder command of Source Control Explorer. This launches a small wizard, and on one of the steps you can select items to exclude (although, I think you have to add at least one item to TFS with this method for the wizard to let you continue).

You can even add a forbidden patterns check-in policy (under Team -> Team Project Settings -> Source Control... -> Check-in Policy) to disallow other people on the team from mistakenly checking in certain assets.

Derek Morrison
  • 5,456
  • 4
  • 31
  • 24
  • 7
    I didn't have a forbidden patterns policy. Answer to a [related question](http://stackoverflow.com/questions/1369442/how-can-i-exclude-a-specific-files-from-tfs-source-control) reveals where to get it. It is apparently part of the [TFS Power Tools](http://msdn.microsoft.com/en-us/vstudio/bb980963.aspx). – Leif Carlsen Dec 13 '11 at 19:33
  • 1
    I am facing similar problem, can you provide more detail on a particular folder to be not checkin using Check-in Policy – Imran Rizvi Mar 17 '12 at 09:21
  • Whoops - you're right, Leif. It is part of the TFS Power Tools. – Derek Morrison Mar 23 '12 at 20:31
  • Imran, unfortunately I switched jobs, and no longer use TFS and don't have access to my past projects, so I'm not sure about the syntax for the forbidden patterns check-in policy syntax. Maybe this questions helps? http://stackoverflow.com/questions/2741412/forbidden-patterns-check-in-policy-in-tfs-2010 – Derek Morrison Mar 23 '12 at 20:34
  • TFS 2010 After you choose files to include, you can exclude from that list. Can't touch existing files... – JNF May 13 '13 at 06:37
  • @CoffeeAddict I totally agree. :) – Derek Morrison Sep 26 '13 at 20:04
49

For TFS 2013:

Start in VisualStudio-Team Explorer, in the PendingChanges Dialog undo the Changes whith the state [add], which should be ignored.

Visual Studio will detect the Add(s) again. Click On "Detected: x add(s)"-in Excluded Changes

In the opened "Promote Cadidate Changes"-Dialog You can easy exclude Files and Folders with the Contextmenu. Options are:

  • Ignore this item
  • Ignore by extension
  • Ignore by file name
  • Ignore by ffolder (yes ffolder, TFS 2013 Update 4/Visual Studio 2013 Premium Update 4)

Don't forget to Check In the changed .tfignore-File.

For VS 2015/2017:

The same procedure: In the "Excluded Changes Tab" in TeamExplorer\Pending Changes click on Detected: xxx add(s)

The Excluded Changes Tab in TeamExplorer\Pending Changes

The "Promote Candidate Changes" Dialog opens, and on the entries you can Right-Click for the Contextmenu. Typo is fixed now :-)

Community
  • 1
  • 1
gReX
  • 1,060
  • 1
  • 20
  • 35
  • Can't seem to find that option. I'm on VS 2015 RC. – Shimmy Weitzhandler Jul 14 '15 at 22:15
  • this will only work if you're working with a local workspace, as described in detail, here: https://msdn.microsoft.com/library/vstudio/ms245454(v=vs.110).aspx#tfignore – stvn Jul 15 '15 at 12:56
  • @Shimmy: The procedure in VS 2015 RC is the same, with still the same typo. See my updated Answer. But as stvn points out .tfignore only works with local workspaces. For server-workspaces you have to undo the Include in the Pending Checkins, see answer here: http://stackoverflow.com/questions/21887308/how-to-ignore-files-on-tfs-online-not-for-local-server – gReX Jul 16 '15 at 09:05
  • @Shimmy: Are you connected to TFS-Server 2015? Ensure, that you start with **Team Explorer** in VS – gReX Jul 18 '15 at 11:52
  • @qw1564 I'm quite sure, when the question was asked first, it wasn't possible the gui-way. But now, this is the answer I would choose. Thanks for the credits! – gReX Dec 19 '15 at 18:53
5

I found the perfect way to Ignore files in TFS like SVN does.
First of all, select the file that you want to ignore (e.g. the Web.config).
Now go to the menu tab and select:

File Source control > Advanced > Exclude web.config from source control

... and boom; your file is permanently excluded from source control.

Bharat
  • 5,869
  • 4
  • 38
  • 58
4

I'm going to assume you are using Web Site Projects. These automatically crawl their project directory and throw everything into source control. There's no way to stop them.

However, don't despair. Web Application Projects don't exhibit this strange and rather unexpected (imho: moronic) behavior. WAP is an addon on for VS2005 and comes direct with VS2008.

As an alternative to changing your projects to WAP, you might consider moving the Assets folder out of Source control and into a TFS Document Library. Only do this IF the project itself doesn't directly use the assets files.

NotMe
  • 87,343
  • 27
  • 171
  • 245
  • yes, your assumption is correct. I thought that would be the case, just wanted to be doubly sure I didn't miss anything. – David May 29 '09 at 02:49