I have a unique requirement for which I am searching for a solution... I did look at a lot of other solutions online and didn't find one that would suit my expectations...
There are two repos - X and Y (illustration below). I want to delete Repo X forever. But there is a folder, say A, that I want to carry to the second repo (Y) with all the Git Commit Histories too… It is okay to tamper with history and I want repo Y's history to claim that the folder A existed in the repo Y from the beginning and all changes to that folder A was made when it was inside repo Y. I do not want Repo Y's history to show that the folder A came after a merge from another branch/repo. I also do not want to rebase as all the commits (~1000) for the folder A will be dumped on top of the existing commits of Repo Y.
***** Original Commits on the timeline for Repo X and Repo Y *****
Repo Y: -------Cx--------Cy--------Cz----------
Repo X: -----------Ca----------Cb------Cc------ (only commits that touch Folder A)
I found a command to filter-branches and rewrite the commits of the repo X such that according to git only the content of folder A existed.
git filter-branch --subdirectory-filter ./A -- --all
And then used the following script to move all the content of the "Folder A" in to a folder named "A", in Repo X
# https://gist.github.com/xkr47/f766f4082112c086af63ef8d378c4304
# placed the script above in PATH and executed the following command at Repo X
git filter-mv 's!^!A/!'
So now I have the repo Y, which is the repo to which I need to move folder A from repo X with all the Git commit histories… I also have the modified repo X, where the only content is folder A
Please note that there is no possibility of a conflicting commits as all the commits on the repo X are done to a folder (A), that does not exist in repo Y at all
Now, I will be able to mark the local Repo X as a remote to my Repo Y and either Merge or rebase
# repo X lives in the same directory as repo Y
# commands run inside repo Y
git remote add repoX ../X
git pull repoX <branch> --allow-unrelated-histories
# or
git pull repoX <branch> --rebase
MERGE
***** MERGE's output *****
Repo Y: -------Cx--------Cy-------Cz-----(MERGE)----
/
/
-----Ca---------Cb------Cc (Folder A commits)
The commits’ timeline will be maintained, but will be on different tracks till both are merged, and I will not be able to see the contents from both the repos together at any point before the merge commit as they will be on separate commit lines (I want folder A, with all the changes it has undergone, at repo Y and it’s commit lines) (… so that I can go back to any point in Repo Y and see the evolution of both Repo Y and Folder A together, according to the time of the commit)
REBASE
***** REBASE's output *****
Repo Y: -------Cx--------Cy--------Cz------Ca-----Cb-----Cc----
But I do not want the commits from Repo X to be after the latest commits of Repo Y… In my use case, Repo X will have ~1000 commits and rebasing them all at once will spoil the commit timeline of Repo Y. I want them to be fused within the commits of Repo Y according to the time in which the commit had happened
EXPECTED
***** Expected Output *****
Repo Y: -------Cx--Ca----Cy----Cb--Cz--Cc------ (Repo Y commits fused with commits from repo X - only adds a folder (A) to repo Y)
I know that meddling with Git Histories is not a good thing. But for my use case, this is probably the best thing I can do…