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

时间:2015-02-02 22:38:52

标签: git visual-studio visual-studio-2013 tfs

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

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

我觉得这是更好的方式。有没有人遇到过类似的使用并找到了解决方法?

1 个答案:

答案 0 :(得分:4)

我们找到了符合我们需求的解决方案。

首先,我们将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,它们定义了我们的基线配置选项。我们还为每个构建配置文件创建了一个文件(debug,qa,release)。所有这些文件都包含在源代码管理和项目文件中。

最后,我们使用web.AppSettings。[build profile] .config添加一个pre-build事件来转换web.AppSettings.base.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>