7

I have a django project and I'm using git.

I need to have different settings.py file, for each branch.

I've tested add settings.py to .gitattributes with merge=ours, but it not worked because if it's not having any conflict Git will merge normally.

Also, add settings.py to .gitignore is not an option, cause if I change something in the settings.py, I want it pushed to the same branch.

Is there a way to ignore a file when merging but still push it?

UPDATE:

I've tried VonC's solution and I've created two settings: settings_production.py and settings_development.py.

So, I pip installed gitpython and used it in my settings.py, like this:

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 *

And it worked fine.

Fernando Freitas Alves
  • 3,709
  • 3
  • 26
  • 45
  • possible duplicate of [Branching: different config files for release/development](http://stackoverflow.com/questions/9636492/branching-different-config-files-for-release-development) – John Zwinck Jun 08 '13 at 05:16

2 Answers2

1

I prefer having versioned:

  • a template file settings.py.tpl
  • a value file per branch: settings.py.branch1 in branch1, settings.py.branch2 in branch2, ... (meaning, no merge issue ever: each value file remains untouched)
  • a script able to detect the current branch, take the right value file and build from the template file the final settings.py (which is private to your working tree: it is never versioned)

That script can be automatically called through a content filter driver which will, on checkout, build the right config file.

smudge

A .gitattributes file can register that 'smudge' script for the settings.py.* files.
(no need for a 'clean' script on checkin)

Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
0

It's either VonC's solution, or you make your settings.py file where you check which branch it is in and import branch specific code from a settings-branchX.py. This solution's pretty close to VonC's, but you're doing it in python, and not messing up with git.

You may get inspiration from that gist.

zmo
  • 24,463
  • 4
  • 54
  • 90