简体   繁体   English

Make.gitignore 忽略除少数文件外的所有内容

[英]Make .gitignore ignore everything except a few files

I understand that a .gitignore file cloaks specified files from Git's version control.我知道.gitignore文件隐藏了 Git 版本控制中的指定文件。

How do I tell .gitignore to ignore everything except the files I'm tracking with Git?我如何告诉.gitignore忽略除我正在使用 Git 跟踪的文件之外的所有内容? Something like:就像是:

# Ignore everything:
*

# Do not ignore these files:
script.pl
template.latex

An optional prefix !一个可选的前缀! which negates the pattern;这否定了模式; any matching file excluded by a previous pattern will become included again.任何被先前模式排除的匹配文件都将再次包含在内。 If a negated pattern matches, this will override lower precedence patterns sources.如果否定模式匹配,这将覆盖较低优先级的模式源。

# Ignore everything
*

# But not these files...
!.gitignore
!script.pl
!template.latex
# etc...

# ...even if they are in subdirectories
!*/

# if the files to be tracked are in subdirectories
!*/a/b/file1.txt
!*/a/b/c/*

If you want to ignore the whole content of a directory except one file inside it, you could write a pair of rules for each directory in the file path.如果你想忽略一个目录的全部内容,除了其中的一个文件,你可以为文件路径中的每个目录编写一对规则。 Eg .gitignore to ignore the pippo folder except from pippo/pluto/paperino.xml例如.gitignore忽略pippo文件夹,除了pippo/pluto/paperino.xml

pippo/*
!pippo/pluto
pippo/pluto/*
!pippo/pluto/paperino.xml

Note that if you simply had written above:请注意,如果您只是在上面写了:

pippo/*
!pippo/pluto/paperino.xml

It wouldn't work because the intermediary pluto folder would not exist to Git, so paperino.xml could not find a place in which to exist.它不起作用,因为中间的pluto文件夹对 Git 不存在,所以paperino.xml找不到存在的地方。

You want to use /* instead of * or */ in most cases在大多数情况下,您想使用/*而不是**/

Using * is valid, but it works recursively.使用*是有效的,但它以递归方式工作。 It won't look into directories from then on out.从那时起,它不会查看目录。 People recommend using !*/ to includelist directories again, but it's actually better to blocklist the highest level folder with /*人们建议使用!*/再次包含目录,但实际上最好使用/*将最高级别的文件夹列入黑名单

# Blocklist files/folders in same directory as the .gitignore file
/*

# Includelist some files
!.gitignore
!README.md

# Ignore all files named .DS_Store or ending with .log
**/.DS_Store
**.log

