tags:

views:

72

answers:

3

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.

A: 

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.

VonC
+1  A: 

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 --indexed <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?".

Tobias Kienzler
I ended up rubyscripting output of git log -p, and turning it into a series of patch/git add/git commit - worked well enough for this simple case, even if it feels really wrong.
taw
@taw would you mind posting that script as an answer to [my question](http://stackoverflow.com/questions/3194240/how-can-a-patchfile-created-with-git-log-p-filename-be-applied-to-create-all) then? A workaround is better than nothing, and future visitors might appreciate this help
Tobias Kienzler
A: 

You could do something like the following.

  1. Use git fetch to pull the tip of the branch with the desired files from the wrong repository into the right repository.
  2. Use git filter-branch with either a --tree-filter or --index-filter to remove everything other than the desired files from the fetched branch.
  3. Use git rebase or reapply the tidied up commits onto an appropriate main branch in the right repository.
Charles Bailey