Ignore file when merging - but include it in click

I have a django project and I am using git.

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

I tested adding settings.py to .gitattributes with merge = ours, but this did not work, because if it does not have a Git conflict, it will merge normally.

Also, add settings.py to .gitignore, this is not an option, because if I change something in settings.py, I want it to move to one branch.

Is there a way to ignore the file when merging, but still click it?

UPDATE:

I tried the VonC solution and I created two settings: settings_production.py and settings_development.py.

So, I installed gitpython and used it in my .py settings, 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 great.

+7
source share
2 answers

I prefer to have a version:

  • settings.py.tpl template file
  • the value file for each branch: settings.py.branch1 in branch1 , settings.py.branch2 in branch2 , ... (which means no problem with merging ever: each value file remains untouched)
  • a script that can detect the current branch, take the file of the correct value and build the final settings.py from the template file (which is private to your working tree: it is never versioned)

That the script can be automatically called using the content filter filter , which during the verification process will create the correct configuration file.

smudge

A .gitattributes file can register a ' smudge ' script for settings.py.* Files.
(no need for a ' clean ' script when registering)

+1
source

This is either a VonC solution, or you make the settings.py file where you check which branch it is in and import the special code of the branch from settings-branchX.py . This solution is pretty close to VonC, but you do it in python, and don't mess it up with git.

You can get inspiration from this gist .

0
source

All Articles