Ignore .git folder in sub folder

2020-08-12 16:22发布

Is it possible to add a sub folder with a .git folder to a repo, without Git treating it like a submodule? I've tried different methods to ignore that .git folder, but nothing this far has worked.

I have tried in /.gitignore:

/vendor/**/.git/

..and in /vendor/.gitignore:

.git/

The .git folder I want to ignore is in /vendor/foo/bar/.

标签: git
7条回答
欢心
2楼-- · 2020-08-12 16:56

You can use Git excludes for this.

First, create a .gitexclude file:

$ echo "vendor/*/.git" >> .gitexclude

Then add this file to your Git config as a source of exclusions:

$ git config core.excludesFile .gitexclude

Now you can add files and Git doesn't offer to add them as submodules:

$ git add vendor
$

You can verify that a file is indeed "invisible" to Git with git check-ignore:

$ git check-ignore vendor/foo/.git
vendor/foo/.git
$ git check-ignore vendor/foo/bar.txt
$

Reference: man git-add(1) man git-check-ignore(1)

查看更多
登录 后发表回答