Reputation: 34884
I need to add a folder in a git repository and that folder is located out of the main (root) folder. I want git to treat it as the other normal folders in the repo: like if it was located in the main folder.
Of course, I can just copy it there and will have to do that over and over again. But I'd like to refer to it somehow, find a more elegant solution.
Is there any way to do that?
Upvotes: 1
Views: 484
Reputation: 5828
It's not possible to add folders that exist outside of the git repository root folder into the repository.
One way you could do something equivalent to this would be to make this folder a git repo in it's own right and push it to a remote repo. Then submodule that repo inside your existing git repository.
So lets say you have two folders:
foo/
..some code..
bar/
.git/
..some code etc..
And you want to make sure foo is a member of the bar repo but you can't move it inside. We could make foo a repo
git init foo
cd foo
git remote add origin my-external-host/foo.git
git add .
git commit -m "initial import"
git push -u origin master
We could then add foo as a submodule to bar
cd bar
git submodule add my-external-host/foo.git
git submodule init
git submodule update
git add .
git commit -m "importing foo as submodule"
git push
Now we would have:
foo/
.git/
..some code..
bar/
.git/
foo -> submodule to my-external-host/foo.git
..some code etc..
The downside of this would mean if you make changes in the foo folder you need to git commit
them and then do a git pull
on the bar/foo submodule to get those updates in the bar repo.
A better solution would be to reconsider why you want to do this. Git isn't designed to do what you are looking for and it wouldn't be easy for you to try to bend git to do this.
Upvotes: 1