首页 > 解决方案 > .gitignore of the parent module affects add and commit of the child module

问题描述

In my home directory, I have a git repository to track configuration files, such as .zshrc, .vimrc. The .gitignore file excludes all the files except these specified ones.

In my ~/Desktop/ directory, I have cloned one other project, and it has its own .gitignore. However, when I created new files in this child module and try to add and commit them. It always says "nothing to commit, working tree clean"

I try to delete the .gitignore of the parent module, and the child module can add and commit. This means "git add" of my child module is affected by the parent module's .gitignore. I also tried created dummy nested repositories, the .gitignore from parent did not affect its child repository. Now I am confused with this different behaviour.

This is parent's .gitignore

# Ignore everything
*

# But not these files:
!.gitignore
!.zshrc
!.bash_profile
!.vimrc

标签: git

解决方案


TL;博士

git config --global core.excludesFile <file name for global ignore patterns>

细节

根据我的经验,.gitignore在您的主目录中适用于全局。

经过一番研究,我发现该设置core.excludesFile定义了一个要在每个沙箱中读取的忽略文件。在我的 Linux 机器上,默认为~/.gitignore,而在我的 Windows 机器上,它在其他地方,我不确定在哪里。

在您的情况下,它显然在您的家中,因此您需要对其进行更改,以便您的家 Git 存储库.gitignore也不是您的全局存储库:

git config --global core.excludesFile ~/.gitignore.global

现在,如果你想在任何地方忽略一些东西,你把它们放在那个文件中,你~/.gitignore只会应用到你的家庭 Git 存储库本身。


推荐阅读