Is it possible to move a bunch of files from one git repository to another while preserving (most) history?
I added some files to the wrong repository and didn't realize until later, once they already had quite a bit of history (just linear revisions, no branching or anything).
Is 开发者_高级运维it possible to get these files and move them to another git repository together with their history? I don't care if their remnants stay in their current one or not, as long as the new one has it all.
With git log -p <filename> > <patchfile>
(see doc) you can export the whole history of the file which can then be applied to the other repo via git apply --reverse --index <patchfile>
. However this does not recreate the commits, and I haven't figured out the switch for git-apply
to do this.
If you want to get rid of the file in the original repo, see "How do I remove sensitive files from git’s history?".
You could do something like the following.
- Use
git fetch
to pull the tip of the branch with the desired files from the wrong repository into the right repository. - Use
git filter-branch
with either a--tree-filter
or--index-filter
to remove everything other than the desired files from the fetched branch. - Use
git rebase
or reapply the tidied up commits onto an appropriate main branch in the right repository.
You could try a git format-patch
on the branch you a reworking on, to generate patches for the last commits improperly made on that repo.
See this SO question.
You would then apply those path on the correct repo.
And you would reset --hard
your current branch in order to get rid of the extra commit.
精彩评论