Dan D.
Dan D.

Reputation: 8557

Git clone remote repo into another remote repo

Cloning git repo from remote server to local computer is rather easily done the standard way, but I need to clone a remote repo into another remote repo, is it possible?

P.S. I come up with this problem because I just can't fork my own project on GitHub to create a child project, I can fork the projects of other users only. This is about cloning to make a whole child project from parent project, not just a submodule.

Upvotes: 2

Views: 8517

Answers (2)

vanekrus
vanekrus

Reputation: 31

The answer above shows only the way for cloning remote repo with one branch. If your source repo has more than one branches than you have to repeat this steps as much times as number of branches you have.

There is another a bit tricky but simplier way. I try to show it using the Git Bash and Git Tortoise.

Git Bash

mkdir testdir
cd testdir
# making repo clone without checking out to any branches
# there is a trick -- name of new directory allows us use all
#     features of bare repo, but still work with it in usual way
git clone --bare [email protected]:vanekrus/test_project.git .git
# adding destination where you want to copy your repo
git remote add origin_copy [email protected]:vanekrus/test_copy_project.git
# in simple words --mirror option makes possible to push all
#     git refs (tags, branches, etc.) to new repo without naming it
git push origin_copy --mirror
# cleaning workspace after work is done
cd ..
rm -rf testdir

Git Tortoise

Make new directory testdir and open it in file manager. Right-click in the file manager window and choose Git Clone. Check Clone into Bare Repo. Change directory name in field Directory to <your_path>\testdir\.git. Click OK.

Now you need to add another repo as new remote destination. In directory testdir Right-Click and choose TortoiseGit->Push.... After the window appears check Push all branches and click button Manage in Destination section. In the appeared settings window type some alias for new remote (e. g. origin_copy) and paste URL of your new remote (in my case it is [email protected]:vanekrus/test_copy_project.git). Click Add New/Save. The answer about tag fetching is up to you (choose Yes -- don't fetch tags from this repo, or No -- leave tag fetching enabled). Now in Destination section for Remote choose new remote (in my case origin_copy). Answer Yes for question: "Do you want push all local branches?". That's all.

Notices

Btw, I have to make two notices:

  1. If you need to copy whole repo to another place you should think about why you need it. It's not usual way.
  2. I understand that my answer is late for several years, but I hope that it might be useful for somebody.

Upvotes: 1

CodeWizard
CodeWizard

Reputation: 141946

Cloning git repo from remote server to local computer is rather easily done the standard way, but I need to clone a remote repo into another remote repo, is it possible?

In git you can't have .git folder inside another git folder

What can you do?

  • Clone the original repo
  • Add new remote git remote add <origin2> <url2>
  • Use the code from both the repositories inside a single repo

Now you can pull and merge the branches from the 2 remotes and have the code of both of them in a single repository.

As you can see in the image below you will have 2 repositories which builds up your big repository.

# clone first repository
git clone <repo1>

# add remote 
git remote add <remote2> <url2>

# display the list of all the remotes
git remote -v

Note: <remote2> can not be an existing remote name.


enter image description here

Upvotes: 6

Related Questions