简体   繁体   中英

Move a directory in a git repo to another git repo, preserving part of commit history

I forked off an existing git repo ( originalrepo ) onto my own github, let's call this repo myrepo .

I edited two or three files in a certain directory here, say mydir .
However, mydir is not located directly in myrepo , instead it's something like myrepo/dirA/dirB/mydir .

Now my group has set up a separate git repo called grouprepo and I want to move the files that I worked on to this new repo.

I want to move it directly to grouprepo/mydir and preserve the commit history, but only the commits that I did (not including thousands of commits from originalrepo ) and only move the files that I made changes to.

Is this possible? If so, how?

You can do it very simple:

  • Add second origin to your new project pointing to the "old" one
  • pick up the relevant commits you need

     # Add the reference to the old project where your commits are git remote add origin2 < url> # get all the data from all the repositories git fetch --all --prune # Now checkout the branch you want to add your previous commit to git checkout <branch> # "pick" the desired commits you want git cherry-pick commit1 commit2 ... commit 

And you are all set and your commits (and only them) are in your current branch.


cherry-pick

choose which commits (from any branch or even can be loose commit) pick this commit and place it in my current branch, in other words - take any commit from anywhere in the repository add bring it to my branch


Retrieving all the content of all the remotes

在此输入图像描述

cherry-pick the desired commits into the current branch
(im my case there is a conflict but it makes no difference.)

在此输入图像描述

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