0

This is very similar to this question, but I'd be interested in knowing how to ignore a committed git file for some branches but not for others, or to find some other solution to my problem.

We have a config file that differs for each customer. We track this in git using a branch for each customer. Likewise, each developer has his own custom development environment which requires a unique config file.

I created a branch for my environment and tried using git update-index --assume-unchanged [path] (from this answer) but realized that although it ignores the changes in my branch, it never updates the file when I change branches. This works perfectly well for changing to another developer's branch, but if I were to change to a customer's branch, I need the config file to change since I want to point the code to their database, e.g.

Is there a way to ignore the config file for some branches but not for others?

If not, I've thought of a couple of alternative approaches, but each has its disadvantages:

  1. Before switching customer branches, run git update-index --no-assume-unchanged. This works, but I know I'm likely to forget this and it may not be immediately obvious that the wrong config file was used.
  2. Don't use git update-index at all and change the config file back to my settings after switching developer branches. I'm also likely to forget to do this occasionally.

Thanks for any other ideas.

Community
  • 1
  • 1
rimsky
  • 1,163
  • 3
  • 16
  • 27

2 Answers2

1

Maybe you could use a symbolic link? If, for example, the file was always found at ./etc/app.conf in the repository you could make it a symbolic link to an untracked file:

$ ln -s dev.conf etc/app.conf
$ echo 'dev.conf' >etc/.gitignore
$ git add etc/app.conf etc/.gitignore
$ git commit -m "point `app.conf' to dev config"
$ ls -l etc/
total 3
lrwxrwxrwx 1 user users  8 Sep 30 23:31 app.conf -> dev.conf
-rw-r--r-- 1 user users 13 Sep 30 23:30 dev.conf

The symbolic link is be tracked by git while dev.conf is ignored. When you switch to a customer branch the symbolic link can switch to an actual customer config:

$ rm app.conf
$ echo '# customer config' >etc/app.conf
$ git add etc/app.conf
$ git commit -m 'add config for customer A'
$ ls -l etc/
total 3
-rw-r--r-- 1 user users 18 Sep 30 23:35 app.conf
-rw-r--r-- 1 user users 13 Sep 30 23:30 dev.conf

The developer's custom config is left untouched but the app.conf entry no longer points to it.Alternatively, you could leave app.conf a symlink and re-point it to the tracked customer config:

$ echo '# customer config' >etc/customerA.conf
$ ln -sf customerA.conf app.conf
$ git add etc/app.conf
$ git commit -m "point `app.conf' to customer A config"
$ ls -l etc/
total 4
lrwxrwxrwx 1 user users 13 Sep 30 23:31 app.conf -> customerA.conf
-rw-r--r-- 1 user users 18 Sep 30 23:36 customerA.conf
-rw-r--r-- 1 user users 13 Sep 30 23:30 dev.conf

Either way, etc/app.conf is now always tracked by Git whether on a developer branch or a customer branch, and whether you are using a tracked (i.e., customer) config or not is handled via some indirection in the file system. This should avoid the ignored vs. tracked contention you are running into, and once it's setup it is basically automatic since the symbolic link is a part of the repository.

Chris
  • 451
  • 2
  • 5
  • Interesting idea, I didn't think of symlinks. However, we may have some issues with Windows environments. – rimsky Oct 03 '16 at 16:25
0

I suggest another solution that is more standard I believe:

  • Rename .conf file to .conf.default in all branches
  • Add .conf file to .gitignore in all branches (or do it in master and merge in all branches)
  • Change your installation script (run initially on production machine) that copy .conf.default into .conf
  • Define a post-checkout hook on each development machine, that copies .conf.default into .conf, or let the developer to maintain his .conf file (hooks are not pushed, they are defined per-machine)
saeedgnu
  • 4,110
  • 2
  • 31
  • 48
  • 1
    Great idea, thanks. We're probably headed in the direction of an installation script anyway. – rimsky Oct 03 '16 at 16:26