如何防止git中的合并更改跟踪的配置文件? [英] How to prevent tracked config files from being changed by merges in git?

查看:126
本文介绍了如何防止git中的合并更改跟踪的配置文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Web项目,其中有两个git分支(开发和生产),每个分支都连接到不同的服务器(dev和prod).每个分支都有一组跟踪的配置文件,例如不同的URL,数据库选项,sql导出文件等,这两个分支上都不同.

I have a web project with two git branches (developing and production) and each one connects to a different server (dev and prod). Each branch has a set of tracked config files, such as different URLs, database options, sql exported files, etc. which are different on both branches.

每次我尝试合并这些分支时,这些设置文件中都有很多冲突,需要手动解决,但是有时我会错过一些东西.简而言之,这是一场噩梦.因此,我的问题是,如何避免这种情况?

Every time I try to merge those branches I get a lot of conflicts in these settings files which need to be resolved manually, but every now and then I miss something. In short, it is a nightmare. My question is therefore, how can I avoid this?

  • 有没有一种方法可以防止在合并或重新建立基础时对那些配置文件进行任何更改?*

建议的解决方案: 尝试使这些文件不被跟踪".我尝试了此操作,但是每当从生产文件中提取文件时,我都会删除并替换那些文件,所以我不知道是否应该以其他方式进行处理.

Suggested Solution: "Try to make those files untracked". I tried this but whenever I pulled from the production file I get those files deleted and replaced, so I don't know if I should do it in a different way.

推荐答案

做到这一点的方法是将合并策略注册为始终使用当前版本的按文件属性.这是操作方法(该想法来自

The way to do this is to register a merge strategy as per-file attribute that will always use the current version. Here is how to do this (the idea is from this blog)

首先,您注册一个合并驱动程序

First you register a merge driver that

  • 什么都不做,
  • 成功退出.

shell命令true正是我们想要的.以下行将true注册为 alwaysours 合并驱动程序:

The shell command true is exactly what we are looking for. The following line registers true as the alwaysours merge driver:

git config --local merge.alwaysours.driver true

接下来,我们需要告诉git使用该假合并驱动程序的文件.这就是.gitattributes文件的用途.它支持与.gitignore文件相同的小型文件模式集.例如

Next, we need to tell git for which files to use this bogus merge driver. This is what the .gitattributes file is for. It supports the same small set of file patterns as the .gitignore file. For example

/configs/* merge=alwaysours
*.conf merge=alwaysours

将对'/config'中的所有文件或所有以'.conf'结尾的文件使用伪造的合并驱动程序.将这个文件也添加到您的git仓库中是很有意义的

would use our bogus merge driver for all files in '/config' or all files ending with '.conf'. It makes sense to also add this file to your git repository

git add .gitattributes

,以便跟踪对它的更改.但是有一个警告:当您合并具有.gitattributes文件的分支时,在该分支上还将使用alwaysours驱动程序!为避免由此引起的任何问题,请添加alwaysours all 分支上.gitattributes的合并驱动程序.

so that changes to it are tracked. There is one caveat though: when you merge in a branch having the .gitattributes file, the alwaysours driver will also be used on this branch! To avoid any problems arising from that, add the alwaysours merge driver for .gitattributes on all branches.

echo '/.gitattributes merge=alwaysours' > .gitattributes
git commit .gitattributes -m 'Register alwaysours driver for .gitattributes'

当然,这会破坏对您在.gitattributes中拥有的任何其他设置的正确处理.

Of course this will break proper handling of any other settings you have in .gitattributes.

这篇关于如何防止git中的合并更改跟踪的配置文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