在 Git 仓库中仅包含 .gitignore、.bashrc、.vimrc、.vim/

Including only .gitignore, .bashrc, .vimrc, .vim/ in a Git repo

我认为直到现在我对 .gitignore 文件的处理还不错。我试图在 Git 存储库中包含 以下文件:

我在 Whosebug 上阅读了其他几个答案 (How do I ignore files in a directory in Git?, Git ignore sub folders and Is there a way to tell git to only include certain files instead of ignoring certain files?)。但是,我似乎无法正确设置 .gitignore 文件。这是目前我的 .gitignore 文件中的内容:

*
.*
!.gitignore
!.bashrc*
!.vimrc
!.vim/

在我 运行 git init; git add .; git status 之后,回购包含:

$ git init; git add .; git status
Initialized empty Git repository in /home/username/.git/
On branch master

Initial commit

Changes to be committed:
  (use "git rm --cached <file>..." to unstage)

    new file:   .bashrc
    new file:   .gitignore
    new file:   .vimrc

我尝试了 .gitignore 文件最后一行的变体,但无济于事。谁能发现我的错误?

编辑

我正在尝试找到一种解决方案,该解决方案仅 .gitignore 文件。

您只需要忽略文件(* 忽略文件和文件夹)

**
.**

然后您可以将文件夹及其文件列入白名单:

!.vim/
!.vim/**

仔细检查 .gitignore 规则适用于:

git check-ignore -v .vim/afile

你想要的是 check-ignore 到 return 什么都没有。