Keywords: TFS | .tfignore | version control
Abstract: This article provides an in-depth exploration of how to exclude files and directories in Team Foundation Server (TFS) using the .tfignore file, preventing unnecessary resources from entering the central source repository. Focusing on Visual Studio 2015/2017 and TFS 2012+, it details the configuration methods, syntax rules, and practical applications of .tfignore, including integration with NuGet package management and Visual Studio tools. Through step-by-step instructions and code examples, it assists developers in efficiently managing source code, optimizing storage, and enhancing team collaboration.
Introduction
In software development, version control systems like Team Foundation Server (TFS) are essential for managing source code. However, not all project files should be included in version control, such as auto-generated resources, third-party dependencies, or large media files. Ignoring these files helps prevent repository bloat and improves team collaboration efficiency. This article, based on TFS 2012+ and Visual Studio 2015/2017 environments, delves into using the .tfignore file to achieve file and directory exclusion.
Fundamentals of the .tfignore File
The .tfignore file is used in TFS to define ignore rules, supported since TFS 2012 for local workspaces. This text-based file can be created in the project root or subdirectories, applying exclusion rules recursively. Its key advantage is shareability: by committing the .tfignore file to version control, team members automatically inherit the same ignore settings, ensuring environment consistency.
Ignore rules are based on file path patterns, supporting wildcards and recursive control. For example, in a web project, to exclude the assets folder and its contents, add assets or assets\* to .tfignore. This prevents TFS from automatically adding these files during refresh, addressing common accidental commit issues in development.
Detailed .tfignore Syntax
The syntax of the .tfignore file is concise yet powerful, adhering to the following rules:
- Comments start with
#, used to explain rule purposes. - Supports
*(matches any character sequence) and?(matches a single character) wildcards. - File patterns are recursively applied to all subdirectories by default, unless prefixed with
\to restrict to the current directory. - Use
!to negate patterns, excluding specific files or types from being ignored.
Example code illustrates common scenarios:
# Ignore all .cpp files in the ProjA subfolder and its subfolders
ProjA\*.cpp
#
# Ignore .txt files in the current folder
\*.txt
#
# Ignore .xml files in the current folder and all subfolders
*.xml
#
# Ignore all files in the Temp subfolder
\Temp
#
# Do not ignore .dll files (including subfolders)
!*.dllIn practice, developers can customize rules based on project needs. For instance, for an assets folder with numerous sample files, adding assets globally excludes it, while using !assets\sample.txt retains specific samples.
Integrating NuGet Package Management
In .NET projects, NuGet packages often lead to accidental commits of the packages folder. Combining .tfignore with NuGet configuration effectively manages dependencies. Steps include:
- Ensure the
packagesfolder is not committed to TFS. If committed, remove it first. - Create a
.nugetfolder in the solution directory (where.slnis located). In Windows, use the commandren nuget .nugetor name it.nuget.(automatically removing the trailing period). - Inside the
.nugetfolder, create aNuGet.configfile with the following content:
This configuration disables source control integration for NuGet packages.<?xml version="1.0" encoding="utf-8"?> <configuration> <solution> <add key="disableSourceControlIntegration" value="true" /> </solution> </configuration> - Create a
.tfignorefile in the solution folder, adding rules to ignore thepackagesfolder:# Ignore the NuGet packages folder in the repository root packages # Include package target files (required for MSBuild) !packages/*.targets - Commit the
.tfignoreand.nugetfolder to TFS. After restarting Visual Studio, Team Explorer will no longer mark thepackagesfolder as pending check-in.
This method works for both on-premises TFS and Visual Studio Online (VSTS), ensuring dependency management is separated from version control.
Advanced Operations in Visual Studio
Beyond manual editing of .tfignore, Visual Studio offers graphical tools to simplify the ignore process. In the Team Explorer's "Pending Changes" window, click the "Detected: x add(s)" link to open the "Promote Candidate Changes" dialog. Right-click on files or folders to select ignore options:
- Ignore this item: Excludes a specific file or folder.
- Ignore by extension: Excludes based on file type.
- Ignore by file name: Targets specific name patterns.
- Ignore by folder: Excludes entire directory trees.
These operations automatically update the .tfignore file, reducing manual errors. For example, ignoring the assets folder adds corresponding rules and prompts to commit .tfignore changes for sharing settings.
Supplementary Ignore Methods
For older TFS versions or specific scenarios, alternative approaches can be considered:
- Undo pending changes: In a project bound to source control, after adding files, immediately undo the "add" operation in Team Explorer; files remain local without being committed.
- Check-in policies: Configure forbidden pattern check-in policies via team project settings to prevent accidental commits of specific file types, though this is more of a team-level constraint than local exclusion.
However, .tfignore has become the preferred method for ignoring files in TFS due to its flexibility and standardization.
Best Practices and Template Resources
To enhance efficiency, it is recommended to use community templates for customizing .tfignore. For example, the sirkirby/tfignore template on GitHub provides common ignore patterns, covering build outputs, log files, and IDE configurations. Developers can adapt this base to fit project needs.
Key practices include:
- Commit
.tfignoreto version control to ensure team consistency. - Regularly review ignore rules to avoid excluding necessary files.
- Combine with automation tools (e.g., build scripts) to verify ignore effectiveness.
Conclusion
Through the .tfignore file, TFS developers can efficiently manage version control exclusions, optimizing storage and collaboration workflows. This article provides comprehensive guidance from basic syntax to advanced integration. In real-world projects, applying these techniques can significantly reduce accidental commits and improve the development experience. As TFS and Visual Studio continue to evolve, ignore features will become more refined; it is advisable to refer to official documentation for the latest updates.