Linux – git: Keep changelog for file when moving to a different repository

gitlinuxmigration

I am versioning my config files with git. Now I need to move some of my config files to a different repository, to achieve a clean structure.
Is there a way I can keep the change log for a file if I move it to a different repository?

I would like to have all commits of repo A in repo B that touched file A/a if I move it to B/a. Ideally, if I afterwards move A/x to B/x, I would want to see B/a and B/x appear together in commits that touched both files in repository A. I would not expect to have any development step of A/a merged into any of the commits of B, I just want them to appear there afterwards.

Thank you, best regards

Best Answer

Assume you want to transfer the history of filename.conf from one source repository to another receiving repository. I think the strategy you want to follow is:

  1. In the source repository, create a branch of commits which are re-written to contain only filename.conf.
  2. Merge the independent line of commits into a normal branch in the receiving repository.

Definitely make backups of your repositories before you do this!

In the source repository, use filter-branch to rebuild the history removing everything except filename.conf.

git checkout -b filtered-commits
git filter-branch -f --prune-empty --tree-filter 'find . -not -name filename.conf -exec rm {} \;' filtered-commits

Then, in the receiving repository:

git pull path/to/source/repo

If you also need to move the path that filename.conf is in within the repository, you'll probably need to use the --subdirectory-filter option on git filter-branch.

Related Question