For example, given the worktree below, if I init git repo in folder my_folder
and type git add .
, I do not want the folders django-backend
and test-vue
added or tracked by Git.
my_folder/
audio/
Michael Jackson.mp3
images/
nature.jpg
projects/
projects_info.md
django-backend/
.git/
manage.py
poetry.lock
test-vue/
.git/
yarn.lock
I tried this but didn't get the desired effect:
.git/**/*
CodePudding user response:
Consider using Git submodules for when using multiple repositories within the super project.
Submodules are a bit trickier to use in the beginning in comparison with a single repository, however it pays off as they will allow you to have fine grained control over each individual repository's history (.git
).
In the example of your question, the django-backend
and test-vue
repositories could be submodules added to the my_folder
's repo.
git init
git submodule add [remote to django-backend] django-backend
git submodule add [remote to test-vue] test-vue
git submodule init
git submodule update
Adding submodules to the super project will create file named .gitmodules
.
Each submodule will have its own history. For example, each submodule's latest commit can be obtained like this:
cd submodule-dir
git pull
cd ..
And then commit your work, with the submodule sync'ed:
git commit -am "Synchronized with submodules"
Later on, when cloning the super project repository, it is necessary to use git clone --recurse-submodules [url/to/super/project]
to bring it back with all its submodules.
In general, that's it. There are many good answers on Stack Overflow that details the manageability aspects of using git repository containing submodules.
CodePudding user response:
If you only wanted to ignore any .git
folder anywhere in the worktree, it would be easy:
**/.git/
But it looks like you want to ignore the parent folder of any .git
folder anywhere in the worktree. There is no single line you can add to .gitignore
that will do this for you. You either have to manually do it, e.g.
my_folder/projects/django-backend/
or create a Git pre-commit
hook that calls a script that detects new folders that should be ignored and adds them to .gitignore
and adds the .gitignore
change to the commit. This apparently only work for some versions of git. If you have any trouble see the other answer and comments for that SO question.
If it turns out your version of git's pre-commit
hook does not include the git add
in the commit, you can instead simply have the script abort the commit with a warning message. Since the directory will have been added to .gitignore
by the script, you can just commit a second time and it will work.
Here are some SO answers that you can use for more guidance with pre-commit
hooks: