我知道.gitignore文件隐藏Git版本控制中的指定文件。
我如何告诉.gitignore忽略除我使用Git跟踪的文件之外的所有内容?类似于:
# Ignore everything:
*
# Do not ignore these files:
script.pl
template.latex
我知道.gitignore文件隐藏Git版本控制中的指定文件。
我如何告诉.gitignore忽略除我使用Git跟踪的文件之外的所有内容?类似于:
# Ignore everything:
*
# Do not ignore these files:
script.pl
template.latex
当前回答
要忽略目录中的某些文件,必须按正确的顺序执行此操作:
例如,忽略文件夹“application”中除index.php和文件夹“config”外的所有内容,注意顺序。
你必须先否定你想要的。
失败
应用程序/*
!应用程序/配置/*
!应用程序/index.php
作品
!应用程序/配置/*
!应用程序/index.php
应用程序/*
其他回答
在大多数情况下,您希望使用/*而不是*或*/
使用*是有效的,但它以递归方式工作。从那时起,它就不会查看目录了。人们建议使用!*/再次包含列表目录,但实际上最好用/*
# 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
我是这样做的:
# 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
使用gig status-u递归查看未跟踪目录中的单个文件-使用git status,您只能看到文件夹,这可能会让您误以为其中的所有内容都被跟踪
# ignore these
*
# except foo
!foo
与OP有类似的问题,但前10名投票支持的答案中没有一个有效。我终于发现了
错误语法:
*
!bin/script.sh
正确语法:
*
!bin
!bin/script.sh
gitignore手册页的解释:
一个可选前缀“!”,用于否定模式;被先前模式排除的任何匹配文件将再次包含。如果排除了文件的父目录,则无法重新包含该文件。由于性能原因,Git没有列出排除的目录,因此包含的文件上的任何模式都不会产生任何影响,无论它们是在哪里定义的。
这意味着上面的“错误语法”是错误的,因为bin/script.sh不能重新包含,因为bin/被忽略。这就是全部。
扩展示例:
$tree。
.
├── .gitignore
└── bin
├── ignore.txt
└── sub
└── folder
└── path
├── other.sh
└── script.sh
$cat.gitignore
*
!.gitignore
!bin
!bin/sub
!bin/sub/folder
!bin/sub/folder/path
!bin/sub/folder/path/script.sh
$git状态--未跟踪的文件--已忽略
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)
如果您想忽略目录中除一个文件外的全部内容,可以为文件路径中的每个目录编写一对规则。例如,.gitignore将忽略pippo文件夹,pippo/pluto/paperino.xml除外
pippo/*
!pippo/pluto
pippo/pluto/*
!pippo/pluto/paperino.xml
请注意,如果您只是在上面写了:
pippo/*
!pippo/pluto/paperino.xml
它无法工作,因为Git中不存在中间的pluto文件夹,因此paperino.xml找不到存在的位置。