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

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

时间  2019-07-07

标签 git gitignore 栏目 Git

 

在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

相关文章

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值