简体   繁体   English

.gitignore 除

[英].gitignore everything except

I have a large directory full of files and subdirectories.我有一个充满文件和子目录的大目录。

Two or three of the files are excel spreadsheets in the main directory that I need to transfer to and from home regularly.其中两三个文件是主目录中的excel电子表格,我需要定期往返于家中。

My idea was to make a git repository that only includes those few files, then use git to pass the files back and forth via a private repository on github.我的想法是创建一个只包含这几个文件的 git 存储库,然后使用 git 通过 github 上的私有存储库来回传递文件​​。

I am unable to move/segregate these files to their own directory elsewhere, they need to stay where they are (amongst the hundreds of other files).我无法将这些文件移动/隔离到它们自己的其他地方的目录中,它们需要留在原处(在数百个其他文件中)。 So if I create a repository on that directory, it's going to include a whole lot of files that I don't want included.因此,如果我在该目录上创建一个存储库,它将包含大量我不想包含的文件。 It's like I need to create the repository with a whitelist that says, "exclude everything except file1.xls, file2.xls".这就像我需要创建一个带有白名单的存储库,上面写着“排除除 file1.xls、file2.xls 之外的所有内容”。

My git skills are weak.我的 git 技能很弱。 Can anyone advise the cleanest way to do this, or if there is a more idiomatic way to do this?任何人都可以建议最干净的方法来做到这一点,或者是否有更惯用的方法来做到这一点?

As @matt pointed out, Git only "sees files you explicitly ask it to see".正如@matt 指出的那样,Git 只“看到你明确要求它看到的文件”。 A simple solution here would be to not check in anything but your desired files.这里的一个简单解决方案是除了您想要的文件外不签入任何内容。

$ git add file1.xls file2.xls
$ git commit -m "Here are the Excel files"

If you want the ability to checkin everything with $ git add .如果您希望能够使用$ git add .签入所有内容$ git add . , or just avoid the chance of adding one of the other files by accident, one technique would be to ignore everything via * , and then include your specific files or matching file extensions. ,或者只是避免意外添加其他文件之一的机会,一种技术是通过*忽略所有内容,然后包含您的特定文件或匹配的文件扩展名。

Here's a sample .gitignore .这是一个示例.gitignore

# Ignore everything
*
# Include specific files excluded by a previous pattern 
!file1.xls

Replace file.xls with *.xls if you can get away with checking in all the Excel files.如果您可以检查所有 Excel 文件,请将file.xls替换为*.xls

From the docs:从文档:

An optional prefix " ! " which negates the pattern;一个可选的前缀“ ! ”,它否定了模式; any matching file excluded by a previous pattern will become included again.任何被先前模式排除的匹配文件将再次包含在内。 It is not possible to re-include a file if a parent directory of that file is excluded.如果排除了该文件的父目录,则无法重新包含该文件。 Git doesn't list excluded directories for performance reasons, so any patterns on contained files have no effect, no matter where they are defined.出于性能原因,Git 不会列出排除的目录,因此包含文件的任何模式都不起作用,无论它们在哪里定义。 Put a backslash (" \\ ") in front of the first " ! " for patterns that begin with a literal " ! ", for example, " \\!important!.txt ".对于以文字“ ! ”开头的模式,例如“ \\!important!.txt ”,在第一个“ ! ”前放置一个反斜杠(“ \\ ”)。

https://git-scm.com/docs/gitignore https://git-scm.com/docs/gitignore

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

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