#!/bin/sh
mv "vendor/modulename/.git" "vendor/modulename/.git2"
git rm --cached vendor/modulename
git add vendor/modulename/*
git reset vendor/modulename/.git2
2)在.git/hooks/下创建post-commit文件,并写入以下内容:
#!/bin/sh
mv "vendor/modulename/.git2" "vendor/modulename/.git"
3) 在你的存储库中更改一个文件,最后:
git commit -a -m "Commit msg"
git push
.git
目录似乎是唯一有效的解决方案。如果Git允许将.git/
添加到.gitignore
中以使嵌套仓库正常工作,那将会更加容易... - Rotareti看起来git会自动忽略根仓库子文件夹中的.git文件夹。
(master)[/tmp]
$ mkdir test_root
(master)[/tmp]
$ git init test_root
Initialized empty Git repository in /tmp/test_root/.git/
(master)[/tmp]
$ cd test
test/ test_root/
(master)[/tmp]
$ cd test_root/
(master)[/tmp/test_root] (master)
$ ls
(master)[/tmp/test_root] (master)
$ git init test_child
Initialized empty Git repository in /tmp/test_root/test_child/.git/
(master)[/tmp/test_root] (master)
$ git status
On branch master
Initial commit
nothing to commit (create/copy files and use "git add" to track)
(master)[/tmp/test_root] (master)
$ touch test_root_file
(master)[/tmp/test_root] (master)
$ cd test_child/
(master)[/tmp/test_root/test_child] (master)
$ ls
(master)[/tmp/test_root/test_child] (master)
$ touch test_child_file
(master)[/tmp/test_root/test_child] (master)
$ cd ..
(master)[/tmp/test_root] (master)
$ git status
On branch master
Initial commit
Untracked files:
(use "git add <file>..." to include in what will be committed)
test_child/
test_root_file
nothing added to commit but untracked files present (use "git add" to track)
(master)[/tmp/test_root] (master)
$ git add test_child/test_child_file
(master)[/tmp/test_root] (master)
$ git status
On branch master
Initial commit
Changes to be committed:
(use "git rm --cached <file>..." to unstage)
new file: test_child/test_child_file
Untracked files:
(use "git add <file>..." to include in what will be committed)
test_root_file
(master)[/tmp/test_root] (master)
$ cd test_child/
(master)[/tmp/test_root/test_child] (master)
$ git status
On branch master
Initial commit
Untracked files:
(use "git add <file>..." to include in what will be committed)
test_child_file
nothing added to commit but untracked files present (use "git add" to track)
(master)[/tmp/test_root/test_child] (master)
$ git --version
git version 1.9.1
$ git add test_root_file
(master)[/tmp/test_root] (master)
$ git status
On branch master
Initial commit
Changes to be committed:
(use "git rm --cached <file>..." to unstage)
new file: test_child/test_child_file
new file: test_root_file
(master)[/tmp/test_root] (master)
$ git commit -m'1 commit'
[master (root-commit) 4d4b695] 1 commit
2 files changed, 0 insertions(+), 0 deletions(-)
create mode 100644 test_child/test_child_file
create mode 100644 test_root_file
(master)[/tmp/test_root] (master)
$ git show
commit 4d4b69589bf4f471c3c784f95f447d2a40ee6d7d
Author: Evgenii Shchemelev
Date: Wed Jan 6 09:20:03 2016 +0200
1 commit
diff --git a/test_child/test_child_file b/test_child/test_child_file
new file mode 100644
index 0000000..e69de29
diff --git a/test_root_file b/test_root_file
new file mode 100644
index 0000000..e69de29
.git
子目录包含已填充的git存储库的信息时,请尝试运行您的测试。 - user151841对我来说,这看起来像是一个设计错误:
如果您从同一存储库中加载第二个项目作为依赖项,则应将此项目移动到另一个存储库中。
并且在 vendor 目录下放置另一个 .gitignore 文件。
# Ignore Git here
.git
# But not these files...
!.gitignore
git 2.19.2
。当我在父仓库中运行 git add -A
时,Git 仍然会抱怨子仓库的问题。 - Rotareti如果你想完全忽略嵌套子模块,你可以这样做(以下我将假设你的嵌套仓库名为 "vendor/")
vendor/
目录下将 .git
文件夹重命名为 .git_
.gitignore
文件中添加:/vendor/.git_/
/vendor/.git_/
将被忽略,你可以添加和提交其他嵌套的文件和文件夹。vendor/
目录下将 .git_
重命名为 .git
.gitignore
文件中将 /vendor/.git_/
改为 /vendor/.git/
现在 /vendor/.git/
被忽略了,所有其他的 vendor/
文件和文件夹都已经提交了。
我已经做过几次了,我认为可能有更简单的方法,但我已经忘记了。不管怎样,这个方法是可行的。
对我来说,最简单的解决方案是使用Composer钩子。
将您的软件包视为子模块是因为存在.git
文件夹,您只需要将其删除即可。
composer.json
"scripts": [
"post-autoload-dump": [
"rm -rf vendor/webdriver/.git"
]
]
vendor
文件夹应该被忽略,而不仅仅是.git
子目录。使用的软件包存储在composer.json
和composer.lock
中,这是您要检入版本控制的内容。composer.json
...{
"repositories": [
{
"url": "https://github.com/l3pp4rd/DoctrineExtensions.git",
"type": "git"
}
],
"require": {
"gedmo/doctrine-extensions": "~2.3"
}
}
您可以创建一个名为packages
的目录,并将其添加到自动加载路径中。这样,您就可以使用单个Git存储库来管理所有代码(如果您想使用两个存储库,则可以使用Git子模块)。
在composer.json
中添加...
"autoload": {
"classmap": ["packages"]
}
如果不想将子文件夹视为子模块,请删除.git
文件夹。这样,您就不会在网站中看到带有@number
的文件夹。如果您想要更新子模块,可以创建一个类似以下Shell
脚本的脚本:
update.sh
git clone <url> <subfolder that you won't treat it as a module>
rm -rf <subfolder>/.git/
git add <subfolder>
git commit -m "Updated module"
update.bat
git clone <url> <subfolder>
rmdir /s /q <subfolder>\.git
git add <subfolder>
git commit -m "Updated module"
我相信这是GitLab中避免@number
的最佳方法。
您可以参考我为回答这个问题创建的GitHub存储库。
.gitignore
文件中只忽略.git
文件怎么样? - Anand S