我知道.gitignore文件隐藏Git版本控制中的指定文件。

我如何告诉.gitignore忽略除我使用Git跟踪的文件之外的所有内容?类似于:

# Ignore everything:
*

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

当前回答

这就是我保持文件夹结构的方式,而忽略其他所有内容。每个目录(或.gitkeep)中都必须有一个README.md文件。

/data/*
!/data/README.md

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

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

其他回答

如果您需要忽略除少数文件和少数根文件夹之外的所有内容,则简单的解决方案:

/*
!.gitignore
!showMe.txt
!my_visible_dir

神奇的是在/*(如上所述)中,它忽略(根)文件夹中的所有内容,但不递归。

# ignore these
*
# except foo
!foo

在大多数情况下,您希望使用/*而不是*或*/

使用*是有效的,但它以递归方式工作。从那时起,它就不会查看目录了。人们建议使用!*/再次包含列表目录,但实际上最好用/*

# 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

上述代码将忽略除.gitignore、README.md、文件夹/a/file.txt、文件夹/a/b1/和文件夹/a/b2/之外的所有文件以及最后两个文件夹中包含的所有内容。(这些文件夹中的.DS_Store和*.log文件将被忽略。)

很明显我能做到/文件夹或!/。gitignore也是。

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

这就是我保持文件夹结构的方式,而忽略其他所有内容。每个目录(或.gitkeep)中都必须有一个README.md文件。

/data/*
!/data/README.md

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

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

最简单的方法是强制添加文件。即使它被隐藏或嵌套在git忽略的子目录树中,它也将在git中进行说明。

例如:

x64文件夹已在中排除。gitignore:

x64/

但您希望包含位于x64/发布/目录中的文件myFile.py。然后你必须:

git add -f x64/Release/myFile.py

您可以对匹配模式的多个文件执行此操作,例如。

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

等等