2013-07-05 81 views
7

我是git的新手,请耐心等待。如何在git仓库中放置一个虚拟文件?

说我有一个包含敏感数据的版本控制下的文件。果然,我把这个文件放在我的.gitignore文件中,所以它不会被推到回购站。现在的问题是在我的项目中的某处我有一条线如

#include <sensitivedata> 

或任何您选择的语言。 问题是每当有人从该回购克隆,该文件丢失,并试图建立/编译解决方案时,他得到一个致命的错误。

因此,不要推我实际工作对我想要把一些虚拟的文件具有相同的名称,而不是文件,在那里我将像

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

我怎么会做这样的评论?

回答

7

你可以使用.gitatrributes过滤的内容:

  • .gitattributes

    secrets.h filter=secret merge=keepMine 
    
  • 的.git /配置

    [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实际上是,回购文件将始终包含:

// replace the next line with the sensitive data 

例如为:

/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

+0

太棒了!谢谢! – lightxx

+2

平视:'.git/config'文件是本地的。您需要确保此回购的所有用户都有过滤器定义,因为'.gitattributes'在回购中。您可能需要阅读[git-attributes手册页](https://www.kernel.org/pub/software/scm/git/docs/gitattributes.html)以获取更多详细信息 – sehe

0

我不知道C++预处理器是能够做到这一点(我假设如上所示的代码是一些C风格的预处理),但这里是我做什么类似的情况:

犯的git:

  • default.config
  • user.config。模板

放于gitignore:

  • user.config

,然后我有代码,基本上没有:

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

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

+1

感谢您的输入。我在想和你一样的东西,但是随后决定反对,因为我完全想把任何源代码控制问题与实际代码分开。只是为了完整起见,如果有人想要走这条路线,可以通过在makefile中使用类似的东西来完成:http://stackoverflow.com/a/142926/899260 – lightxx