gpt4 book ai didi

git - merge 时忽略文件——但将其包含在推送中

转载 作者:太空狗 更新时间:2023-10-29 13:03:38 24 4
gpt4 key购买 nike

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

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

我测试过使用 merge=ours 将 settings.py 添加到 .gitattributes,但它不起作用,因为如果它没有任何冲突,Git 将正常 merge 。

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

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

更新:

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

所以,我 pip 安装了 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 *

而且效果很好。

最佳答案

我更喜欢版本控制:

  • 模板文件settings.py.tpl
  • 每个分支一个值文件:settings.py.branch1branch1 , settings.py.branch2branch2 , ...(意思是,从来没有 merge 问题:每个值文件都保持不变)
  • 一个能够检测当前分支的脚本,获取正确的值文件并从模板文件构建最终的 settings.py (这是您的工作树私有(private)的:它从不进行版本控制)

可以通过 content filter driver 自动调用该脚本这将在 checkout 时构建正确的配置文件。

smudge

A .gitattributes file可以注册'smudge ' settings.py.* 的脚本文件。
( checkin 时不需要“clean”脚本)

关于git - merge 时忽略文件——但将其包含在推送中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16992426/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com