2

I have 2 different branches, master and test. Those branches are exact replicas except for the database string which is different for master and test. Everytime I merge test to master, the database string in the master gets replaced by the test database string.

I have heard about using branch specific variables, so I don't need to worry about the database string getting changes on merge as .env variables will take care of it.

Please let me know how do I do that.

SwissCodeMen
  • 4,222
  • 8
  • 24
  • 34
Farhaan Patel
  • 177
  • 3
  • 9

1 Answers1

1

as .env variables will take care of it.

You could generate the right .env file.
And rename the current .env into .env.master and .env.test

In each branch, you would then generate .env, with the right content in it, from one of those files, depending on the current execution environment.

The generation script will determine the name of the checked out branch with:

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

Finally, you would register (in a .gitattributes declaration) a content filter driver.

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

The smudge script, associated the .env.branch file, would generate (automatically, on git checkout or git switch) the actual .env file by looking values in the right .env.branch value file.
The generated actual .env file remains ignored (by the .gitignore).

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

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250