3

I would like to move a folder from git to another git repository.

For example:

This is my current folder structure in Git.

src/Project/RegistrationBundle
src/Project/PictureGenerationBundle
src/Project/CloudDriveBundle
readme.md

I would like to move it to a new repo with the following folder structure but I want to keep the git history from the previous repository:

src/RegistrationBundle
readme.md

I have tried to run it with git filter-branch

git filter-branch --subdirectory-filter src/Project/RegistrationBundle/

However, I cannot find a solution to customize the folder structure.

Please let me know if it is achievable.

Thanks a lot.

Maximillian Laumeister
  • 19,884
  • 8
  • 59
  • 78
Ralph
  • 2,065
  • 3
  • 15
  • 23
  • 1
    Related: https://stackoverflow.com/questions/41811986/git-move-directory-to-another-repository-while-keeping-the-history – Maximillian Laumeister Sep 24 '18 at 15:40
  • 1
    Do you want the folder "src/RegistrationBundle" to remain a part of old repo? If yes, do you want a single change in any file of src/RegistrationBundle to be reflected on both repos ? – Rishabh Agarwal Sep 24 '18 at 15:42
  • 1
    I don't have a complete answer to your question, but it's also worth mentioning that setting the repository move aside, git is [not great at tracking file renames and directory structure changes within a repo](https://stackoverflow.com/questions/2314652/is-it-possible-to-move-rename-files-in-git-and-maintain-their-history). You may have some luck if you first commit the directory structure change as its own commit in Repo A, then do the migration to Repo B with filter-branch afterwards. – Maximillian Laumeister Sep 24 '18 at 15:49

2 Answers2

0

You could change your repo architecture on the current local repository (removing the 'Project' folder), commit the change, and then change the remote git repository.

Use git remote set-url origin <the new git url> to change the repository url.

The history will be the same one since the local history in the .git folder didn't changed at all.

Kapcash
  • 6,377
  • 2
  • 18
  • 40
0
git filter-branch --subdirectory-filter src/Project/RegistrationBundle/ -- --all

when you run this command all you have is the files inside the RegistrationBundle, and then in the new repository add a remote with the path to the RegistrationBundle and then do a git pull

Jithu17
  • 1
  • 1
  • Your answer came up in review. My comment is a suggestion to improve your answer. Please click "edit" above and add the explanation you've provided. Don't explain it in the comments, since comments can be deleted at any time. – CJ Dennis Aug 28 '19 at 05:40
  • this command has a warning: WARNING: git-filter-branch has a glut of gotchas generating mangled history rewrites. Hit Ctrl-C before proceeding to abort, then use an alternative filtering tool such as 'git filter-repo' (https://github.com/newren/git-filter-repo/) instead. – AlignedDev Dec 01 '20 at 14:20