Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git: move repository to a subfolder of another repository

Tags:

I have two separate git repositories(in BitBucket), Repo A and Repo B. I would like to move Repo B into a subfolder of Repo A. I also need ensure that the commit history etc. in Repo B is preserved and not lost.

How can I achieve this with git?

like image 357
grizzthedj Avatar asked Nov 29 '17 18:11

grizzthedj


People also ask

How do I move a repository from one folder to another?

Merge the files into the new repository B. Step 2: Go to that directory. Step 3: Create a remote connection to repository A as a branch in repository B. Step 4: Pull files and history from this branch (containing only the directory you want to move) into repository B.


2 Answers

To merge a Repo B into Repo A as a subfolder, run this command inside Repo A;

git subtree add -P <prefix> <repo> <rev>

Set <prefix> to the name of the subdirectory, <repo> to the clone URL of Repo B, and <rev> to the revision of Repo B you want (HEAD if latest)

This will take the history of Repo B and merge it with Repo A, along with an additional merge commit.

like image 171
Edmund Dipple Avatar answered Oct 25 '22 00:10

Edmund Dipple


It isn't done in BitBucket, it's done using git. You need the following steps:

  • Fetch repo B into a cloned repo A. Git might warn you about this, but still do it.
  • Checkout that fetched code (you will have only code from repo B at that moment) and move it all into a subfolder you want. Commit that.
  • Checkout your repo A code again and merge the commit you have just created.
like image 44
aragaer Avatar answered Oct 25 '22 00:10

aragaer