git 使 .gitignore 忽略除少数文件之外的所有内容

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/987142/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 06:35:43  来源:igfitidea点击:

Make .gitignore ignore everything except a few files

gitgitignore

提问by Andrew

I understand that a .gitignore file cloaks specified files from Git's version control. I have a project (LaTeX) that generates lots of extra files (.auth, .dvi, .pdf, logs, etc) as it runs, but I don't want those to be tracked.

我知道 .gitignore 文件隐藏了 Git 版本控制中的指定文件。我有一个项目 (LaTeX),它在运行时会生成大量额外文件(.auth、.dvi、.pdf、日志等),但我不希望跟踪这些文件。

I'm aware that I could (maybe should) make it so all those files are put in an separate subfolder in the project, since I could then just ignore the folder.

我知道我可以(也许应该)将所有这些文件放在项目中的一个单独的子文件夹中,因为这样我就可以忽略该文件夹。

However, is there any feasible way to keep the output files in the root of the project tree and use .gitignore to ignore everything except the files I'm tracking with Git? Something like

但是,是否有任何可行的方法可以将输出文件保留在项目树的根目录中并使用 .gitignore 忽略除我使用 Git 跟踪的文件之外的所有内容?就像是

# Ignore everything
*

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

回答by Joakim Elofsson

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/*

回答by Giuseppe Galano

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

.gitignore

.gitignore

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

回答by Ryan Taylor

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 whitelist directories again, but it's actually better to blacklist the highest level folder with /*

使用*是有效的,但它会递归地工作。从那时起它不会查看目录。人们建议!*/再次使用白名单目录,但实际上最好将最高级别的文件夹列入黑名单/*

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

# Whitelist some files
!.gitignore
!README.md

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

# Whitelist 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. (And .DS_Storeand *.logfiles would be ignored in those folders.)

上面的代码会忽略所有的文件,除了.gitignoreREADME.mdfolder/a/file.txtfolder/a/b1/folder/a/b2/和包含在那些过去的两个文件夹的一切。(并且.DS_Store*.log文件将在这些文件夹中被忽略。)

Obviously I could do e.g. !/folderor !/.gitignoretoo.

显然我可以做 eg!/folder或者!/.gitignore也可以。

More info: http://git-scm.com/docs/gitignore

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

回答by Nik

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 cachefolder:

注意cache文件夹后面的斜杠 (/) :

FAILS

失败

webroot/cache*
!webroot/cache/.htaccess

WORKS

作品

webroot/cache/*
!webroot/cache/.htaccess

回答by Suvesh Pratapa

# ignore these
*
# except foo
!foo

回答by slatunje

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/*

!application/config/*

!application/config/*

!application/index.php

!application/index.php

WORKS

作品

!application/config/*

!application/config/*

!application/index.php

!application/index.php

application/*

application/*

回答by Robert Munteanu

You can use git config status.showUntrackedFiles noand all untracked files will be hidden from you. See man git-configfor details.

您可以使用git config status.showUntrackedFiles no,所有未跟踪的文件将对您隐藏。详情请参阅man git-config

回答by Guy Baskin

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_componentsexcept for /highcharts.

这将忽略里面的所有文件/子文件夹,bower_components除了/highcharts.

回答by Katie

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!

这些都不适合我。很多线索和错误!

回答by Fabian Picone

I had a problem with subfolder.

我的子文件夹有问题。

Does not work:

不起作用:

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

Works:

作品:

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