Georgi Peev
Georgi Peev

Reputation: 1109

Moved a GitHub Repository to Organization ,but with specific requirements

I have a GitHub repository ,which consists of 3 folders and have 1500 commits:

A repository:
Folder I
Folder II
Folder III

The thing that I want to do is to move the repository to a GitHub organization,but I want in the organization to have 3 repositories for each folder:

A organization:
Repository I == Folder I
Repository II == Folder II
Repository III == Folder III

But the specific part here is ,I want each of the repositories to keep the commits ,that were for the specific folder like so:

A organization:
Repository I -> 300 commits ,which were related to Folder I
Repository II -> 500 commits ,which were related to Folder II

Because when I do the migration I don't want to lose the commits ,which were made for each of the folders.Is that possible and what are the ways to do it?

Upvotes: 2

Views: 1603

Answers (1)

ffflabs
ffflabs

Reputation: 17481

Let's say your file structure is

- original_repo
    └- .git
    └- folder_I
    |    └- library_1.js
    |
    └- folder_II
    |    └- library_2.js
    |
    └- folder_III
    |    └- library_3.js
    |
    └- .gitignore
    └- .eslintrc
    └- index.js
    └- package.jon

1. Clone into new local copies

Each folder will contain your detached repos at the end of the procedure.

git clone [email protected]:georgi/original_repo subfolder_one
git clone [email protected]:georgi/original_repo subfolder_two
git clone [email protected]:georgi/original_repo subfolder_three

For each of them create a repo in your organization. On each folder, point the remote to the organization repo.

cd subfolder_one
git remote set-url origin [email protected]:organization/subfolder_one

cd ../subfolder_two
git remote set-url origin [email protected]:organization/subfolder_two


cd ../subfolder_three
git remote set-url origin [email protected]:organization/subfolder_three

Now each is a separate repo so you don't break the original one, mind you.

2. make the subfolder the new root

Now in each subfolder, rewrite the history so that only include commits that have made modifications to said subfolder. This will rewrite your original sha

Just to compare, let's see how many commits you have in your original repo

cd ../subfolder_one
git log --oneline | wc -l
# it print let's say, 500, so you have 500 commits in your original repo

Let's make the subfolder the new root folder of this new repo

git filter-branch --prune-empty --subdirectory-filter folder_I

Now your local content has only the contents of the former folder_I (plus a .git folder )

- subfolder_one
  └- .git
  └- library_1.js

let's see how many commits we have now (as I said, the sha is different, but they kept its date, author and comments)

git log --oneline | wc -l
# it print let's say, 100, just the commits that ever touched the subfolder

You can now push this repo to the new origin. You may have to force push. Just make sure you're pointing to the organization origin so you don't break the original repo.

3. Figure out how to keep your project from then on

Your original project still need the subfolders, or so I guess. It's up to you to figure out the way, but it will involve removing the now detached subfolders and include them either as submodules or dependencies (you can use github dependencies in npm/yarn, composer and most package managers)

Upvotes: 1

Related Questions