简体   繁体   English

GIT:忽略除子子目录和某些文件以外的所有内容

[英]GIT: ignore everything except sub-subdirectory and some files

I'm banging my head against the wall with this, I hope you people can help me... I have this structure of files : 我用这个把我的头撞在墙上,希望大家能帮我...我有这种文件结构:

/public/images/foo
/public/images/foo/default.jpg
/public/images/foo/1/...
/public/images/foo/2/...
/public/images/bar
/public/images/bar/default.jpg
/public/images/bar/1/...
/public/images/bar/2/...
/public/images/baz
/public/images/baz/default.jpg
/public/images/baz/1/...
/public/images/baz/2/...
/public/images/other/...

I want to ignore everything inside images but keep the default.jpg files and the other folder and it's content. 我想忽略images所有内容,但保留default.jpg文件和other文件夹及其内容。 What I have in my .gitignore doesn't work as expected, the default.jpg files are still ignored : 我的.gitignore文件中的内容无法按预期运行,但default.jpg文件仍被忽略:

/public/images/*
!/public/images/*/default.jpg
!/public/images/other/

Thanks for your help ! 谢谢你的帮助 !

!public/images/**/default.jpg应该有2颗星而不是1颗星来代表当前目录。

Ok I found a solution with a friend : 好的,我和一个朋友找到了一个解决方案:

/public/images/*
!/public/images/*/
/public/images/*/*
!/public/images/other/*
!/public/images/*/default.jpg

Here I can forget everything inside images but keep some folders by naming them and also keep first level default.jpg files.The problem was that you need to be specific when working with subdirectories whitelisting. 在这里我可以忘记images所有内容,但是可以通过命名它们来保留一些文件夹,还可以保留第一层default.jpg文件。问题是,在处理子目录白名单时需要特别说明。

If your files are already in the repository you will have to delete them and only then you will be able to "ignore" them. 如果文件已经在存储库中,则必须将其删除,然后才可以“忽略”它们。

How to remove tracked files? 如何删除跟踪文件?

git rm --cached <file>

Now you can use the .gitignore file. 现在,您可以使用.gitignore文件。


If you wish to ignore specific file (in your case you want the folder itself so it suitable for your csse) you can use the assume-unchanged flag 如果您想忽略特定文件(在您的情况下,您想要文件夹本身以使其适合csse),则可以使用assume-unchanged标志
https://git-scm.com/docs/git-update-index https://git-scm.com/docs/git-update-index

--[no-]assume-unchanged - [无糖]假设 - 不变

When this flag is specified, the object names recorded for the paths are not updated. 指定此标志时,为路径记录的对象名称不会更新。
Instead, this option sets/unsets the "assume unchanged" bit for the paths. 而是,此选项设置/取消设置路径的“假定不变”位。

When the "assume unchanged" bit is on, the user promises not to change the file and allows Git to assume that the working tree file matches what is recorded in the index. 当“假定未更改”位打开时,用户承诺不更改文件,并允许Git假定工作树文件与索引中记录的文件匹配。 If you want to change the working tree file, you need to unset the bit to tell Git. 如果要更改工作树文件,则需要取消设置该位以告知Git。 This is sometimes helpful when working with a big project on a filesystem that has very slow lstat(2) system call (eg cifs). 当在lstat(2)系统调用非常慢的文件系统上处理大型项目(例如cifs)时,这有时会很有用。

Git will fail (gracefully) in case it needs to modify this file in the index eg when merging in a commit; 如果需要修改索引中的该文件(例如合并提交)时,Git将失败(正常)。 thus, in case the assumed-untracked file is changed upstream, you will need to handle the situation manually. 因此,如果假定未跟踪的文件在上游被更改,则您将需要手动处理这种情况。

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM