Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

split repo using git filter-branch: how to recover history from renamed/moved files?

Disclaimer: I've seen Detach (move) subdirectory into separate Git repository but it doesn't fully answer the question.

I've migrated a project from svn to git. When it lived in svn, some files were moved and/or renamed.

After the git migration, some of the commits are only visible with git log --follow

so:

the git structure looks like:

MyMainRepo/
.git/
XYZ/
ABC/myFile.txt

git log ABC/myFile.txt show: - commit1 - commit2

git log --follow ABC/myFile.txt shows: - commit1 - commit2 - commit3 (the ABC directory didn't exist back then) ...

now, when splitting the git repo to have a distinct ABC git repo: git log --follow ABC/myFile.txt now lost the older history and I just have :

  • commit1
  • commit2

What I would like:

  • split ABC from MyMainRepo,
  • not losing the history

Any help is welcome :)

like image 474
user546973 Avatar asked Nov 14 '22 04:11

user546973


1 Answers

I just came across the same problem, and was unable to get a complete history of the moved folder, even with index-filter.

My workaround is to do a separate subdir export of the folder (ABC) that has been moved at a point before it was moved, and then graft the history of this repository in the new one.

Let's say ABC used to be called MNO. I clone MyMainRepo, reset to a version when MNO still existed (before it was renamed to ABC). I do a filter-branch with subdir to get a repository called "MNO".

Inside ABC, I do a git fetch ../MNO.

I then enter the commit of when the contents were moved into ABC (this will be the first commits with the contents in repo ABC) PLUS the last commit in the MNO repo into .git/grafts.

Now I can do git log on files inside ABC, and see history tracing all the way through how it was in MNO. Next step is to do another filter-branch to make it a permanent graft, etc. I've made a screencast that demonstrates grafting here (although the context is a bit different):

http://blog.tfnico.com/2010/10/gitsvn-6-grafting-together-svn-history.html

Note that this has to be done on a per-moved-folder basis, it's a lot of work, and you end up with a merge commit for each case. But git log works.

I'm writing this in a bit of a hurry, but if anyone is interested in trying this approach, let me know, and I'll try to clear it up a bit, and show some more concrete examples.

like image 123
Thomas Ferris Nicolaisen Avatar answered Dec 09 '22 14:12

Thomas Ferris Nicolaisen