Skip to content

Instantly share code, notes, and snippets.

@whistler
Created March 16, 2015 17:31
Show Gist options
  • Star 51 You must be signed in to star a gist
  • Fork 8 You must be signed in to fork a gist
  • Save whistler/de34b77aba2221ed8b2e to your computer and use it in GitHub Desktop.
Save whistler/de34b77aba2221ed8b2e to your computer and use it in GitHub Desktop.
Copy files to another repository while saving git history
# copied from http://gbayer.com/development/moving-files-from-one-git-repository-to-another-preserving-history/
git clone <git repository A url> # clone source repository
cd <git repository A directory>
git remote rm origin # to make sure it doesn't affect the original repository
git filter-branch --subdirectory-filter <directory 1> -- --all # remove all files other than the ones needed
mkdir <directory 1> # move them into another directory where they will be stored in the destination repository (if needed)
mv * <directory 1>
git add .
git commit
git clone <git repository B url> # clone destination repository
cd <git repository B directory>
git remote add repo-A-branch <git repository A directory> # add source as a remote
git pull repo-A-branch master # pull
git remote rm repo-A-branch
@simonferronforget
Copy link

Hello, I'm trying to do this under Windows 7 and Git 2.13.3.windows.1 and I get following error:
...
­> git remote add feature/singleLevelTest /c/stash/test_manager/.git

git pull feature/singleLevelTest master
fatal: refusing to merge unrelated histories

Any idea why ?

@sdlins
Copy link

sdlins commented May 12, 2018

@simonferronforget, You can use --allow-unrelated-histories to force the merge to happen. Source: StackOverflow

@maphew
Copy link

maphew commented Nov 20, 2018

@mqliutie
Copy link

fatal: /Users/liu/work/static-pro/: '/Users/liu/work/static-pro1/' is outside repository.
how should I do?

@mistergamarra
Copy link

mistergamarra commented Apr 26, 2021

Hi @whistler after moving files at line 7 I've moved to but the .git folder hasn't been copied so git add . doesn't work. Do u have any suggestion?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment