Copying different git repository content and preserving specific directories content and history
I have git repositories rA and rB, I would like to copy only specific directories in rA and preserving their history, e.g.
rA
- d1
- d2
- d3
- d4
rB (with d1 and d2 preserved)
- d1
- d2
What I have done so far is:
git remote add -f rA /path/to/rA
git merge -s ours --no-commit rA/master
That gives me all the files in rA
and I am wondering if I can use commands such as git-filter-branch
to exclude d3
and d4
? Or I a开发者_StackOverflowm having a terribly wrong concept on how repositories are treated and managed?
Thanks.
Updated:
I made this possible by:
- Duplicate (cp -R) my existing repository to a dir.
Perform this command on this new dir:
git filter-branch --index-filter 'git rm --cached -rgit ls-tree --name-only --full-tree -r $GIT_COMMIT | grep -Ev "^dirs|to|be|included" | xargs -r git rm --cached -r; fi' -- --all
Carried on the steps I mentioned earlier.
You could first split rA in two (in order to get only the directories you want), before adding it to rB.
However, a simple merge will likely to have many conflicts, since the split would have rewritten all SHA1 in rA.
Meaning any common history rA and rB may have shared is lost.
Your merge (-s ours
) allows for new files from rA to appear in rB.
精彩评论