Home > Mobile >  Split a subdirectory to its own repository using git filter-repo
Split a subdirectory to its own repository using git filter-repo

Time:11-16

I am following this: https://docs.github.com/en/get-started/using-git/splitting-a-subfolder-out-into-a-new-repository

I have a repository called my-repo

The structure of the directories below it is as:

MyProject
   |
   ------ UI
   |
   ------ DB
   |
   ------ SERVER
   |
   ------ ANDROID

I want to split ANDROID directory above into its own new repository called my-repo-android

I did the following for it:

  1. Cloned the repository

    git clone [email protected]:myproject/my-repo.git
    
  2. Go into that directory:

    cd my-repo
    
  3. Remove origin to avoid any push by mistake

    git remote rm origin
    
  4. Run filter repo command

    git filter-repo --path MyProject/ANDROID --path-rename ANDROID/:
    

The command runs successfully. However, instead of getting the root directory as ANDROID, I still see the directory as MyProject/ANDROID with everything else removed.

What am I doing wrong?

How to use filter-repo path-rename option correctly so that the project structure changes from MyProject\ANDROID to directly ANDROID directory at the root ?

Edit:

The following command as per comments and answer below worked:

git filter-repo --path MyProject/ANDROID/ --path-rename MyProject/ANDROID/:ANDROID/

After this, I created a new repository remotely and ran the following commands:

  1. Add remote origin

    git remote add origin [email protected]:mypronect/androidRepo.git
    
  2. Master branch

    git branch -M master
    
  3. Pull first

    git pull origin master --allow-unrelated-histories
    
  4. Push

    git push -u origin master
    

Now I can see ANDROID directory correctly on the new androidRepo on bitbucket.

And when I open any file, I can see the history as well. Thus filter-repo has worked as expected.

However, I only see master branch. I do not see any of the other historical branches and tags. (We tag/create a separate branch for every release we do).

What am I missing? How can I preserve all branches and tags related to ANDROID as well ?

I tried the following command, but it didn't work:

 git push --all origin

CodePudding user response:

You forgot MyProject before ANDROID in the --path-rename option. With

  1. --path MyProject/ANDROID you are telling filter-repo to keep only the path MyProject/ANDROID folder.
  2. --path-rename ANDROID/: you are converting the ANDROID folder to the root folder basically.

My guess is that you filter-repo is not finding ANDROID folder, because it is inside MyProject, and therefore it is not able to rename it.

Final command might be:

git filter-repo --path MyProject/ANDROID/ --path-rename MyProject/ANDROID/:

Or, the corresponding shortcut:

git filter-repo --subdirectory-filter=MyProject/ANDROID

EDIT: I thought you wanted to make the content of the ANDROID folder as the root, instead it seems you want to move the ANDROID folder from ./MyProject to ./. In this case @LeGEC gets the point with --path-rename MyProject/ANDROID:ANDROID.

CodePudding user response:

For the updated part of my question, please see below answer:

Push local Git repo to new remote including all branches and tags

  • Related