简体   繁体   中英

Can I git ignore an entire branch to create a local branch that interfaces with live server, with debug set to True.?

I'm about to start deploying, and I've had some issues problems with my live server crashing, and I can't have debug set to True.

I've got an idea of how I'd like things setup but I think I need some help.

I have a local branch, master , and a local database with Django Debug=True .
I push master to my server, where there's a live database, and a .env file with Debug set to False .

I've now setup a new local branch - debug - which has Debug=True , and using an SSH tunnel, I can connect to my live database.
All I want this for, is to run my code against the live database, with Debug set to True . So when something breaks on the server, as long as debug and master are meaningfully the same apart from the .env , I should be able to debug whatever is wrong without Debug=TRUE ever happening on the live, internet facing instance.

What I'd like is for my local debug branch to be an exact replica of master , in every way, but instead of connecting to my local database, it connects to the live one via SSH tunnel, with and Debug = True .

The problem

I have a .gitignore file called .env , and I'm running python decouple .
My .env files are referenced in that .gitignore file.
There's an .env for the live server, and one for the local master branch.

I now need a third one for debug branch - but it's not tracked by Git, so when I switch over branches, it still points at my single .env file.
If I track it, it ends up in the repository defeating the purpose of decoupling.

So far, I've hard coded the environment variables (database etc) into the settings.py file of my debug branch.

But how do I now move forward?
How can I have debug the same as master in every way other than settings.py ? My .gitignore is not branch specific?
And also, how do I automatically keep debug synced with master ?
Ideally, I'd have debug sync to master (apart from settings.py ), and then the entire branch just never commits to the repo?

Is there a better solution to all of this?

Is there a better solution to all of this?

Yes, and it involves keeping (for a given file, here env-dev.txt ) different content based on branches , which means:

  • versionning only a template file env.tpl
  • version value files named after the branches: env.dev , env.master : since they are different, there is no merge issue when merging or switching branches.

For that, you would register (in a .gitattributes declaration ) in your submodule repo a content filter driver .

弄脏 (image from " Customizing Git - Git Attributes ", from " Pro Git book ")

The smudge script, associate to the template file ( env.tpl ), would generate (automatically on git checkout ) the actual env file by looking values in the right env.<branch> value file. The generated actual env file remains ignored (by the .gitignore ), as you currently do.

See a complete example at " git smudge/clean filter between branches ".

Your smudge script can determine the name of the checked out branch with:

branch=$(git rev-parse --symbolic --abbrev-ref HEAD)

The same idea applies to settings.py if you need branch-specific content. `

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM