【问题标题】:Including only .gitignore, .bashrc, .vimrc, .vim/ in a Git repo在 Git 存储库中仅包括 .gitignore、.bashrc、.vimrc、.vim/
【发布时间】:2018-05-19 20:13:34
【问题描述】:

直到现在,我还以为我对 .gitignore 文件的处理非常好。我正在尝试在 Git 存储库中包含仅以下文件:

  • .gitignore
  • .bashrc
  • .vimrc
  • .vim/(此目录中的所有内容)

我已经阅读了 StackOverflow 上的其他几个答案(How do I ignore files in a directory in Git?、Git ignore sub folders 和 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 后,repo 包含:

$ 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/ 目录包含一个名为bundle 的子目录。 bundle 文件夹下有很多文件和目录。 “输出似乎正确”是什么意思?我的目标是包含.vim/ 文件夹及其中的所有内容。
  • 你可以使用-force标志:git add -f .vim
  • 我尝试了!/.vim,结果相同。我可以使用-force 标志,但我试图通过仅修改.gitignore 文件来找到可行的解决方案。我会更新问题以反映这一点。

标签: git gitignore


【解决方案1】:

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

**
.**

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

!.vim/
!.vim/**

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

git check-ignore -v .vim/afile

你想要的是 check-ignore 什么都不返回。

【讨论】:

  • 我尝试了您的建议,但没有添加.vim/ 文件夹。它只添加了.gitignore、.bashrc 和.vimrc。我首先做了rm -rf ~/.git/,然后使用** 和.** 编辑了.gitignore。然后我运行 git init; git add .; git status 来查看添加到 repo 中的文件。
  • @MikeyE 尝试git check-ignore -v .vim/afile 查看应用了哪个忽略规则。我已经编辑了答案。
  • 您的 cmets 关于 ** 忽略文件和 * 忽略文件和文件夹为我清除了它。我已选择您的答案作为接受的答案。
猜你喜欢
  • 1970-01-01
  • 2014-07-08
  • 2014-02-05
  • 1970-01-01
  • 1970-01-01
  • 2023-03-27
  • 1970-01-01
  • 2011-08-11
  • 1970-01-01
相关资源
最近更新 更多