合并时忽略文件 - 但将其包含在推送中

Fer*_*ves 7 git

我有一个django项目,我正在使用git.

我需要为每个分支设置不同的settings.py文件.

我已经测试了使用merge = ours将settings.py添加到.gitattributes,但它没有用,因为如果它没有任何冲突,Git将正常合并.

另外,添加settings.py到.gitignore不是一个选项,因为如果我在settings.py中更改了某些内容,我希望它被推送到同一个分支.

有没有办法在合并时忽略文件但仍然推送它?

更新:

我尝试过VonC的解决方案,并创建了两个设置:settings_production.py和settings_development.py.

所以,我点了安装gitpython并在我的settings.py中使用它,如下所示:

from git import Repo
import os

r = Repo(os.path.realpath(os.path.dirname(__file__)))
if r.active_branch.__str__( == 'master':
    from settings_production.py import *
else:
    from settings_development.py import *
Run Code Online (Sandbox Code Playgroud)

它工作得很好.

Von*_*onC 1

我更喜欢版本控制:

  • 模板文件settings.py.tpl
  • 每个分支一个值文件:settings.py.branch1in branch1settings.py.branch2in branch2、 ... (这意味着,永远不会出现合并问题:每个值文件保持不变)
  • 一个能够检测当前分支的脚本,获取正确的值文件并从模板文件构建最终分支settings.py(这是您的工作树私有的:它永远不会版本化)

该脚本可以通过内容过滤器驱动程序自动调用,该驱动程序将在签出时构建正确的配置文件。

弄脏

文件可以为.gitattributes文件注册“ ”脚本。(签入时 不需要“ ”脚本)smudgesettings.py.*
clean