1

In several projects, I need to have two branches/repos that are exactly the same, except that I want one commit to appear in one branch and not in the other (for exemple because the git repo I'm following don't set up the good environnment variables for my machine, or because one branch is specific to a given OS and the other one is for another system, but the difference between the two branches is just a matter of a few lines…).

In an ideal world, I would like to automatically apply the changes to the other branch when I modify it, or, for repos, when the upstream repo gets updated.

I saw some tricks involving git hooks, but the hooks are just local to a given computer, so if I clone the repo I'll need to manually re-create the hook files…

tobiasBora
  • 1,542
  • 14
  • 23
  • 3
    FWIW, the problem you're facing is exactly why people generally recommend against using a one-branch-per-configuration approach :/ – Oliver Charlesworth Dec 22 '17 at 18:50
  • You could for example maintain a single branch and make the platform-specific changes into a patch that you keep on top of the main branch (rebasing it when you update the main branch). Have you tried something like this? Did it work? What problems did you have? What exactly are you asking? – mkrieger1 Dec 22 '17 at 18:56
  • @OliverCharlesworth what would be a good alternative? – evolutionxbox Dec 22 '17 at 20:33
  • @evolutionxbox I would suggest a content filter driver, as I describe below: then you only need one branch. – VonC Dec 22 '17 at 21:16

1 Answers1

1

That is a good case for a content filter driver, using .gitattributes declaration.

That means you do not version the actual valued file, but only a template file, and a file with all possible values per environment.

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

The generated actual file remains ignored (by the .gitignore).
That means your actual working tree does not get "dirty".

The smudge script:

  • detect the right environment (not the branch, since only one is needed)
  • selects the correct value file and generates the correct file based on the template the smudge script is applied on during a git checkout.
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250