Git:忽略跟踪的文件


179

我在存储库中有一些跟踪文件,这些文件在构建代码时会自动修改。我不想取消跟踪它们,我只是不想让它们显示为已修改状态,并且我不希望它们在git add上演。

这可能吗?



5
如果这些文件是由构建自动生成的,则不应在git中跟踪它们。
编码

这是一个关于gitignore的快速教程,您可能会发现它很有用:learningpassion.wordpress.com/2016/06/17/…–
小丑

Answers:


281

当然。

git update-index --assume-unchanged [<file> ...]

要撤消并再次开始跟踪:

git update-index --no-assume-unchanged [<file> ...]

3
如果我对文件进行修改,在这种状态下会发生什么情况?
haymansfield,2012年

2
@haymansfield命令的帮助页面显示以下内容Git will fail (gracefully) in case it needs to modify this file in the index e.g. when merging in a commit; thus, in case the assumed-untracked file is changed upstream, you will need to handle the situation manually.
Parham

3
有什么方法可以--assume-unchanged在克隆时申请?这样,所有用户都可以拥有文件,但看不到差异中的本地更改。
Gauthier

9
@Tyler-不,一旦git跟踪了文件,即使该文件出现在.gitignore中,也将始终对其进行跟踪。我的用例类似于“这是文件的基本模板,您可以在其中存储凭据,而现在不再提交它”。
乔恩五世

5
@watbywbarif如果我将命令git update-index --no-assume-unchanged $(git ls-files $(git rev-parse --show-toplevel))设置为git别名。每当我怀疑是其中一些恶作剧时,我都会这样做。
菲利普·卡芬

5

在filter命令中使用git属性和%f的另一种解决方案:

git config filter.orig.clean "cat %f.orig"
cp filename filename.orig
echo "filename filter=orig" >> .git/info/attributes
echo "filename.orig" >> .git/info/exclude

16
您能否详细说明这意味着什么以及它在做什么?
rogerdpack

这会为从原始文本的“ .orig”文件中读取内容的文件添加过滤器
koct9i

5

另一种方法(从Seth Robertson现在删除的答案中删除,但我发现它很有帮助,因此可以恢复它)是维护一个“已跟踪”模板文件,然后使用其本地未跟踪版本,例如:“ config.sample.ini”或“ config.ini.template” 中的完整示例,请参见https://gist.github.com/canton7/1423106

然后,如果在git等文件中进行了更改,就不会有任何问题,您可以(最终)对本地未跟踪的文件使用.gitignore。

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.