Git ignore - 如何将异常重写为 ignore-all?

Git ignore - How do you override an exception to an ignore-all?

我想为我的 bash 设置和插件等创建一个 git 存储库。我忽略了所有内容(第 0 行),然后在回购中手动添加了我想要的 files/folders 。 (我必须这样做,因为存储库在我的 ~ 文件夹中。)我想忽略 .vim/colors/ 目录中的所有颜色配置文件,但我确实想包含一个文件我正在使用 (apprentice.vim).

.vim/colors/* 行似乎不起作用 - 它根本不忽略任何文件。使用 !!.vim/colors/* 也不起作用。我应该如何让它覆盖所有以前的规则并忽略颜色文件夹,然后仍然允许忽略 apprentice.vim 文件?

/*

*.swp

!.gitignore

!.bashrc
!.bash_profile

!.vimrc
!.vim
.vim/colors/* # Don't include all of the other color schemes
!.vim/colors/apprentice.vim

你的规则似乎没问题。除了 *.swp 会被 /* 覆盖并且评论不在自己的行上。

如您所见,所有其他 .vim/colors 都被忽略了。

如果您的文件已经暂存,您可能需要取消暂存它们并将它们从存储库中删除,然后重新添加它们。

git rm --cached ./.vim/colors/*
git add ./.vim/colors/
git commit -m"Unstaged .vim/colors"

最终要使用的 .gitignore

/*
!.gitignore

!.bashrc
!.bash_profile

!.vimrc
!.vim
.vim/colors/*
!.vim/colors/apprentice.vim

问题是 # comment.vim/colors/* 在同一行,但这里有一个替代方案。

gitignore 的主要规则是:

It is not possible to re-include a file if a parent directory of that file is excluded.

这意味着:
(假设元素尚未版本化,在这种情况下您需要先 git rm --cached 它们):

  • 您需要递归地忽略所有 文件:即“**
  • 递归排除所有文件夹:那些是'**/'
  • 排除你想要的文件(这会起作用,因为它的父文件夹也不会被忽略)

结果:

/**
!/**/
!exclude what you want to *not* be ignored
# for instance
.vim/colors/* # Don't include all of the other color schemes
!.vim/colors/apprentice.vim

检查 git check-ignore -v 忽略和未忽略的内容(-v 很重要):

git check-ignore -v -- afile

这比手动取消忽略子文件夹更容易,尤其是当要取消忽略的子文件夹内容有几层深时:要排除 a/b/c 中的文件,您需要先取消忽略-忽略 !/a,然后 !/a/b,然后 !/a/b/c)


Illustration/test:

C:\Users\vonc\prog\git\tests>git init i
Initialized empty Git repository in C:/Users/vonc/prog/git/tests/i/.git/

C:\Users\vonc\prog\git\tests>cd i

C:\Users\vonc\prog\git\tests\i>mkdir .vim\colors

C:\Users\vonc\prog\git\tests\i>touch .vim\colors\apprentice.vim

C:\Users\vonc\prog\git\tests\i>git st
On branch master

Initial commit

Untracked files:
  (use "git add <file>..." to include in what will be committed)

        .vim/

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

具有 /* 规则的简单 .gitignore

C:\Users\vonc\prog\git\tests\i>sbt .gitignore
/*

C:\Users\vonc\prog\git\tests\i>git st
On branch master

Initial commit

nothing to commit (create/copy files and use "git add" to track)

让我们添加 !.gitignore.
.gitignore 现在可以追踪了。

C:\Users\vonc\prog\git\tests\i>git st
On branch master

Initial commit

Untracked files:
  (use "git add <file>..." to include in what will be committed)

        .gitignore

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

但是如果我添加:

.vim/colors/*
!.vim/colors/apprentice.vim

.vim 全部内容被仍然忽略:

C:\Users\vonc\prog\git\tests\i>git st
On branch master

Initial commit

Untracked files:
  (use "git add <file>..." to include in what will be committed)

        .gitignore

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

让我们用 git check-ignore 来检查原因:

C:\Users\vonc\prog\git\tests\i>git check-ignore -v -- .vim\colors\apprentice.vim
.gitignore:1:/* ".vim\colors\apprentice.vim"

添加 !.vim 有效,因为它取消忽略文件夹,允许应用该文件夹中的其他规则。

不过,这更简单:

/**
!/**/
!.gitignore
.vim/colors/*
!.vim/colors/apprentice.vim