# Includelist folder/a/b1/ and folder/a/b2/
# trailing "/" is optional for folders, may match file though.
# "/" is NOT optional when followed by a *
!folder/
folder/*
!folder/a/
folder/a/*
!folder/a/b1/
!folder/a/b2/
!folder/a/file.txt

# Adding to the above, this also works...
!/folder/a/deeply
/folder/a/deeply/*
!/folder/a/deeply/nested
/folder/a/deeply/nested/*
!/folder/a/deeply/nested/subfolder

The above code would ignore all files except for .gitignore , README.md , folder/a/file.txt , folder/a/b1/ and folder/a/b2/ and everything contained in those last two folders.上面的代码将忽略除.gitignoreREADME.mdfolder/a/file.txtfolder/a/b1/folder/a/b2/以及最后两个文件夹中包含的所有文件之外的所有文件。 (And .DS_Store and *.log files would be ignored in those folders.) (并且.DS_Store*.log文件将在这些文件夹中被忽略。)

Obviously I could do eg !/folder or !/.gitignore too.显然,我也可以使用!/folder!/.gitignore

More info: http://git-scm.com/docs/gitignore更多信息:http: //git-scm.com/docs/gitignore

A little more specific:更具体一点:

Example: Ignore everything in webroot/cache - but keep webroot/cache/.htaccess .示例:忽略webroot/cache中的所有内容 - 但保留webroot/cache/.htaccess

Notice the slash (/) after the cache folder:注意cache文件夹后的斜线 (/):

FAILS失败

webroot/cache*
!webroot/cache/.htaccess

WORKS作品

webroot/cache/*
!webroot/cache/.htaccess
# ignore these
*
# except foo
!foo

Let's toolify!让我们工具化!

As @Joakim said, to ignore a file, you can use something like below.正如@Joakim 所说,要忽略文件,您可以使用如下所示的内容。

# Ignore everything
*

# But not these files...
!.gitignore
!someFile.txt

but if the file is in nested directories, it's a little difficult to write those rules.但如果文件位于嵌套目录中,则编写这些规则有点困难。

For example, if we want to skip all files but not a.txt , located in aDir/anotherDir/someOtherDir/aDir/bDir/cDir .例如,如果我们想跳过所有文件,但不跳过位于aDir/anotherDir/someOtherDir/aDir/bDir/cDir中的a.txt文件。 Then, our .gitignore will be something like this然后,我们的.gitignore将是这样的

# Skip all files
*

# But not `aDir/anotherDir/someOtherDir/aDir/bDir/cDir/a.txt`
!aDir/
aDir/*
!aDir/anotherDir/
aDir/anotherDir/*
!aDir/anotherDir/someOtherDir/
aDir/anotherDir/someOtherDir/*
!aDir/anotherDir/someOtherDir/aDir/
aDir/anotherDir/someOtherDir/aDir/*
!aDir/anotherDir/someOtherDir/aDir/bDir/
aDir/anotherDir/someOtherDir/aDir/bDir/*
!aDir/anotherDir/someOtherDir/aDir/bDir/cDir/
aDir/anotherDir/someOtherDir/aDir/bDir/cDir/*
!aDir/anotherDir/someOtherDir/aDir/bDir/cDir/a.txt

This is quite hard to write by hand.这很难手写。

To solve this hurdle, I've created a web app named git-do-not-ignore which will generate the rules for you.为了解决这个障碍,我创建了一个名为git-do-not-ignore的 Web 应用程序,它将为您生成规则。

Tool工具

Demo演示

Sample Input样本输入

aDir/anotherDir/someOtherDir/aDir/bDir/cDir/a.txt

Sample Output样本输出

!aDir/
aDir/*
!aDir/anotherDir/
aDir/anotherDir/*
!aDir/anotherDir/someOtherDir/
aDir/anotherDir/someOtherDir/*
!aDir/anotherDir/someOtherDir/aDir/
aDir/anotherDir/someOtherDir/aDir/*
!aDir/anotherDir/someOtherDir/aDir/bDir/
aDir/anotherDir/someOtherDir/aDir/bDir/*
!aDir/anotherDir/someOtherDir/aDir/bDir/cDir/
aDir/anotherDir/someOtherDir/aDir/bDir/cDir/*
!aDir/anotherDir/someOtherDir/aDir/bDir/cDir/a.txt

To ignore some files in a directory, you have to do this in the correct order :要忽略目录中的某些文件,您必须按正确的顺序执行此操作:

For example, ignore everything in folder "application" except index.php and folder "config" pay attention to the order .例如,忽略文件夹“application”中除 index.php 和文件夹“config”之外的所有内容,注意顺序

You must negate want you want first.你必须首先否定你想要的。

FAILS失败

application/*

!application/config/*

!application/index.php

WORKS作品

!application/config/*

!application/index.php

application/*

Had the similar issue as OP but none of top 10 upvoted answer actually worked .有与 OP 类似的问题,但前 10 名赞成的答案都没有真正起作用
I finally found out the following我终于发现了以下内容

Wrong syntax :错误的语法:

*
!bin/script.sh

Correct syntax :正确的语法:

*
!bin
!bin/script.sh

Explanation from gitignore man page :来自gitignore 手册页的解释:

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 不会列出排除的目录,因此包含文件的任何模式都无效,无论它们是在哪里定义的。

Which means that "Wrong syntax" above is wrong because bin/script.sh cannot be reincluded as bin/ is ignored.这意味着上面的“错误语法”是错误的,因为bin/script.sh不能重新包含为bin/被忽略。 That's all.就这样。

Extended example :扩展示例:

$ tree . $树。

.
├── .gitignore
└── bin
    ├── ignore.txt
    └── sub
        └── folder
            └── path
                ├── other.sh
                └── script.sh

$ cat .gitignore $ 猫 .gitignore

*
!.gitignore
!bin
!bin/sub
!bin/sub/folder
!bin/sub/folder/path
!bin/sub/folder/path/script.sh

$ git status --untracked-files --ignored $ git status --untracked-files --ignored

On branch master

No commits yet

Untracked files:
  (use "git add <file>..." to include in what will be committed)
        .gitignore
        bin/sub/folder/path/script.sh

Ignored files:
  (use "git add -f <file>..." to include in what will be committed)
        bin/ignore.txt
        bin/sub/folder/path/other.sh

nothing added to commit but untracked files present (use "git add" to track)

This is how I keep the structure of folders while ignoring everything else.这就是我在忽略其他所有内容的同时保留文件夹结构的方式。 You have to have a README.md file in each directory (or .gitkeep).您必须在每个目录(或 .gitkeep)中有一个 README.md 文件。

/data/*
!/data/README.md

!/data/input/
/data/input/*
!/data/input/README.md

!/data/output/
/data/output/*
!/data/output/README.md

You can use git config status.showUntrackedFiles no and all untracked files will be hidden from you.您可以使用git config status.showUntrackedFiles no并且所有未跟踪的文件都将对您隐藏。 See man git-config for details.有关详细信息,请参阅man git-config

To exclude folder from .gitignore, the following can be done.要从 .gitignore 中排除文件夹,可以执行以下操作。

!app/

app/*
!app/bower_components/

app/bower_components/*
!app/bower_components/highcharts/

This will ignore all files/subfolders inside bower_components except for /highcharts .这将忽略bower_components中的所有文件/子文件夹,除了/highcharts

There are a bunch of similar questions about this, so I'll post what I wrote before:有很多类似的问题,所以我将发布我之前写的内容:

The only way I got this to work on my machine was to do it this way:我让它在我的机器上工作的唯一方法就是这样做:

# Ignore all directories, and all sub-directories, and it's contents:
*/*

