简体   繁体   English

git忽略除某些子目录中的文件以外的所有内容

[英]git ignore everything except files in certain subdirectories

I just created a folder like this: 我刚刚创建了一个像这样的文件夹:

.
├── config
│   ├── subfolder
│   │   └── config.xml
│   └── subfolder2
│       └── config.xml
├── .gitignore
├── log2.txt
└── log.txt

And I've typed git init in this folder. 我在此文件夹中输入了git init I want to ignore everything except .gitignore , and all config.xml files. 我想忽略除.gitignore和所有config.xml文件之外的所有内容。

These are the .gitignore I have tried: 这些是我尝试过的.gitignore

1: 1:

*
!.gitignore
!**/config.xml

2: 2:

*
!.gitignore
!config/*/config.xml

3: 3:

*
!.gitignore
!**/config.xml

git status shows .gitignore is the only untracked file with any one of the gitignore settings above. git status显示.gitignore是唯一具有以上任何gitignore设置的未跟踪文件。

I want all the config.xml files to be on the untracked files list too. 我也希望所有config.xml文件也位于未跟踪的文件列表中。

I've read the tutorial at atlassian . 我已经阅读了atlassian的教程。 And I think that maybe it's the following rule that makes me not able to ignore everything except certain files under subdirectories. 而且我认为也许是以下规则使我无法忽略除子目录下的某些文件以外的所有内容。

I'm using git version 2.11.0.windows.1 in windows 10 operating system. 我在Windows 10操作系统中使用git version 2.11.0.windows.1 Anyone knows why my .gitignore file is not working the way I expect? 有人知道为什么我的.gitignore文件无法按我期望的方式工作吗?

在此处输入图片说明

Cloned your repo. 克隆了您的仓库。

With this .gitignore: 有了这个.gitignore:

*
!.gitignore
!*/*/config.xml

It's ignoring everything but .gitignore and config files: 它忽略了.gitignore和config文件之外的所有内容:

On branch master
Your branch is up-to-date with 'origin/master'.
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

    modified:   .gitignore
    modified:   config/subfolder/config.xml

no changes added to commit (use "git add" and/or "git commit -a")
.
├── config
│   ├── subfolder
│   │   ├── config.xml
│   │   └── ignoringthis
│   └── subfolder2
│       └── config.xml
├── log3.txt
└── newdir

Is that what you want? 那是你要的吗?

Thanks to torek's link , I finally come up with the following solution: 多亏了torek的链接 ,我终于提出了以下解决方案:

/**
!/**/
!.gitignore
!**/config.xml

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

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