使用 Visual Studio 从 TFS-GIT 中排除 web.config

Exclude web.config from TFS-GIT with Visual Studio

我们正在从 TFS 迁移到 TFS-GIT。以前,我们设置了三个不同的分支(dev、qa、prod),每个分支都有自己的 web.config。当我们对 web.config 进行更改时,我们会手动将它们向上移动并强制它们不合并。现在我们正在迁移到 GIT,我们已经开始使用 web.base.config,然后使用配置文件和转换为我们想要的环境配置它。

总的来说,这很好用,但是每次我们尝试提交时都会看到 web.config 已被修改,我们要么需要手动撤消它,要么需要处理潜在的合并冲突。我试过从 GIT 中删除它,将它添加到 git 忽略,并从源代码管理中删除它,但随后 visual studio 抱怨未启用调试,重新添加 web.config 到项目并将其重新添加到 GIT.

我觉得有更好的方法。有没有人 运行 遇到过类似的问题并找到了解决方法?

我们找到了满足我们需求的变通方法。

首先,我们将 appSettings 和 connectionStrings 配置文件从 web.config 中移出并放入名为 web.appSettings.config 和 web.connections.config 的文件中。这些文件都没有包含在项目文件中,也没有添加到源代码管理中。

<appSettings file="web.AppSettings.Config" />
<connectionStrings configSource="web.Connections.Config" />

接下来,我们创建了 web.AppSettings.base.config 和 web.Connections.base.config 来定义我们的基线配置选项。我们还为每个构建配置文件(调试、质量检查、发布)创建了一个文件。所有这些文件都包含在源代码管理和项目文件中。

最后,我们添加一个预构建事件来转换 web.AppSettings.base.config 使用 web.AppSettings.[build profile].config 来创建 web.AppSettings.config。我们对 web.Connections.config.

做同样的事情
<Target Name="BeforeBuild">
    <TransformXml Source="Web.AppSettings.Base.config" Transform="Web.AppSettings.$(Configuration).config" Destination="Web.AppSettings.config" />
    <TransformXml Source="Web.Connections.Base.config" Transform="Web.Connections.$(Configuration).config" Destination="Web.Connections.config" />
</Target>

这实现了我们所有的目标。我们能够快速更改我们指向的环境,并且能够避免需要不断地提交和合并不断变化的配置文件。此外,为了使其易于管理,我们将各种配置文件标记为相互依赖,使它们显示为 hierarchy in solution explorer,只需在项目文件中进行简单修复。

<Content Include="Web.config" />
<None Include="web.AppSettings.base.config">
  <DependentUpon>web.config</DependentUpon>
</None>
<None Include="web.AppSettings.dev.config">
  <DependentUpon>web.AppSettings.base.config</DependentUpon>
</None>
<None Include="web.AppSettings.qa.config">
  <DependentUpon>web.AppSettings.base.config</DependentUpon>
</None>
<None Include="web.AppSettings.release.config">
  <DependentUpon>web.AppSettings.base.config</DependentUpon>
</None>
<None Include="web.Connections.base.config">
  <DependentUpon>web.config</DependentUpon>
</None>
<None Include="web.Connections.dev.config">
  <DependentUpon>web.Connections.base.config</DependentUpon>
</None>
<None Include="web.Connections.qa.config">
  <DependentUpon>web.Connections.base.config</DependentUpon>
</None>
<None Include="web.Connections.release.config">
  <DependentUpon>web.Connections.base.config</DependentUpon>
</None>