How do I place a dummy file in a git repo?

强颜欢笑 提交于 2019-12-03 13:30:49

You could use .gitatrributes to filter the contents:

  • .gitattributes

    secrets.h filter=secret merge=keepMine
    
  • .git/config

    [filter "secret"]
    clean  = echo "// replace the next line with the sensitive data"
    smudge = cat
    
    [merge "keepMine"]
        name = always keep mine during merge
        driver = /bin/true %O %A %B
    

I threw in a 'keepMine' merge to prevent accidental merges. However, AFAIK merge should not even kick in, as local changes would be effectively 'invisible' due to the clean filter step. Regardless of what's actually in secrets.h, the repo file will always contain:

// replace the next line with the sensitive data

E.g.:

/tmp/work$ echo '// agent 007 reporting for duty' > secrets.h
/tmp/work$ git status -s
M secrets.h
/tmp/work$ git diff
/tmp/work$ git cat-file -p HEAD:secrets.h
// secret contents not in repo

i do not know if the c++ preprocessor is able to do this (i assume the code shown above is for some c-style preprocessor), but here is what i do in similar cases:

commit in git:

  • default.config
  • user.config.template

put in gitignore:

  • user.config

and then i have code that basically does:

if (file_exists(user.config)):
    use user.config
else:
    use default.config

that way i can supply some sensible default and have a simple and clean way to override it.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!