为每个分支保留一个不同的配置文件(未跟踪) [英] Keep a different configuration file (untracked) for each branch

查看:386
本文介绍了为每个分支保留一个不同的配置文件(未跟踪)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在一个git仓库中,我有两个文件: config / conf.yaml.sample (通过git进行跟踪,但在启动程序时忽略)和a它的副本名为 config / conf.yaml (它被git忽略,但是当我的程序启动时读取它)。



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



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

我该怎么做? /git-scm.com/docs/githooks.htmlrel =nofollow> 结账后挂钩是正确的在你的胡同里:


git checkout 运行后调用该钩子更新了工作树。这个钩子有三个参数:前面的 HEAD 的引用,新的 HEAD 的引用(可能或者可能没有改变),以及一个表示结帐是否是分支结帐(改变分支,标志= 1)或文件结帐(从索引中检索文件,标志= 0)的标志。这个钩子不会影响 git checkout 的结果。



它也在git clone之后运行,除非<$使用c $ c> - no-checkout ( -n )选项。给钩子的第一个参数是null-ref,新的 HEAD 的引用的第二个参数,并且该标志始终为1。



该钩子可用于执行存储库有效性检查,自动显示与以前 HEAD 不同的区别,或设置工作目录元数据属性。 p>

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

 #!/ bin / sh 

#一个示例post-checkout钩子脚本,用于在$ b $上有条件地执行一个操作。如果有的话)刚刚签出。

#测试一个分支是否刚刚签出
if [$ 3-eq 1];然后
#保存HEAD的值(如果HEAD被分离,不要发出错误)
symrefHEAD =`git symbolic-ref --quiet HEAD`
if [$ symrefHEAD= refs / heads / master];然后
#为主人做一些有用的事情,例如
#cp config / conf_master.yaml config / conf.yaml
printf--- test:你刚刚签出了master。--- \\\

elif [$ symrefHEAD =refs / heads / develop];然后
#为开发做一些有用的事情,例如
#cp config / conf_develop.yaml config / conf.yaml
printf--- test:你刚刚签出了开发。--- \\\

else
#默认情况下
printf你刚刚签出了一些其他的分支。@ b $ b fi
else
printf没有分支被检出\
fi


In a git repository I have two files: config/conf.yaml.sample (which is tracked by git, but it's ignored when my program is launched) and a copy of it called config/conf.yaml (which is ignored by git, but it's read when my program is launched).

When I switch from branch A to branch B I always have the same configuration file (because config/conf.yaml is untracked) and that means, for example, that each branch relates to the same database, same ports, and so on.

I want to keep a different config/conf.yaml for each branch, so that it changes when switching branches, but I don't want to have git track it (e.g. because it contains the name and password to access the database).

How can I do it?

解决方案

It seems that Git's post-checkout hook is right up your alley:

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.

The following script (which must be made executable) should get you started; modify it to suit your needs and save it as .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

这篇关于为每个分支保留一个不同的配置文件(未跟踪)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