Git忽略.git文件夹。

18

我有一个使用composer进行包管理的php项目,其中一个包是属于同一仓库的另一个项目。我需要提交整个vendor文件夹,但我想忽略子项目中的.git文件夹,以免它被视为子模块。

到目前为止,我还没有成功过。我已经尝试过:

vendor/.git

vendor/**/.git/

谷歌搜索

堆栈溢出搜索


这里是在GitLab中子项目文件夹的样子。它只是某种引用,而不是文件。

enter image description here


https://dev59.com/yHA75IYBdhLWcg3wipmH - Yevgeniy Shchemelev
1
那本来很好,但我没有使用任何包作为子模块。事实上,这正是我特意要避免的。 - bluemoonballoon
.gitignore 文件中只忽略 .git 文件怎么样? - Anand S
这可能有效,但它会忽略每个 .git 目录。我想只忽略一个特定目录下的 .git。 - bluemoonballoon
所以你有两个 Git 存储库,第二个是第一个的子项目? - tomrlh
显示剩余3条评论
7个回答

4
您可以使用git钩子来实现您想要的功能。可以尝试使用pre-commit钩子重命名您所包含项目的.git目录,例如将其更名为“.git2”,添加后面项目中除“.git2”目录之外的所有文件,提交所有更改并推送,最后使用post-commit钩子将“.git2”目录重命名回“ .git”。
1)在您的根仓库的.git/hooks/目录下创建pre-commit文件,并添加以下内容:
#!/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

1
谢谢!重命名.git目录似乎是唯一有效的解决方案。如果Git允许将.git/添加到.gitignore中以使嵌套仓库正常工作,那将会更加容易... - Rotareti

3

看起来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

当我像平常一样提交和推送时,子项目会显示为引用,而其他包(来自packagist)则显示为文件。但是,如果在提交之前手动删除子项目中的.git目录,则所有文件都会像预期的那样显示出来。也许有比尝试忽略.git目录更好的方法? - bluemoonballoon
1
实际上,忽略git文件夹的想法似乎是错误的。肯定应该有其他方法。我已经尝试了您描述的条件提交 - 一切都正常工作。提交中没有引用。也许您的软件包实际上是文件系统中的软链接? - Yevgeniy Shchemelev
@YevgeniyShchemelev 当.git子目录包含已填充的git存储库的信息时,请尝试运行您的测试。 - user151841

2

对我来说,这看起来像是一个设计错误:

如果您从同一存储库中加载第二个项目作为依赖项,则应将此项目移动到另一个存储库中。

并且在 vendor 目录下放置另一个 .gitignore 文件。

# Ignore Git here
.git

# But not these files...
!.gitignore

1
这在我的机器上无法使用 git 2.19.2。当我在父仓库中运行 git add -A 时,Git 仍然会抱怨子仓库的问题。 - Rotareti

0

如果你想完全忽略嵌套子模块,你可以这样做(以下我将假设你的嵌套仓库名为 "vendor/")

  1. vendor/ 目录下将 .git 文件夹重命名为 .git_
  2. 在父级目录下的 .gitignore 文件中添加:/vendor/.git_/
  3. /vendor/.git_/ 将被忽略,你可以添加和提交其他嵌套的文件和文件夹。
  4. vendor/ 目录下将 .git_ 重命名为 .git
  5. 在父级目录下的 .gitignore 文件中将 /vendor/.git_/ 改为 /vendor/.git/

现在 /vendor/.git/ 被忽略了,所有其他的 vendor/ 文件和文件夹都已经提交了。

我已经做过几次了,我认为可能有更简单的方法,但我已经忘记了。不管怎样,这个方法是可行的。


0

对我来说,最简单的解决方案是使用Composer钩子。 将您的软件包视为子模块是因为存在.git文件夹,您只需要将其删除即可。

composer.json

"scripts": [
    "post-autoload-dump":  [
        "rm -rf vendor/webdriver/.git"
    ]
]

0
整个vendor文件夹应该被忽略,而不仅仅是.git子目录。使用的软件包存储在composer.jsoncomposer.lock中,这是您要检入版本控制的内容。
请参见:https://getcomposer.org/doc/faqs/should-i-commit-the-dependencies-in-my-vendor-directory.md 如果您想创建一个可重用的软件包作为项目的一部分,您有两个选择:

A)使用Composer处理另一个repo

添加到composer.json...
{
    "repositories": [
        {
            "url": "https://github.com/l3pp4rd/DoctrineExtensions.git",
            "type": "git"
        }
    ],
    "require": {
        "gedmo/doctrine-extensions": "~2.3"
    }
}

B) 使用第二个包目录

您可以创建一个名为packages的目录,并将其添加到自动加载路径中。这样,您就可以使用单个Git存储库来管理所有代码(如果您想使用两个存储库,则可以使用Git子模块)。

composer.json中添加...

"autoload": {
    "classmap": ["packages"]
}

这是一个有效的答案。OP表示他们正在使用PHP,而他们试图实现的内容表明存在设计错误。 - Jamie

-2

如果不想将子文件夹视为子模块,请删除.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存储库


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接