4

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.

taw
  • 18,110
  • 15
  • 57
  • 76
  • I'm no GIT expert but could you: 1. clone the target repo. 2. push the wrong source into the cloned target. 3. rebase the changesets you want to keep. 4. push only those changesets back to the original repo? As I said, I'm not an GIT expert so I don't even know if this is possible. – Lasse V. Karlsen Jul 07 '10 at 07:55
  • Also checkout [this](http://stackoverflow.com/questions/1365541/how-to-move-files-from-one-git-repo-to-another-not-a-clone-preserving-history) (near duplicate) question. With thousands of views, it might be the more authoritative answer. – Randall Cook May 09 '12 at 18:50

3 Answers3

2

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

Community
  • 1
  • 1
Tobias Kienzler
  • 25,759
  • 22
  • 127
  • 221
  • 1
    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 Jul 09 '10 at 03:05
  • 1
    @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 Jul 09 '10 at 03:57
  • With git version 1.7.10.3 `git apply --reverse --indexed ` will return with `error: unknown option `indexed'` – oschrenk May 31 '12 at 13:47
1

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.
CB Bailey
  • 755,051
  • 104
  • 632
  • 656
0

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.

Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250