18

I have a php project that uses composer for package management. One of the packages is another project belonging to the same repo. I have a need to commit my entire vendor folder, but I want to ignore the .git folder in the sub-project so that it doesn't get treated like a submodule.

So far I have had no success. Things I've already tried:

vendor/.git

vendor/**/.git/

google search

stack overflow search


Here's what the sub-project folder looks like in GitLab. Instead of the files, it's just some kind of reference.

enter image description here

bluemoonballoon
  • 299
  • 1
  • 2
  • 8

7 Answers7

4

You can use git hooks to achieve what you want. Thinking out of the box, you could use pre-commit hook to rename the .git directory of your included project, eg. to ".git2", add all files in the latter project except the ".git2" directory, commit all, push it and finally use post-commit hook to rename ".git2" folder back to ".git" in your module.

1) Create pre-commit file under .git/hooks/ of your root repo with contents:

#!/bin/sh
mv "vendor/modulename/.git" "vendor/modulename/.git2"

git rm --cached vendor/modulename
git add vendor/modulename/*
git reset vendor/modulename/.git2

2) Create post-commit file under .git/hooks/ also with contents:

#!/bin/sh
mv "vendor/modulename/.git2" "vendor/modulename/.git"

3) Change a file in your repo and finally:

git commit -a -m "Commit msg"
git push
Jannes Botis
  • 11,154
  • 3
  • 21
  • 39
  • 1
    Thanks for this! Renaming the `.git` dir seems to be the only valid solution. It would be so much easier if git allowed adding `.git/` to `.gitignore` to make nested repos work... – Rotareti Dec 10 '18 at 14:29
2

It looks like git automatically ignores .git folders in subfolders of root repository.

(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
Yevgeniy Shchemelev
  • 3,601
  • 2
  • 32
  • 39
  • Hmm. When I commit and push as normal, the sub-project shows up in the repo as a reference, while other packages (from packagist) show up as files. However if I manually delete the .git dir from my sub-project before committing, then all the files show up like expected. Maybe there's a better way than trying to ignore the .git dir? – bluemoonballoon Jan 05 '16 at 19:47
  • 1
    Actually, idea of ignoring git folder looks wrong. Should be other way definitely. I've tried commit with conditions you described - everything working right. There is no references in commit. May be your packages actually are soft links in your file system? – Yevgeniy Shchemelev Jan 06 '16 at 07:29
  • @YevgeniyShchemelev try your test when the `.git` subdirectory houses information for a populated git repo. – user151841 Jan 30 '18 at 18:47
2

For me this looks like a design error:

If you load a second project from the same repository as dependency, this project should be moved to a different repository.

And inside the vendor Directory place another .gitignore file with

# Ignore Git here
.git

# But not these files...
!.gitignore
Wolfgang Blessen
  • 900
  • 10
  • 29
  • 1
    This doesn't work with `git 2.19.2` on my machine. Git still complains about subrepos, when I run `git add -A` in the parent repo. – Rotareti Dec 10 '18 at 14:32
0

The entire vendor folder should be ignored, not just the .git sub-directories. Which packages are used are stored in composer.json and composer.lock which are what you check into version control.

See: https://getcomposer.org/doc/faqs/should-i-commit-the-dependencies-in-my-vendor-directory.md

If you want to create a reusable package as part of your project, you have two options:

A) Use Composer to handle another repo

Add to composer.json...

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

B) Use a 2nd packages directory

You can create a packages directory and add it to your autoload paths. This will let you use a single Git repo for all the code (if you want to use two repos, you can use a Git submodule)

Add to composer.json...

"autoload": {
    "classmap": ["packages"]
}
Erik Berkun-Drevnig
  • 2,306
  • 23
  • 38
  • This is a valid answer. OP stated they are using PHP, and what they are trying to achieve indicates a design error. – Jamie Feb 09 '19 at 16:23
0

For me, the most simple solution is using composer hooks. Your package treated as submodule is because there is exist .git folder, then you just need to delete it.

composer.json

"scripts": [
    "post-autoload-dump":  [
        "rm -rf vendor/webdriver/.git"
    ]
]
nmfzone
  • 2,755
  • 1
  • 19
  • 32
0

If you want to ignore nested submodule totally, what you can do (below I will assume your nested repo is called "vendor/")

  1. inside vendor/ rename .git folder to .git_
  2. in parent .gitignore file add: /vendor/.git_/
  3. /vendor/.git_/ will be ignored and you can add and commit other nested files and folders.
  4. in vendor/ rename .git_ to .git
  5. in parent .gitignore change /vendor/.git_/ to /vendor/.git/

Now the /vendor/.git/ is ignored and all other vendor/ files and folders are committed.

I have done this several times and I think there may be easier way to do it but I have forgotten how. Anyway this works.

Orhan
  • 1,395
  • 13
  • 12
-2

To not treat sub folder as a submodule, delete the .git folder. So that you won't see a folder with @number in the website. If you want to update the submodule. You can create a script like the following written in 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"

I believe this is the best way to avoid @number in GitLab

You can refer my GitHub repository created for answering this question.

TravorLZH
  • 302
  • 1
  • 9