How can I copy a directory from one git repository to another, preserving history?

I know that I can create a new repository out of a directory of a git repository. See here: https://help.github.com/articles/splitting-a-subfolder-out-into-a-new-repository/

However, how can I copy a directory from one repository to a new directory of another completely different repository, while keeping history of that directory?

Update: is it possible for that history to show up with git log?

Answers


You could do this with git filter-branch. Basically you'd want to:

  1. Split out the subpath in the first project into a new repository, using the link you've already found.
  2. Push it to the second project's remote on a unique branch.
  3. Fetch that branch into the second repository, then use git filter-branch to index-filter it into the correct subdirectory:

    git filter-branch --index-filter '
        git ls-files -sz | 
        perl -0pe "s{\t}{\tnewsubdir/}" |
        GIT_INDEX_FILE=$GIT_INDEX_FILE.new \
            git update-index --clear -z --index-info &&
            mv "$GIT_INDEX_FILE.new" "$GIT_INDEX_FILE"
    ' HEAD
    
  4. Finally, checkout the master branch of the second project (or whatever branch you're using), then merge in the newly-filtered branch.

Really not too awful an operation, all told. As AlexanderGladysh notes in the comments, you could also use a subtree merging strategy in the place of steps 3 and 4.


Need Your Help

Using asynchronous save changes on Entity Framework with multiple contexts

c# asp.net-mvc entity-framework async-await unit-of-work

I am using EF 6 with a UoW pattern. I have multiple contexts defined in my UoW since I'm using data from multiple databases. Everything seems to be working correctly except the CommitAsync function I

Java Group Chat Application: Knowing if groups have users currently active

java swing sockets chat chatroom

For the past 2 weeks, I have been working on a java group chat application. So far a user can login and join groups created by other users. The user can also send chat messages to each other inside...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.