#Now ignore all files in the current directory 
#(This fails to ignore files without a ".", for example 
#'file.txt' works, but 
#'file' doesn't):
*.*

#Only Include these specific directories and subdirectories and files if you wish:
!wordpress/somefile.jpg
!wordpress/
!wordpress/*/
!wordpress/*/wp-content/
!wordpress/*/wp-content/themes/
!wordpress/*/wp-content/themes/*
!wordpress/*/wp-content/themes/*/*
!wordpress/*/wp-content/themes/*/*/*
!wordpress/*/wp-content/themes/*/*/*/*
!wordpress/*/wp-content/themes/*/*/*/*/*

Notice how you have to explicitly allow content for each level you want to include.请注意,您必须如何明确允许要包含的每个级别的内容。 So if I have subdirectories 5 deep under themes, I still need to spell that out.所以如果我在主题下有 5 个子目录,我仍然需要把它拼出来。

This is from @Yarin's comment here: https://stackoverflow.com/a/5250314/1696153这是来自@Yarin 的评论: https ://stackoverflow.com/a/5250314/1696153

These were useful topics:这些是有用的主题:

I also tried我也试过

*
*/*
**/**

and **/wp-content/themes/****/wp-content/themes/**

or /wp-content/themes/**/*/wp-content/themes/**/*

None of that worked for me, either.这些都不适合我。 Lots of trail and error!很多线索和错误!

This is how I did it:我是这样做的:

# Ignore everything
*

# Whitelist anything that's a directory
!*/

# Whitelist some files
!.gitignore

# Whitelist this folder and everything inside of it
!wordpress/wp-content/themes/my-theme/**

# Ignore this folder inside that folder
wordpress/wp-content/themes/my-theme/node_modules

# Ignore this file recursively
**/.DS_Store

Use gig status -u to view individual files in untracked directories recursively - with git status you'd only see folders, which could fool you into thinking that everything inside them was tracked使用gig status -u以递归方式查看未跟踪目录中的单个文件 - 使用git status您只会看到文件夹,这可能会让您误以为其中的所有内容都已被跟踪

I had a problem with subfolder.我的子文件夹有问题。

Does not work:不工作:

