Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git - Push subfolder to different repository

I have a big repository with many files and projects. One of the sub-folders in this repo has to be a new, separated repository.

I don't need to preserve history in the new one, but I have to be able, at any time, to update new repo with changes from the original repo.

The idea is that the old repo is a private, internal gitlab and the new repo is a public repo for deployed project.

How can I do this?

like image 699
zupazt3 Avatar asked Aug 08 '17 10:08

zupazt3


People also ask

Can I clone a subdirectory in git?

Cloning only a subdirectory is not possible in Git. The network protocol doesn't support it, the storage format doesn't support it. Every single answer to this question always clones the whole repository.


2 Answers

You can treat the new repo as subtree for the old repo, and update new repo with old repo by git subtree push. Detail steps as below:

Assume the old repo has folders project1, project2, project3 etc for different projects. And the project1 has managed in the new repo.

Frist, replace the project1 folder in old repo by the new repo.

# Copy project1 folder out of old repo
# Delete the project1 folder in old repo, then use the commands in old repo
git add .
git commit -m 'delete project1 folder in old repo'
git subtree add --prefix=project1 <URL for new repo> master

Now project1 folder in old repo is the subtree from new repo. You can make change and commit (git commit) in old repo.

When you need to update the new repo by the old repo, you can use:

git subtree push --prefix=project1 <URL for new repo> master

Now the new repo will be updated.

like image 186
Marina Liu Avatar answered Oct 03 '22 08:10

Marina Liu


If you only ever need to update the public repository from the private repository and never in the opposite direction, you can use:

C:\privateRepo> git subtree push --prefix=public ..\publicRepo publicBranch

This will copy all commits that access the subfolder public (and the copies will only contain information about files that are in that subfolder) and push those commits into the repository in C:\publicRepo at branch publicBranch.

like image 21
Petr Hudeček Avatar answered Oct 03 '22 07:10

Petr Hudeček