Moving files between git repositories

In our project we had to move several files between git repositories. Having searched the internet, there didn't seem to be any well defined way to do it. I don't claim to be a git wizard, but I came up with a solution that I wanted to share in case it was useful to anyone else.

There is a method to filter a folder in a git repository which can then be used to move between repositories, but not so easily for files. I came up with a method that effectively involves rewriting the history to put everything into a folder which we then steal.

As we are using filter-branch we first need to take a clone of the source repository. Filter-branch is a destructive command that actually edits the history and will destroy your repository.

#!/usr/bin/python
import os

files = ["service-availability-checker.cpp",
"service-availability-checker.h"]

for filename in files:
    if not os.access("keep", os.R_OK):
        os.mkdir("keep")
    if os.access(filename, os.R_OK):
       os.rename(filename, "keep/"+filename)

move-files.py

git filter-branch --tree-filter /path/to/move-files.py

Now we can grab the folder we wanted

git filter-branch --subdirectory-filter keep/

Now we turn the directory into a set of patches

git format-patch HEAD^ HEAD

This gives us a nice set of patch files which contain commit information which can be imported into a new repository.

Leave a Reply

Your email address will not be published. Required fields are marked *