Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ignore .git folder in sub folder

Tags:

git

Is it possible to add a sub folder with a .git folder to a repo, without Git treating it like a submodule? I've tried different methods to ignore that .git folder, but nothing this far has worked.

I have tried in /.gitignore:

/vendor/**/.git/

..and in /vendor/.gitignore:

.git/

The .git folder I want to ignore is in /vendor/foo/bar/.

like image 617
Znarkus Avatar asked Mar 27 '14 10:03

Znarkus


2 Answers

You can do this by directly adding some (any) internal content first. Git detects submodules by encountering the .git entry when searching a newly-encountered directory, but if you give it an actual path to look for inside that directory it doesn't search.

So

git add path/to/some/submodule/file     # bypass initial search that detects .git
git add path/to/some/submodule          # now git's index has it as ordinary dir
like image 57
jthill Avatar answered Oct 24 '22 12:10

jthill


You can use git hooks to achieve what you want. Thinking out of the box, you could create a pre-commit hook that renames 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/foo/bar/.git" "vendor/foo/bar/.git2"

git rm --cached vendor/foo/bar
git add vendor/foo/bar/*
git reset vendor/foo/bar/.git2

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

#!/bin/sh
mv "vendor/foo/bar/.git2" "vendor/foo/bar/.git"

3) Change a file in your repo and finally:

git commit -a -m "Commit msg"
git push

My Original answer

like image 35
Jannes Botis Avatar answered Oct 24 '22 10:10

Jannes Botis