Git忽略.git文件夹

Posted

技术标签:

【中文标题】Git忽略.git文件夹【英文标题】:Git ignore .git folder 【发布时间】:2016-04-09 16:57:33 【问题描述】:

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

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

供应商/.git

供应商/**/.git/

谷歌搜索

堆栈溢出搜索


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

【问题讨论】:

***.com/questions/3240881/… 那太好了,除了我没有使用任何包作为子模块。事实上,这正是我特别想避免的。 .git in .gitignore 怎么样 这可能有效,但它会忽略每个 .git 目录。我只想忽略一个特定目录的 .git。 *.git 应该可以工作! 【参考方案1】:

你可以使用 git hooks 来实现你想要的。开箱即用,您可以使用 pre-commit 挂钩重命名包含项目的 .git 目录,例如。到“.git2”,添加后一个项目中除“.git2”目录外的所有文件,全部提交,推送,最后使用post-commit hook将“.git2”文件夹重命名为“ .git”在你的模块中。

1) 在 root 存储库的 .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

【讨论】:

谢谢!重命名 .git 目录似乎是唯一有效的解决方案。如果 git 允许将 .git/ 添加到 .gitignore 以使嵌套 repos 工作,那就容易多了……【参考方案2】:

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

【讨论】:

嗯。当我正常提交和推送时,子项目作为参考显示在 repo 中,而其他包(来自 packagist)显示为文件。但是,如果我在提交之前从我的子项目中手动删除 .git 目录,那么所有文件都会按预期显示。也许有比试图忽略 .git 目录更好的方法? 实际上,忽略 git 文件夹的想法看起来是错误的。肯定应该是其他方式。我已经尝试按照您描述的条件提交 - 一切正常。提交中没有引用。可能您的软件包实际上是文件系统中的软链接? @YevgeniyShchemelev 在 .git 子目录包含已填充 git 存储库的信息时尝试您的测试。【参考方案3】:

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

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

并且供应商目录中放置另一个.gitignore文件

# Ignore Git here
.git

# But not these files...
!.gitignore

【讨论】:

这不适用于我机器上的git 2.19.2。当我在父仓库中运行 git add -A 时,Git 仍然抱怨子仓库。【参考方案4】:

应该忽略整个 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,而他们试图实现的目标表明存在设计错误。【参考方案5】:

要不将子文件夹视为子模块,请删除 .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 repository。

【讨论】:

以上是关于Git忽略.git文件夹的主要内容,如果未能解决你的问题,请参考以下文章

git merge 忽略文件

git怎么把忽略文件变成普通文件

git配置忽略文件(已提交文件和目录的忽略)

git添加忽略文件

git 提交怎么忽略编译文件

git-忽略已提交的文件或目录