gpt4 book ai didi

git - 为每个分支保留不同的配置文件(未跟踪)

转载 作者:太空狗 更新时间:2023-10-29 13:40:54 25 4
gpt4 key购买 nike

在一个 git 存储库中,我有两个文件:config/conf.yaml.sample(由 git 跟踪,但在我的程序启动时它会被忽略)和一个名为 config/conf.yaml(git 会忽略它,但会在我的程序启动时读取它)。

当我从分支 A 切换到分支 B 时,我总是有相同的配置文件(因为 config/conf.yaml 未被跟踪),这意味着,例如,每个分支都与同一个数据库相关、相同的端口等。

我想为每个分支保留不同的 config/conf.yaml,以便在切换分支时它会发生变化,但我不想让 git 跟踪它(例如,因为它包含访问数据库的名称和密码)。

我该怎么做?

最佳答案

好像是 Git 的 post-checkout hook就在你的巷子里:

This hook is invoked when a git checkout is run after having updated the worktree. The hook is given three parameters: the ref of the previous HEAD, the ref of the new HEAD (which may or may not have changed), and a flag indicating whether the checkout was a branch checkout (changing branches, flag=1) or a file checkout (retrieving a file from the index, flag=0). This hook cannot affect the outcome of git checkout.

It is also run after git clone, unless the --no-checkout (-n) option is used. The first parameter given to the hook is the null-ref, the second the ref of the new HEAD and the flag is always 1.

This hook can be used to perform repository validity checks, auto-display differences from the previous HEAD if different, or set working dir metadata properties.

以下脚本(必须可执行)应该可以帮助您开始;修改它以满足您的需要并将其保存为 .git/hooks/post-checkout:

#!/bin/sh
#
# An example post-checkout hook script to perform an action conditionally on
# the branch (if any) just checked out.
#
# Test whether a branch was just checked out
if [ "$3" -eq 1 ]; then
# Save the value of HEAD (do not issue an error if HEAD is detached)
symrefHEAD=`git symbolic-ref --quiet HEAD`
if [ "$symrefHEAD" = "refs/heads/master" ]; then
# Do something useful for master, e.g.
# cp config/conf_master.yaml config/conf.yaml
printf " --- test: You just checked out master. ---\n"
elif [ "$symrefHEAD" = "refs/heads/develop" ] ; then
# Do something useful for develop, e.g.
# cp config/conf_develop.yaml config/conf.yaml
printf "--- test: You just checked out develop. ---\n"
else
# default case
printf "You just checked out some other branch.\n"
fi
else
printf "No branch was checked out\n"
fi

关于git - 为每个分支保留不同的配置文件(未跟踪),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25571031/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com