Andrew Schott
Andrew Schott

Reputation: 101

Personal git repo & .gitignore file

I am using a gitlab instance to sync data to several machines, including my home directory (minus a few things such as ssl keys). And one thing that I want to see how to properly deal with is how to ignore the git repo data on repos that I have in my ~/workspace/ folder and such. Git has a fit with committing them as it correctly notes that I cannot sync another repo.

Currently I am scripting this out as I have not gotten the .gitignore parameters hammered out, or its unsupported. Anyone have any better idea than just rm -rf path/to/problem/.git/ for each git repo I have pulled down? I have quite a few, and this is getting tiresome, but if this is the best bet, ok. I have this all scripted out along with the git add . && git commit -m ....... && git push.


I was able to quickly replicate the error on, admittedly a folder I care nothing about, but a sample nonetheless. I just created a repo for my wow addons, updated them and did not use my script. The following is the error:

hint: You've added another git repository inside your current repository.  
hint: Clones of the outer repository will not contain the contents of  
hint: the embedded repository and will not know how to obtain it.  
hint: If you meant to add a submodule, use:  
hint:  
hint:   git submodule add <url> EasyDisenchant  
hint:  
hint: If you added this path by mistake, you can remove it from the
hint: index with:  
hint: 
hint:   git rm --cached EasyDisenchant  
hint:  
hint: See "git help submodule" for more information.

And the lines that I have in my main gitignore are:

# Ignore git repo data folders
.git  
**/.git

Upvotes: 1

Views: 240

Answers (1)

Ortomala Lokni
Ortomala Lokni

Reputation: 62575

One way to solve your problem is to use submodules.

On each git repository inside the main repository, you can do:

cd /path/to/module1
git submodule add ssh://path.to.repo/module1
git commit -m "Added submodule module1"

You will then have a new .gitmodules file at the main repository level referencing all submodules. Each submodules will be stored independently in its own repository, they will also need to be pushed independantly. The references in the main repository will also need to be pushed.

To clone the main repository will all its submodules, you will have to do:

git submodule update --init 

Read 7.11 Git Tools - Submodules for details.

Another possible solution is to use git subtree. Read Git subtree: the alternative to Git submodule

== EDIT ==

According to the OP (Andrew Schott), Git subtrees are best adapted to the situation.

To add a subtree:

git subtree add --prefix path/to/new/subtree url.git master --squash

To update a subtree:

git subtree pull --prefix path/to/subtrree url.git master --squash

Upvotes: 1

Related Questions