-
-
Save trongthanh/2779392 to your computer and use it in GitHub Desktop.
# source: http://st-on-it.blogspot.com/2010/01/how-to-move-folders-between-git.html | |
# First of all you need to have a clean clone of the source repository so we didn't screw the things up. | |
git clone git://server.com/my-repo1.git | |
# After that you need to do some preparations on the source repository, nuking all the entries except the folder you need to move. Use the following command | |
git filter-branch --subdirectory-filter your_dir -- -- all | |
# This will nuke all the other entries and their history, creating a clean git repository that contains only data and history from the directory you need. If you need to move several folders, you have to collect them in a single directory using the git mv command. | |
# You also might need to move all your content into some directory so it didn't conflict with the new repository when you merge it. Use commands like that | |
mkdir new_directory/ | |
git mv my_stuff new_directory/ | |
# Once you've done commit your changes, but don't push! | |
git commit -m "Collected the data I need to move" | |
# This is all about the source repository preparations. | |
# Now go to your destination repository | |
cd ../my-repo2/ | |
# And here is the trick. You need to connect your source repository as a remote using a local reference. | |
git remote add repo1 ../my-repo1/ | |
# After that simply fetch the remote source, create a branch and merge it with the destination repository in usual way | |
git fetch repo1 | |
git branch repo1 remotes/repo1/master | |
git merge repo1 --allow-unrelated-histories | |
# This is pretty much it, all your code and history were moved from one repository to another. All you need is to clean up a bit and push the changes to the server | |
git remote rm repo1 | |
git branch -d repo1 | |
git push origin master | |
# That's all. After that you can nuke the temporary source repository. |
Hey, works great! But is there a way to keep the tags? And maybe the branches too?
@KastenBrot Tags are assigned to specific commit hash. This solution will change all commit hashes although commit messages are still there. So I don't think you can keep tags.
Regarding keeping the branches, you check out a new non-default branch after switching to my-repo2
and do the merge from there.
@KastenBrot Tags are assigned to specific commit hash. This solution will change all commit hashes although commit messages are still there. So I don't think you can keep tags.
Regarding keeping the branches, you check out a new non-default branch after switching to
my-repo2
and do the merge from there.
Yeah, thanks for the Snippet tho :) If anyone is interested, I used this tutorial as a workaround. Clone the repo and all branches you need, manually delete everything you don't want and change the remote. It keeps the branches and tags. Not the cleanest way but it works.
Thank you!
Works perfectly! Thanks.
Hi,
Iam following the same, but in the push getting following error.Kindly help
Total 4203 (delta 1720), reused 4202 (delta 1720), pack-reused 0
remote: Resolving deltas: 100% (1720/1720), done.
remote: Rejected by YACC
remote:
remote: (c).-.(c) (c).-.(c) (c).-.(c) (c).-.(c) (c).-.(c)
remote: / .. \ / .. \ / .. \ / .. \ / ..
remote: ( Y )/ ( Y )/ ( Y )/ ( Y )/ ( Y )/
remote: (.-/'-'-.)(.-/'-'-.)(.-/'-'-.)(.-/'-'-.)(.-/'-'-._)
remote: || E || || R || || R || || O || || R ||
remote: .' -' '._ _.'
-' '. .' -' '._ _.'
-' '. _.' -' '. remote: (.-./
-'.-.)(.-./-
.-.)(.-./-
.-.)(.-./-'\.-.)(.-./
-\.-.) remote:
-' -'
-' -'
-' -'
-' -'
-' `-'
remote:
remote:
remote: Push rejected.
remote:
remote: refs/heads/master: 1f77a4445ec: expected committer email 'xxxx@yyy.com' but found 'bbbb@yyy.com'
remote:
remote: refs/heads/master: be3b4b47d9a: expected committer email 'xxxx@yyy.com' but found 'bbbb@yyy.com'
noticed an issue when running git merge repo1 --allow-unrelated-histories
to a repository with an existing main/master branch.
Scenario: copying over a branch in repo1 to repo2 with full commit history, repo2 already has an existing main and develop branch.
Problem: When running the last step of merging featurebranch
from repo1 to repo2 using git merge repo1 --allow-unrelated-histories
, it fails to completely copy the code from featurebranch
in repo1.
Solution: before creating featurebranch
in repo2, I ran git checkout --orphan featurebranch
and git rm -rf .
in this order. Since there is nothing to reference before running git merge repo1 --allow-unrelated-histories
it will completely copy featurebranch
from repo1 to repo2 with no conflicts.
If there are no main/master branches then this solution works just fine, Thanks! @trongthanh
Is there any way to move the history under repo2/(target folder) ? because after migration it is available only under repo2 root folder ... The history of repo2/(target folder) shows only 1 commit : "Collected the data I need to move"
This process works perfectly fine if I am moving a folder from repo 1 to the ROOT of repo 2 (& I think this is the default behavior of the methodology) preserving the history. Could you please let me know how can I move the repo 1 folder to a sub directory of repo 2 (NOT ROOT) ===> For example repo1/folder1 has to be moved to repo2/folderABC/folder1.