Sonu Mishra
Sonu Mishra

Reputation: 1779

How to import a git repository as a subdirectory of another git repository without losing any commit?

I have two GitHub repositories TinyDroids and Miwok

TinyDroids has a subdirectory JustJava and a README.md. This has about 10 commits in the log. Miwok has about 12 commits in the log.

How can I copy the entire Miwok repository as a subdirectory of TinyDroids repository?

The resultant TinyDroids repository must have two subdirectores: JustJava and Miwok. Also, it must have its original 10 commits + 12 commits from Miwok.

What I tried so far:

git clone https://github.com/username/TinyDroids.git
cd TinyDroids
git clone https://github.com/username/Miwok.git

If I commit and push after this, the entire Miwok subirectory is shown as a single commit over the 10 commits.

I also tried:

git clone https://github.com/username/TinyDroids.git
cd TinyDroids
git pull --rebase https://github.com/username/Miwok.git

This gives some issues in rebasing that I do not understand. git log after this only show the 12 commits of Miwok.

Upvotes: 2

Views: 1837

Answers (1)

Code-Apprentice
Code-Apprentice

Reputation: 83577

I suggest creating a branch and merging. You also might need to move the Miwok directory to the desired subdirectory. This is a rough sketch of what to do:

$ git clone https://github.com/username/TinyDroids.git
$ cd TinyDroids

# Create an orphan branch to pull the Miwok repo to
$ git checkout --orphan miwok
$ git pull https://github.com/username/Miwok.git

# Move the Miwok repo into a subdir
$ mkdir Miwok
$ git mv . Miwok
$ git commit -m "Move Miwok repo to Miwok subdir"

# Merge Miwok into TinyDroids
$ git checkout master
$ git merge miwok

The command git mv . Miwok won't quite work right. The idea is to move everything to the Miwok subdirectory. You might need to do a few more commands to do this more manually.

p.s. Since these are Android Studio projects, you will need to do some housecleaning if you want them to be modules in a single project instead of two separate projects.

Upvotes: 2

Related Questions