/custom/*
!/custom/config/foo.yml.dist

Works:作品:

/custom/config/*
!/custom/config/foo.yml.dist

I tried all answers as given here above, but none worked for me.我尝试了上面给出的所有答案,但没有一个对我有用。 After reading the gitignore documentation ( here ) i found out that if you exclude a folder first that the filenames in the subfolder are not being indexed.阅读 gitignore 文档(此处)后,我发现如果您首先排除文件夹,则子文件夹中的文件名不会被索引。 So if you use the exclamation mark afterwards to include a file, it is not found in the index and thus not being included in your git client.因此,如果您之后使用感叹号来包含文件,则在索引中找不到它,因此不会包含在您的 git 客户端中。

That was the way to finding the solution.这就是找到解决方案的方法。 I started with adding exceptions for all subfolders in my folder tree to get it working, which is a hell of a job.我开始为我的文件夹树中的所有子文件夹添加例外以使其正常工作,这是一项艰巨的工作。 Afterwards i was able to compact the detailed configuration to the configuration below, which is a bit contrary to the documentation..之后我能够将详细配置压缩为下面的配置,这与文档有点相反..

Working .gitignore:工作.gitignore:

# Ignore the 'Pro' folder, except for the '3rdparty' subfolder 
/Pro/*
!Pro/3rdparty/

# Ignore the '3rdparty' folder, except for the 'domain' subfolder
/Pro/3rdparty/*
!Pro/3rdparty/domain/

# Ignore the 'domain' folder, except for the 'modulename' subfolder
Pro/3rdparty/domain/*
!Pro/3rdparty/domain/modulename/

As result i see in my git client that only the two files inside the Pro/3rdparty/domain/modulename/ folder are being staged for the next commit, and that was exactly what i was looking for.结果,我在我的 git 客户端中看到,只有 Pro/3rdparty/domain/modulename/ 文件夹中的两个文件正在为下一次提交暂存,而这正是我想要的。

And if you need to whitelist several subfolders of the same folder then group the exclamation mark lines below the exclude statement like this:如果您需要将同一文件夹的多个子文件夹列入白名单,则将感叹号行分组到 exclude 语句下方,如下所示:

# Ignore the 'Pro' folder, except for the '3rdparty' subfolder 
/Pro/*
!Pro/3rdparty/

# Ignore the '3rdparty' folder, except for the 'domain' & 'hosting' subfolders
/Pro/3rdparty/*
!Pro/3rdparty/domain/
!Pro/3rdparty/hosting/

# Ignore the 'domain' folder, except for the 'modulename' subfolder
Pro/3rdparty/domain/*
!Pro/3rdparty/domain/modulename/

# Ignore the 'hosting' folder, except for the 'modulename' subfolder
Pro/3rdparty/hosting/*
!Pro/3rdparty/hosting/modulename/

Else it wont work as expected.否则它将无法按预期工作。

That's what have worked for me, I wanted to commit only one Cordova plugin to the repo:这对我有用,我只想向 repo 提交一个 Cordova 插件:

...
plugins/*
!plugins/cordova-plugin-app-customization

a lot of complex answers here... here's something pretty simple that I don't see above and works well in many scenarios:这里有很多复杂的答案......这是我在上面看不到的非常简单的东西,并且在许多情况下都能很好地工作:

# ignore all files that have an extension
**/*.*

# except for these extensions
!**/*.extension1
!**/*.extension2

this doesn't ignore any extensionless files, but if you have a bunch that all have the same or similar name, you can add exclusions for those这不会忽略任何无扩展名文件,但如果您有一堆名称相同或相似的文件,您可以为这些文件添加排除项

**/EXTENSIONLESSFILETOIGNORE

I have Jquery and Angular from bower.我有来自凉亭的 Jquery 和 Angular。 Bower installed them in Bower 将它们安装在

/public_html/bower_components/jquery/dist/bunch-of-jquery-files
/public_html/bower_components/jquery/src/bunch-of-jquery-source-files
/public_html/bower_components/angular/angular-files

The minimized jquery is inside the dist directory and angular is inside angular directory.最小化的 jquery 位于dist目录中,而 angular 位于angular目录中。 I only needed minimized files to be commited to github.我只需要将最小化的文件提交到 github。 Some tampering with .gitignore and this is what I managed to conjure...对 .gitignore 进行了一些篡改,这就是我设法想到的……

