Git ignore .git folder

bluemoonballoon picture bluemoonballoon · Jan 5, 2016 · Viewed 12.2k times · Source

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

Answer

Jannes Botis picture Jannes Botis · Feb 6, 2018

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