【问题标题】:How do I place a dummy file in a git repo?如何在 git repo 中放置一个虚拟文件?
【发布时间】:2013-07-03 05:34:52
【问题描述】:

我是 git 的新手,所以请多多包涵。

假设我有一个包含敏感数据的受版本控制的文件。果然,我把那个文件放在我的 .gitignore 文件中,所以它不会被推送到 repo。现在的问题是在我的项目中的某个地方,我有这样一行

#include <sensitivedata>

或任何您选择的语言。 问题是每当有人从该仓库克隆时,该文件就会丢失,并且在尝试构建/编译解决方案时会出现致命错误。

因此,我不想推送我实际正在处理的文件,而是想推送一些具有相同名称的虚拟文件,我在其中放置评论,例如

// replace the next line with the sensitive data!!!

我该怎么做?

【问题讨论】:

    标签: version-control git-commit git-add git


    【解决方案1】:

    我不知道 c++ 预处理器是否能够做到这一点(我假设上面显示的代码适用于某些 c 风格的预处理器),但这是我在类似情况下所做的:

    在 git 中提交:

    • default.config
    • user.config.template

    放入 gitignore:

    • user.config

    然后我的代码基本上可以做到:

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

    这样我可以提供一些合理的默认值,并有一个简单而干净的方法来覆盖它。

    【讨论】:

    • 感谢您的意见。我的想法与您的想法相同,但后来决定反对它,因为我完全想将任何源代码控制问题与实际代码分开。只是为了完整起见,如果其他人想走那条路,可以通过在 makefile 中使用类似的东西来完成:stackoverflow.com/a/142926/899260
    【解决方案2】:

    您可以使用 .gitatrributes 过滤内容:

    • .git 属性

      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
      

    我加入了“keepMine”合并以防止意外合并。但是,AFAIK 合并甚至不应该启动,因为由于 clean 过滤步骤,本地更改实际上是“不可见的”。不管secrets.h 中的实际内容是什么,repo 文件将始终包含:

    // replace the next line with the sensitive data
    

    例如:

    /tmp/work$ echo '// agent 007 reporting for duty' &gt; 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

    【讨论】:

    • 提醒:.git/config 文件是本地文件。您需要确保此 repo 的所有用户都具有过滤器定义,因为 .gitattributes 在 repo 中。您可能想阅读git-attributes man page 了解更多详情
    猜你喜欢
    • 2012-05-10
    • 2023-02-10
    • 2016-11-27
    • 2018-06-13
    • 2011-06-07
    • 2016-08-01
    • 2018-10-08
    • 2011-11-01
    • 2015-01-06
    相关资源
    最近更新 更多