POST
Moving files between git repositories, preserving history
I needed to copy a directory between two git
repositories while preserving its history. I found some good instructions at http://gbayer.com/development/moving-files-from-one-git-repository-to-another-preserving-history/, which got me started, but I figured out a way to avoid having to move all the files into their directory again (lines 5-6 in Greg’s instructions) by reversing the filter to remove everything I don’t want instead of only including the directory I want. Here are the steps (the idea is the same as in Greg’s post, so please read that to get the explanation, I’m only listing the commands here for reference):
Get files ready for the move
(directory1
is the one you want to preserve)
git clone git-repository-A-url repo-A-tmp-dir
cd repo-A-tmp
git remote rm origin # for safety
git filter-branch --tree-filter 'rm -rf $(git ls-files | egrep -v directory1)' -- --all
(2016-06-08: update thanks to Pawel Veselov, using git ls-files
instead of ls
in the last command)
The last line replaces lines 4-8 in the original instructions.
At this point you should verify that only directory1
remains in the repository, with the correct history. I used the following command to verify that only commits related to my desired directory remained:
git log -p | grep '^diff ' | grep -v directory1
Merge files into the new repository
These steps are identical from Greg’s instructions.
git clone git-repository-B-url
cd git-repository-B-dir
git remote add repo-A-branch repo-A-tmp-dir
git pull repo-A-branch master
git remote rm repo-A-branch
- Related:
- My blogging setup with Emacs, Org mode, ox-hugo, Hugo, GitLab and Netlify
- How to easily create and use human-readable IDs in Org mode and Doom Emacs
- My Doom Emacs configuration, with commentary
- August 2020 release of "Learning Hammerspoon" is out!
- How to insert screenshots in Org documents on macOS
- New release of "Learning Hammerspoon" is out!
- First release of "Learning Hammerspoon"
- My Hammerspoon Configuration, With Commentary
- My Emacs Configuration, With Commentary
- My Elvish Configuration With Commentary
- Just Enough Lua to Be Productive in Hammerspoon, Part 2
- Just Enough Lua to Be Productive in Hammerspoon, Part 1
- Using Nix's "Single-user Mode" on macOS
- Fixing the Outlook-to-OmniFocus Script
- Using Spoons in Hammerspoon
- Hosting a Ghost Blog in GitHub - the easier way
- Getting Started With Hammerspoon
- Fast translation with Google Translator
- Spotlight: use-package, a declarative configuration tool - Mastering Emacs
- Find all single-note tags in Evernote
- How to solve truncated docker output in Mac OS X using boot2docker
- HowTo: Changing the available display formats for Evernote's "Insert Date" command
- Installing cfengine on Mac OS X
- Automatically creating tasks from email in Outlook
- Making cygwin, Windows and emacs understand the same symlinks
- Storing CFEngine configuration in CVS
- Installing cfengine on Windows 7 under cygwin