/public_html/bower_components/jquery/*
!public_html/bower_components/jquery/dist
/public_html/bower_components/jquery/dist/*
!public_html/bower_components/jquery/dist/jquery.min.js
/public_html/bower_components/angular/*
!public_html/bower_components/angular/angular.min.js

Hope someone could find this useful.希望有人能发现这很有用。

Simple solution if you need to ignore everything except few files and few root folders:如果您需要忽略除少数文件和少数文件夹之外的所有内容,则简单的解决方案:

/*
!.gitignore
!showMe.txt
!my_visible_dir

The magic is in /* (as described above) it ignores everything in the (root) folder BUT NOT recursively.魔法就在/*中(如上所述),它会忽略(根)文件夹中的所有内容,但不会递归。

The simplest way that I go about this is to force add a file.我解决这个问题的最简单方法是强制添加文件。 It will be accounted for in git even if it is buried or nested inside a git-ignored subdirectory tree.即使它被掩埋或嵌套在 git 忽略的子目录树中,它也会在 git 中进行说明。

For example:例如:

x64 folder is excluded in .gitignore: x64 文件夹不包含在 .gitignore 中:

x64/

But you want to include the file myFile.py located in x64/Release/ directory.但是您想包含位于x64/Release/目录中的文件myFile.py Then you have to:然后你必须:

git add -f x64/Release/myFile.py

You can do this for multiple files of files that match a pattern eg您可以对匹配模式的多个文件执行此操作,例如

git add -f x64/Release/myFile*.py

and so on.等等。

I got this working我得到了这个工作

# Vendor
/vendor/braintree/braintree_php/*
!/vendor/braintree/braintree_php/lib

Nothing worked for me so far because I was trying to add one jar from lib.到目前为止,没有什么对我有用,因为我试图从 lib 中添加一个 jar。

This did not worked:这没有用:

build/*
!build/libs/*
!build/libs/
!build/libs/myjarfile.jar 

This worked:这有效:

build/*
!build/libs

I also had some issues with the negation of single files.我对单个文件的否定也有一些问题。 I was able to commit them, but my IDE (IntelliJ) always complained about ignored files, which are tracked.我能够提交它们,但我的 IDE (IntelliJ) 总是抱怨被跟踪的被忽略的文件。

git ls-files -i --exclude-from .gitignore

Displayed the two files, which I've excluded this way:显示了我以这种方式排除的两个文件:

public/
!public/typo3conf/LocalConfiguration.php
!public/typo3conf/PackageStates.php

In the end, this worked for me:最后,这对我有用:

public/*
!public/typo3conf/
public/typo3conf/*
!public/typo3conf/LocalConfiguration.php
!public/typo3conf/PackageStates.php

The key was the negation of the folder typo3conf/ first.关键是首先否定了typo3conf/文件夹。

Also, it seems that the order of the statements doesn't matter .此外,似乎语句的顺序无关紧要 Instead, you need to explicitly negate all subfolders, before you can negate single files in it.相反,您需要明确否定所有子文件夹,然后才能否定其中的单个文件。

The folder !public/typo3conf/ and the folder contents public/typo3conf/* are two different things for .gitignore.文件夹!public/typo3conf/和文件夹内容public/typo3conf/*对于 .gitignore 来说是两个不同的东西。

Great thread!伟大的线程! This issue bothered me for a while ;)这个问题困扰了我一段时间;)

I seem to have found something that worked for me which no one else mentioned.我似乎找到了其他人没有提到的对我有用的东西。

# Ignore everything
*

# But not these files...
!.gitignore
!script.pl
!template.latex
# etc...

# And if you want to include a sub-directory and all sub-directory and files under it, but not all sub-directories
!subdir/
!subdir/**/*

Basically, it seems to negate a sub-directory from being ignored, you have to have two entries, one for the sub-directory itself !subdir/ and then another one which expands to all files and folders under it !subdir/**/*基本上,它似乎否定了一个子目录被忽略,你必须有两个条目,一个用于子目录本身!subdir/另一个用于扩展它下的所有文件和文件夹!subdir/**/*

It's a disaster that after 16 years (2005) of using git , there is no simple and obvious way to exclude one file when located deep in a directory hierarchy, that should otherwise be ignored.这是一场灾难,在使用git 16 年(2005 年)之后,当位于目录层次结构的深处时,没有简单而明显的方法来排除一个文件,否则应该被忽略。 Instead we need to resort to crazy things like repeatedly digging down the structure and excluding and including directories in the correct order.相反,我们需要诉诸疯狂的事情,例如反复挖掘结构并以正确的顺序排除和包含目录。 Something which is darn right impossible to remember those 4 times a year you need to do it.一年 4 次你需要做的事情是不可能记住的。

The only smart, but very weird alternative, is to use the git add --force .唯一聪明但非常奇怪的选择是使用git add --force Something which is bound to fail at some point, when you're not working on the gitted repo alone.当您不单独处理 gitted repo 时,某些时候肯定会失败。

So I wrote a small bash function to fix this for you, for easy copy paste.所以我写了一个小 bash 函数来为你解决这个问题,以便于复制粘贴。 Let me first explain the one liner:让我先解释一下单线:

