如何维护 git 中的生产和开发分支?

how to maintain a production and development branches in git?

在我的项目中,我有这个 constants.ts 文件来存储我的 API_URL。我想在我的生产(主要)和开发分支中保留两个 API_URLs。

例如:开发 -> API_URL = 'http://localhost:4000' 主要 -> APIURL = 'https://backend.com'

但是当我更改 API_URL 并提交合并主分支和开发分支时,它检测为更改。所以如果我合并它,主分支中的常量文件也会发生变化。如何忽略这个文件的变化?

In my project, I have this constants.ts file which stores my API_URL.

我们的想法是存储两个文件:

  • constants.ts.main
  • constants.ts.dev

(我用的是main instead of master here

  • 从现在开始忽略 constants.tsecho constants.ts>>.gitignore

这意味着在本地生成正确的 constants.ts 文件:将 dev 合并到 main 时不涉及复杂的合并。

然后,在每个分支中,根据当前的执行环境,您将从这些文件之一中 生成 constants.ts,其中包含正确的内容。

生成脚本将确定检出分支的名称:

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

最后,您将注册(在 .gitattributes declaration) a content filter driver.

(图片来自“Customizing Git - Git Attributes", from "Pro Git book”)

constants.ts 文件关联的 smudge 脚本将生成(自动,在 git checkoutgit switch 上)实际的 constants.ts 文件右侧 constants.ts.<branch> 值文件中的值。
生成的实际 constants.ts 文件仍然被忽略(由 .gitignore)。

在“git smudge/clean filter between branches”查看完整示例。