[英]Ignore .git folder in sub folder
是否可以将具有.git文件夹的子文件夹添加到repo,而不将Git视为子模块? 我已经尝试了不同的方法来忽略.git文件夹,但是迄今为止没有任何方法可行。
我在/.gitignore尝试过:
/vendor/**/.git/
..and in /vendor/.gitignore:
.git/
我想忽略的.git文件夹位于/ vendor / foo / bar /中。
您可以先直接添加一些(任何)内部内容来完成此操作。 Git在搜索新遇到的目录时通过遇到.git
条目来检测子模块,但如果你给它一个实际的路径来查找该目录,它就不会搜索。
所以
git add path/to/some/submodule/file # bypass initial search that detects .git
git add path/to/some/submodule # now git's index has it as ordinary dir
你可以使用git hooks来实现你想要的。 开箱即用,你可以创建一个预提交钩子,重命名你所包含的项目的.git目录,例如。 到“.git2”,添加后一个项目中的所有文件,除了“.git2”目录,提交全部,推送它,最后使用post-commit hook将“.git2”文件夹重命名为模块中的“.git”。
1)使用内容在根 目录的.git / hooks /下创建预提交文件:
#!/bin/sh
mv "vendor/foo/bar/.git" "vendor/foo/bar/.git2"
git rm --cached vendor/foo/bar
git add vendor/foo/bar/*
git reset vendor/foo/bar/.git2
2)在.git / hooks /下创建post-commit文件,内容如下:
#!/bin/sh
mv "vendor/foo/bar/.git2" "vendor/foo/bar/.git"
3)更改仓库中的文件,最后:
git commit -a -m "Commit msg"
git push
你的问题引起了我的兴趣,所以我试了一下:
$ cd /tmp
$ git init foo
Initialized empty Git repository in /private/tmp/foo/.git/
$ cd foo
$ git init bar
Initialized empty Git repository in /private/tmp/foo/bar/.git/
$ cd bar
$ echo "this is bar" > bar.txt
$ git add bar.txt
$ git commit -m "initial commit"
[master (root-commit) c9d6507] initial commit
1 file changed, 1 insertion(+)
create mode 100644 bar.txt
$ cd ..
$ pwd
/tmp/foo
$ git status
On branch master
No commits yet
Untracked files:
(use "git add <file>..." to include in what will be committed)
bar/
nothing added to commit but untracked files present (use "git add" to track)
$ git add bar
$ git commit -m "parent initial commit"
[master (root-commit) b23e106] parent initial commit
1 file changed, 1 insertion(+)
create mode 100644 bar/bar.txt
$ echo "baz" > bar/.git/baz.txt
$ git status
On branch master
nothing to commit, working tree clean
根据您的问题, /tmp/foo
是一个Git存储库。 对于/tmp/foo
, /tmp/foo/bar
是一个子目录,它本身有一个.git
目录。
如您所见,父foo
存储库完全忽略了bar/.git
子目录,我甚至不需要.gitignore
文件。
并且Git没有办法将包含.git
文件夹的路径作为子模块处理,而没有在.gitmodules
文件中注册。
与jthill回答一样 ,您可以使用.git
从子目录添加任何项目,它将被视为子目录而不是子模块。
但是如果您已经将此子目录注册为具有.git
子模块,则必须首先删除该子模块,但将其保留在工作树中 - git rm --cached a/submodule
。
更多信息: 如何删除子模块?
然后你可以重复jthill回答的步骤。
根据我的研究,没有办法停止将.git
文件夹作为子模块处理。 因此,我现在能够想到的最佳解决方案是使用bash
或任何类似的脚本,您可以在控制台的任何文件夹中运行。 它应该找到任何具有.git
子文件夹并尝试将该文件夹中的任何文件添加到主.git
,因此它将停止将它们视为子模块。
下面的代码源: Bash脚本自动将git子模块转换为常规文件
#!/bin/bash
# Get a list of all the submodules
submodules=($(git config --file .gitmodules --get-regexp path | awk '{ print $2 }'))
# Loop over submodules and convert to regular files
for submodule in "${submodules[@]}"
do
echo "Removing $submodule"
git rm --cached $submodule # Delete references to submodule HEAD
rm -rf $submodule/.git* # Remove submodule .git references to prevent confusion from main repo
git add $submodule # Add the left over files from the submodule to the main repo
git commit -m "Converting submodule $submodule to regular files" # Commit the new regular files!
done
# Finally remove the submodule mapping
git rm.gitmodules
请记住在尝试新脚本之前始终提交/备份更改以防止任何工作/数据丢失。
虽然它是真的.git
会自动被忽略,但主要的repo会将/vendor/foo/bar
视为gitlink(主要repo索引中的特殊条目),而不是常规文件。
一个技巧只是将 bar/.git
移动到主回购之外 :
vendor/foo/bar
将被视为常规子文件夹,如果需要,将添加到主仓库。 git
命令之前使用GIT_DIR=/path/to/bar/.git git ...
,bar repo可以随时检测更改并进行提交GIT_DIR=/path/to/bar/.git git ...
例如:
cd /path/to/main/repo/vendor/foo/bar
GIT_DIR=/path/to/bar/.git git status
GIT_DIR=/path/to/bar/.git git add .
GIT_DIR=/path/to/bar/.git git commit -m "changes to bar"
GIT_DIR=/path/to/bar/.git git push
你可以使用Git排除。
首先,创建一个.gitexclude文件:
$ echo "vendor/*/.git" >> .gitexclude
然后将此文件添加到您的Git配置中作为排除源:
$ git config core.excludesFile .gitexclude
现在你可以添加文件而Git不提供将它们添加为子模块:
$ git add vendor
$
您可以通过git check-ignore
验证文件确实对Git“不可见”:
$ git check-ignore vendor/foo/.git
vendor/foo/.git
$ git check-ignore vendor/foo/bar.txt
$
参考:man git-add(1)man git-check-ignore(1)
你试过通配符吗?
**/vendor/.git/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.