f=0; y='';for x in $(echo "uploads/rubbish/stuff/KEEP_ME/a.py" | tr '\/' '\n'); do y="$y/$x"; if [ $(($f)) -eq 0 ]; then y="$x"; f=1; fi; echo -e '!'"$y/\n$y/*"; done | sed '$d' |sed -z 's/..$//'; echo;

# output:
!uploads/
uploads/*
!uploads/rubbish/
uploads/rubbish/*
!uploads/rubbish/stuff/
uploads/rubbish/stuff/*
!uploads/rubbish/stuff/KEEP_ME/
uploads/rubbish/stuff/KEEP_ME/*
!uploads/rubbish/stuff/KEEP_ME/a.py

Description描述

  • There is an if statement to adjust for the 1st item not getting a / .有一个if语句来调整第一个项目没有得到/
  • tr '\/' '\n' - translate the / to \n in standard POSIX paths (to get a list) tr '\/' '\n' - 将标准 POSIX 路径中的/转换为\n (以获取列表)
  • y="$y/$x" - add the next sub-dirctory (in list) after the previous directory . y="$y/$x" - 在上一个目录之后添加下一个子目录(在列表中)。
  • echo -e '!'"$y/\n$y/*" - echo -e '!'"$y/\n$y/*" -
    print the 2 lines of sub-directory item, 1st with !打印 2 行子目录项,第 1 行带有! prefix , and 2nd with /* postfix . prefix ,第二个是/* postfix
  • sed '$d' - remove last line sed '$d' - 删除最后一行
  • sed -z 's/..$//' - remove last 2 characters /* from last line sed -z 's/..$//' - 从最后一行删除最后 2 个字符/*

Then we create the function:然后我们创建函数:

function gitkeep () { f=0; y='';for x in $(echo "$1" | tr '\/' '\n'); do y="$y/$x"; if [[ f -eq 0 ]]; then y="$x"; f=1; fi; echo -e '!'"$y/\n$y/*"; done | sed '$d' |sed -z 's/..$//'; echo; }

# gitkeep "uploads/rubbish/stuff/KEEP_ME/a"

!uploads/
uploads/*
!uploads/rubbish/
uploads/rubbish/*
!uploads/rubbish/stuff/
uploads/rubbish/stuff/*
!uploads/rubbish/stuff/KEEP_ME/
uploads/rubbish/stuff/KEEP_ME/*
!uploads/rubbish/stuff/KEEP_ME/a

Here I simplified the arithmetic if statement to if [[ f -eq 0 ]];这里我将算术if语句简化为if [[ f -eq 0 ]]; . .

Enjoy!享受!

Gist要旨

# Ignore everything
*

# But not these files...
!script.pl
!template.latex

And probably include:可能包括:

!.gitignore

Reference参考

From https://git-scm.com/docs/gitignore :来自https://git-scm.com/docs/gitignore

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 ”,在第一个“ ! ”前面放置一个反斜杠(“ \ ”)。

... ...

Example to exclude everything except a specific directory foo/bar (note the /* - without the slash, the wildcard would also exclude everything within foo/bar ):排除除特定目录foo/bar之外的所有内容的示例(注意/* - 没有斜杠,通配符也将排除foo/bar中的所有内容):

 $ cat .gitignore # exclude everything except directory foo/bar /* !/foo /foo/* !/foo/bar

Via github.com/gitinclude your can download a command line tool (for windows, linux and osx) for including deeply nested sets of files.通过github.com/gitinclude,您可以下载一个命令行工具(适用于 windows、linux 和 osx),用于包含深度嵌套的文件集。

Define the file sets in a .gitinclude and execute the tool (via double click or command line)..gitinclude中定义文件集并执行该工具(通过双击或命令行)。 Your .gitignore is now updated.您的.gitignore现已更新。

Not sure if this has been pointed out already but I was having trouble ignoring a subfolder inside an ignored folder using the !不确定是否已经指出了这一点,但我无法使用!忽略忽略文件夹中的子文件夹。 in front of the folder i want gitignore to ignore.在我希望 gitignore 忽略的文件夹前面。

However I figured out that the folder that was being ignored had no * in its path.但是我发现被忽略的文件夹在其路径中没有* My.gitignore looked like this: My.gitignore 看起来像这样:

/folder/
!/folder/subfolder

The subfolder was still being ignored, but adding a * after folder made it work like so子文件夹仍然被忽略,但是在文件夹之后添加*使它像这样工作

/folder/*
!/folder/subfolder

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

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