简体   繁体   中英

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.

  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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM