Git 吉特元';不要忽略一个目录

Git 吉特元';不要忽略一个目录,git,Git,我只想在一个更大的项目中跟踪git中的某些目录,其中大多数目录将被排除在外,只有少数目录将被跟踪。所以,我想在我的.gitignore或excludes文件中使用not运算符,ja 为什么会发生这种情况: % mkdir wtfgit % cd wtfgit % git init Initialized empty Git repository in /home/foobar/wtfgit/.git/ % mkdir iwantthesefiles % touch iwantthesefile

我只想在一个更大的项目中跟踪git中的某些目录,其中大多数目录将被排除在外,只有少数目录将被跟踪。所以,我想在我的.gitignore或excludes文件中使用not运算符,ja

为什么会发生这种情况:

% mkdir wtfgit
% cd wtfgit 
% git init
Initialized empty Git repository in /home/foobar/wtfgit/.git/
% mkdir iwantthesefiles
% touch iwantthesefiles/foo
% mkdir idontwantthesefiles
% touch idontwantthesefiles/bar
% vim .gitignore
% cat .gitignore 
*
!iwantthesefiles/        
% git add --all
% git status
# On branch master
#
# Initial commit
#
nothing to commit (create/copy files and use "git add" to track)
“not”语法适用于最高目录中的文件

% touch baz          
% vim .gitignore
% cat .gitignore 
*
!iwantthesefiles/
!baz
% git add --all 
% git status    
# On branch master
#
# Initial commit
#
# Changes to be committed:
#   (use "git rm --cached <file>..." to unstage)
#
#   new file:   baz
#
%touch baz
%vim.gitignore
%猫,吉特
*
!我想要这些文件/
!巴兹
%git add--all
%git状态
#论分行行长
#
#初始提交
#
#要提交的更改:
#(使用“git rm--cached…”取消存储)
#
#新档案:baz
#
我怎样才能把baz和foo作为git跟踪文件提供给我?(基本上,目录非样式的globbing是如何工作的?!似乎不是。)


谢谢

不要提及.git文件中的目录,只提及文件。执行此操作并显式添加要跟踪的目录应该可以满足您的需要:

$ cat > .gitignore << EOF * !bar EOF $ git add -f iwantthesefiles
$cat>.gitignoreGit的行为如下:它不会忽略存储库中的文件。此外,要排除目录,请以斜杠结束。正如您在下面的示例中所看到的,只跟踪了iwantthesefiles/baz

$ cat .gitignore
*/
!iwantthesefiles
$ git status
# On branch master
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
#       .gitignore
#       iwantthesefiles/baz
nothing added to commit but untracked files present (use "git add" to track)
$ ls -lhR
.:
total 8.0K
drwxr-xr-x 2 user group 4.0K 2009-12-29 14:24 idonntwantthesefiles
drwxr-xr-x 2 user group 4.0K 2009-12-29 14:24 iwantthesefiles

./idonntwantthesefiles:
total 0
-rw-r--r-- 1 user group 0 2009-12-29 14:22 bar
-rw-r--r-- 1 user group 0 2009-12-29 14:24 baz

./iwantthesefiles:
total 0
-rw-r--r-- 1 user group 0 2009-12-29 14:24 baz
-rw-r--r-- 1 user group 0 2009-12-29 14:19 foo
$ rm -r * && git reset --hard && ls -lhR
HEAD is now at 698c66a monkey
.:
total 4.0K
drwxr-xr-x 2 user group 4.0K 2009-12-29 14:25 iwantthesefiles

./iwantthesefiles:
total 0
-rw-r--r-- 1 user group 0 2009-12-29 14:25 foo
$cat.gitignore
*/
!我想要这些文件
$git状态
#论分行行长
#未跟踪的文件:
#(使用“git add…”包含在将提交的内容中)
#
#.gitignore
#我想要这些文件/baz
提交时未添加任何内容,但存在未跟踪的文件(使用“git add”跟踪)
$ls-lhR
.:
总共8.0公里
drwxr-xr-x 2用户组4.0K 2009-12-29 14:24我不发送这些文件
drwxr-xr-x 2用户组4.0K 2009-12-29 14:24 iwant这些文件
/IDonTwant这些文件:
总数0
-rw-r--r--1用户组0 2009-12-29 14:22巴
-rw-r--r--1用户组0 2009-12-29 14:24 baz
/我想要这些文件:
总数0
-rw-r--r--1用户组0 2009-12-29 14:24 baz
-rw-r--r--1用户组0 2009-12-29 14:19 foo
$rm-r*&&git重置-硬和ls-lhR
头部现在是698c66a
.:
总计4.0K
drwxr-xr-x 2用户组4.0K 2009-12-29 14:25 iwant这些文件
/我想要这些文件:
总数0
-rw-r--r--1用户组0 2009-12-29 14:25 foo
并且,为了演示跟踪文件覆盖.gitignore,请执行以下操作:

$ mkdir idonntwantthesefiles
$ touch idonntwantthesefiles/baz
$
git add idonntwantthesefiles/baz
$ git commit -m 'llama'
Created commit a62b6d5: llama
 0 files changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 idonntwantthesefiles/baz
$ echo foobar > idonntwantthesefiles/baz
$ git status
# On branch master
# Changed but not updated:
#   (use "git add <file>..." to update what will be committed)
#
#       modified:   idonntwantthesefiles/baz
#
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
#       .gitignore
no changes added to commit (use "git add" and/or "git commit -a")
$mkdir IDONNTWANTTHES文件
$touch i不发送这些文件/baz
$
git添加IDonTwantTheseFiles/baz
$git commit-m'llama'
创建提交a62b6d5:骆驼
0个文件已更改,0个插入(+),0个删除(-)
创建模式100644 IDONTWANTTESFILES/baz
$echo foobar>IDonTwantTheseFiles/baz
$git状态
#论分行行长
#已更改但未更新:
#(使用“git add…”更新将提交的内容)
#
#已修改:IDonTwantTheseFiles/baz
#
#未跟踪的文件:
#(使用“git add…”包含在将提交的内容中)
#
#.gitignore
未向提交添加任何更改(使用“git add”和/或“git commit-a”)

恢复旧帖子,以防其他人无意中发现:

如果要忽略当前目录中的所有文件夹/文件,请使用前导斜杠(
/*/
用于目录或
/*
用于文件);否则Git将递归地忽略该模式,这将导致未忽略目录中的文件也被忽略


记住这一点,
!目录/
pattern unignores目录很好。

要强制添加被忽略的文件夹,请使用:

git add -f my-ignored-folder

如果以上工作都没有,请在.gitignore中尝试此变体

Not working for me:
!build/**/*
Working for me:
!build**/*

出于某种原因,这是唯一对我有效的方法。

对我来说,我不得不添加两行:
!脚本/
!脚本/*