Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
How to move a folder from one repo to another and keep its commit history
# 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.
@sudhirshahu51

This comment has been minimized.

Copy link

commented Jul 2, 2017

I am facing a problem with this please tell me where is repo1 present i.e. on my actual git repository or at the local system?. please help me out I am too confused with this.
image

@recyclerobot

This comment has been minimized.

Copy link

commented Aug 7, 2017

@trongthanh from git 2.9 you'll need to do git merge repo1 --allow-unrelated-histories (line 35)

@Romeus

This comment has been minimized.

Copy link

commented Aug 31, 2017

@sudhirshashu51 Have exactly the same problem

@robin-carry

This comment has been minimized.

Copy link

commented Sep 12, 2018

Rock star article :)

@marcellanz

This comment has been minimized.

Copy link

commented Mar 8, 2019

@sudhirshahu51 your first command failed as by the "usage" error git displays. you called "git remote add" with three arguments (repo, ../Python-programming-Udacity and /) but you can only supply 2 (, ) as by the git command stated. So for sure, the next command (git fetch repo1) also fails.

For help I suggest to provide more context, what you did and what you have. And during the session of what you did, have a eye to errors or message you get from git that shows any error. This helps to find which commands you issued failed and why.

@jpop32

This comment has been minimized.

Copy link

commented Apr 30, 2019

This failed (git version 2.16.2):

git merge repo1

with

fatal: refusing to merge unrelated histories

needed to include the switch

git merge repo1 --allow-unrelated-histories

and the rest went without any problems.

@flakrat

This comment has been minimized.

Copy link

commented Jun 11, 2019

+1 to @jpop32 I was just about to comment about the need for --allow-unrelated-histories

@trongthanh

This comment has been minimized.

Copy link
Owner Author

commented Jun 12, 2019

Added --allow-unrelated-histories to the Gist as per above comments

@gibello

This comment has been minimized.

Copy link

commented Aug 16, 2019

Works like a charm, thanks a lot :)

@nhmaha

This comment has been minimized.

Copy link

commented Aug 24, 2019

Thanks a lot. it works.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.