且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用git进行分支特定的配置文件维护

更新时间:2022-12-05 22:22:42

这是可能的,并且不涉及符号链接。

您不是版本 my.config ,而是模板文件 my.config.tpl ,和一个值文件(包含每个分支的值)

  prod.conf 
dev.conf

然后,您可以使用
(图片来源于定制Git - Git Attributes ,来自 Pro Git book



脚本生成 my。通过将占位符值替换为< branch.conf> 文件的值,可以在每次结帐分支时使用config b $ b您可以通过以下方式了解当前分支名称:

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

生成的实际 my.config 仍然被忽略(由 .gitignore )。

这意味着您的实际工作树不会变脏。

污迹脚本选择正确的值文件,生成正确的 web.config 根据模板在 git checkout 过程中应用了涂抹脚本。



查看分支之间的git smudge / clean filter 的完整示例。


Here is a question keeps me puzzled for a long time as I cannot find nice and more-or-less universal solution to it.

Lets say there are two git branches available, production and dev; each uses it's own configurable parameters for some tasks (i.e. credentials, build path, test/deployment scripts switches et cetera). Same time implementation scripts & code is common for both branches.

Now, the problem arises is - how to maintain branch-specific configuration within git repository the way required. One of common solutions out there is to use 'template' configuration file within git, symlinking and ignoring the concrete for specific branch, like

$ cat .gitignore | grep conf
/concrete.conf
$ ls -l *.conf
lrwxrwxrwx 1 1000 100 12 Oct 29 10:23 concrete.conf -> generic.conf
-rw-r--r-- 1 1000 100  0 Oct 29 10:16 generic.conf

breaking and adjusting concrete.conf on development box next. Yet not a solution I'm in pursuit for.

My requirements (ok, wishes) are:

  • support separate configuration file per git branch, and
  • keep branch-specific configuration file managed by git same time

Is it even possible? Could be (actually, preferred to be file sourced by some other one, but it's none related...)

The best that has came to my mind so far is to use post-merge hooks to adjust per-branch configuration per se from some other source ignored by git, but it stinks from the very beginning.

Any suggestions on solution of problem described ?

PS: *nix-specific suggestions (i.e. using symlinks/hardlinks) suggestions is absolutely fine, I do do have any interest in M$ targets

It is possible, and does not involve symlinking.

You do not version my.config, but a template file my.config.tpl, and a value file (with values for each branches)

 prod.conf
 dev.conf

Then, you can use a content filter driver, using .gitattributes declaration.

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

The script generate my.config file by replacing placeholder values with the values of the <branch.conf> file, each time you checkout a branch.
You can know about the current branch name with:

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

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

The smudge script selects the correct value file and generates the correct web.config based on the template the smudge script is applied on during a git checkout.

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