Is there any possible to automate this?
Yes: You would need to not version config.js
(keep it private), but to generate it (with the right value in it).
The generation script will determine the name of the checked out branch with:
branch=$(git rev-parse --symbolic --abbrev-ref HEAD)
Or (since Git 2.22, Q2 2019)
branch=$(git branch --show-current)
That means you could:
- version only a template file
config.js.tpl
- version value files named after the branches:
config.js.dev
, config.js.master
: since they are different, there is no merge issue when merging or switching branches.
Finally, you would register (in a .gitattributes
declaration) a content filter driver.
(image from "Customizing Git - Git Attributes", from "Pro Git book")
The smudge
script, associated to the template file (config.js.tpl
), would generate (automatically, on git checkout
) the actual config.js
file by looking values in the right config.js.<branch>
value file.
The generated actual config.js
file remains ignored (by the .gitignore
).
See a complete example at "git smudge/clean filter between branches".