taw
taw

Reputation: 18820

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.

Upvotes: 4

Views: 998

Answers (3)

Tobias Kienzler
Tobias Kienzler

Reputation: 27393

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

Upvotes: 2

CB Bailey
CB Bailey

Reputation: 791421

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.

Upvotes: 1

VonC
VonC

Reputation: 1323223

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.

Upvotes: 0

Related Questions