63

I'm writing a short script with a few simple variables at the top of the page. I want to work on them with a friend, but we aren't sure how to manage the variables needing to be changed after pulling each time for one of us, adding unnecessary junk to git status. I thought about just creating different named branches for each of us, and then the master will just have example usernames set, but it seems silly to have to do all that extra work merging. We could have the variables passed to the script as options, but that isn't desired, nor is separating it out to another separate configuration file. It would be great to have something like a .gitignore but for only ignore a few lines in a file.

How can this be elegantly managed? How is this problem usually managed?

2

6 Answers 6

71

You can't easily just ignore changes to particular lines of a file, I'm afraid, so you're probably stuck with having a separate configuration file. Below I've listed two typical ways of dealing with this, and one slightly more exotic one:

Have a sample configuration file in git

Here, you would keep a file config.sample in git as an example, but the application would actually use the values in a file config which is in .gitignore. The application would then produce an error unless config is present. You have to remember to change values in the sample file when you add new configuration variables to your personal config file. In this case it's also a good idea to have your application check that all the required configuration variables are actually set, in case someone has forgotten to update their config file after changes to the sample.

Have a file of default values in git

You keep a file config.defaults in git, which has sensible default configuration values as far as possible. Your application first sources configuration from config.defaults and then from config (which is in .gitignore) to possibly override any of the default values. With this method, typically you wouldn't make it an error for config not to exist, so the application can work out of the box for people who haven't bothered to create config.

Using a single configuration file with --assume-unchanged

A third possibility, which I wouldn't recommend in this case, personally, would be to have a single configuration file which is committed in git, but to use git update-index --assume-unchanged <FILE>, to tell git to ignore changes to it. (This is described further in this useful blog post.) That means that your local changes to the configuration file won't be committed with git commit -a or show up in git status.

3
  • 1
    For the sample configuration file solution, people will always forget to update the sample file after updating their local config file, because they can run successfully with their local config file.
    – Cloud
    May 25, 2018 at 9:41
  • On production and dev branches, you can do #2 and #3 together so that depending on the branch you can have different overrides. This should help with continuous integration environments or build scripts. Aug 26, 2018 at 5:58
  • 1
    "to tell git to ignore changes to it"—that's not what assume-unchanged does: 'Assume-unchanged should not be abused for an ignore mechanism. It is "I know my filesystem operations are slow. I'll promise Git that I won't change these paths…" Especially, it is not a promise… that Git will always consider these paths are unmodified—if Git can determine a path… has changed without incurring extra lstat(2) cost, it [can] report that the path has been modified (…git commit -a is free to commit that change).'
    – Chris
    May 14, 2019 at 13:37
8

Python/Django-specific solution is to have a shared settings.py files that is checked into the repository, and a local settings_local.py imported at the end of settings.py, that overrides some of the settings with machine-specific values.

5

In my case, I have "config" variables in a separate (small) file as do all the other developers on the team. Things like my database location etc. are kept there. We put the name of this file in our .gitignore so that it's not version controlled but checkin a "sample_config" file so that newcomers can make a copy and use it for their own purposes.

4
  • 1
    Hope still relevant this answer...I have a question... How do you update both files? Every time a developer adds a key to the "small" file, adds it to the sample. Now how the other developers know that a new key was added to the sample and need to update their small files? Could you clarify this for me please?
    – elvainch
    Jan 28, 2018 at 15:10
  • If it's something that everyone needs, there will be a corresponding dummy value in the sample file. The rest is proper team communication. Jan 28, 2018 at 15:17
  • And in a big team how would you manage it? Also proper team communication?
    – elvainch
    Jan 28, 2018 at 15:23
  • 1
    I can't really offer concrete suggestions. That would depend on the size and nature of the team. One reliable way is to make sure that tests catch this upfront so that people's setup will crash when they do an update and run tests. Jan 28, 2018 at 16:43
2

Other options (not elegant but may be helpful):

  • Use git stash and git stash pop for your config file
  • Have a branch named, say, config which has your local config file changes and then use git checkout config <your config file>

Second option is good if you need to keep the local config changes in the repo (somewhere).

1
  • Git stash is VERY handy for very early development, but I would move to Mark's suggestion ASAP, specifically #2 and/or #3. Aug 26, 2018 at 5:56
0

I have a couple of short scripts like this and instead of creating a separate configuration file, I create a separate setenv.sh (or setenv.bat) file. I move the few, simple variables to this new file, and call the setenv.sh file in the main script. Variables that will not change per user remains in the main script. Depending on how small this setenv.sh script is, I will either write documentation on how to create this setenv.sh, or will commit a setenv.sh.sample to be used as a template.

A variation to this is not to create or call a setenv.sh, and let the user set environment variables used in the main script. The main script will complain if the variables don't exist.

Some short scripts grow into big scripts or become full-fledged applications. When this happens, I go the way of configuration files. We have an application that manages configuration files called Config, at http://www.configapp.com. Config has the concept of environments and instances. In your example, you have 1 Local environment and 2 instances. Common variables go into Local environment and machine specific variables (you and your friend) go into the instances. This is a little too much for small scripts but works well for applications.

0

Nowadays I use ENV vars for example in python/django, you can also add defaults to them. In the context of docker I can save the ENV vars in a docker-compose.yml file or an extra file which is ignored in version control.

# settings.py
import os
DEBUG = os.getenv('DJANGO_DEBUG') == 'True'
EMAIL_HOST = os.environ.get('DJANGO_EMAIL_HOST', 'localhost')
1
  • If these env vars are not directly for Django, they should be prefixed with the name of your application, e.g. MYAPP_DJANGO_DEBUG and MYAPP_DJANGO_EMAIL_HOST. If they are directly for Django, then of course they're fine without the prefix.
    – Asclepius
    Nov 1, 2020 at 13:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.