0

I use git for everything including managing configurations across machines. One set of configurations is almost the same across every machines, but with a few lines different. I use branches to distinguish machines.

Sometimes, when I do modification on one machine, I will merge the change to master change, such that other machines can have the same change. However, like I said, there are some machine specific configurations, which I don't want to merge.

In the current state, this kind of machine specific configurations is in a manageable scale, like 3, 4 lines. So I am wondering is there any git dark magic to tell git that please do not merge any change in those few lines?


Let me make some clarification here. The word "configurations" is very vague here because it's actually vague. I literally use git for lots of configurations: editors, .bashrc, systemd, apache, and many other you might think of. All those things are for personal purpose, and I use git mainly to avoid spending too much time on these things, and my focus was on reproducibility so I imagine things can work just by cloning and pulling.

Considering the varieties, it would be nice if git can work just like that. Is there a chance to implement git hooks to achieve such goal? I have not implemented any git hook before so I am not aware of the complexity of that.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
Jason Hu
  • 6,239
  • 1
  • 20
  • 41
  • 1
    Why not use environment variables instead? Then it's reproducible, as well. – jhpratt Feb 07 '18 at 20:51
  • You cant. You can drop the whole file from versionning, you can (as part of your build process) apply xslt (Xls-Transformations) on configurations that are xml, etc. I would propbably try to inject the needed changes in my build steps and do not use branches for distinguishing different build targets. You could checkin `file.m1.config`, `file.m2.config`, `file.m3.config` (one per machine) and use the correct one when building for m1, m2, m3, etc... – Patrick Artner Feb 07 '18 at 20:51
  • You can add parts of files to staged changes if you use `git add --patch` – Kevin Hoerr Feb 07 '18 at 20:52
  • You can stage or unstage "chunks". https://stackoverflow.com/questions/1085162/commit-only-part-of-a-file-in-git ...I do this all the time using a GUI. I imagine it's quite a PITA to do via command line though – Joe Phillips Feb 07 '18 at 20:52
  • Manually patching / staging files for different configs - in the age of CI/CD? – Patrick Artner Feb 07 '18 at 20:56
  • hey guys, I updated the question. honestly avoiding doing it manually is the exact reason why i am posting a question here. if none of you can think of a way then i guess it's not quite feasible from git's side. – Jason Hu Feb 07 '18 at 21:17
  • @HuStmpHrrr No need to be rude. Is there any particular reason you can't use environment variables, though? You never answered that. – jhpratt Feb 07 '18 at 21:26
  • @jhpratt it's unclear to me what do you mean by using env vars. if you are saying loading some variables in .bashrc or some other scripts, then what it means is some configuration of that particular tool will now spread its dependencies on the environment at the time it instantiates. I think it just complicates the issue because it builds into some implicit dependencies which requires me to remember. – Jason Hu Feb 07 '18 at 21:30
  • You could also use a `.env` file that's read in at runtime, with similar implications. It would avoid having to set it in `.bashrc`, and can also have a default version checked into version control. – jhpratt Feb 07 '18 at 21:32
  • @jhpratt so I perceive this similar to what Patrick said, using multiple configuration files. it works better for those tools with either arbitrary execution or special modularity support, and this treatment differs in every tool, and requires certain level of refactoring as well. definitely it can work, but it would be the best if some work can be done on git's side so that it works once and for all, and i don't have to bring changes to configurations. – Jason Hu Feb 07 '18 at 21:42
  • I'm not sure I understand exactly what your problem is, then. I've no clue why git would support what you're describing, as anything specific to the environment should be kept as such. – jhpratt Feb 07 '18 at 21:43
  • @jhpratt you are right if it was for work and it's important to do the right thing from the beginning. however, if it's just one person who tries to save time on replicating his linux configurations across machines, then there is a good value to avoid spending extra time on making things modular. i think it's a clear conclusion to me that there is pretty much no way to do it from git. – Jason Hu Feb 07 '18 at 21:49
  • Does [this](https://stackoverflow.com/a/20087076/1290731) help? – jthill Feb 07 '18 at 23:08

1 Answers1

0

One possibility to avoid any merge conflict is to keep the template separate from the values for a given config file.

For that, put in place a content filter driver, which on checkout can assess the hostname and generate the config file the right values.

https://git-scm.com/book/en/v2/images/smudge.png

(From the Git Pro Book): "Customizing Git - Git Attributes: Keyword Expansion")

The smudge script would be able to comine a template file with a value file (named afte rthe current hostname), generating the actual config file (which would then remain private, untracked.

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