How move code from repoA to repoB with commit log in Git
Don’t you have any situation making you want to separate meaningful enough module from biggest source code to improve how you maintain your module?
If you do have a experience to work on developing software actively adopting plugin architecture, you could be in such a situation or you got good foresight and project and avoid such a situation in the first place :)
For me such a situation came up when I’m working on customizing OpenStack which were not going to be big change in the first plan but actually it is. So I had to think about how should I continue on and decided to separate.
What I wanted to do is following
- Move certain code from Nova to other repository (start to maintain there)
- Move commit logs related to the code I’m gonna move as well
I didn’t come up with good solution to move codes with commit logs immediately, but finally I did find a solution. At first I was thinking directly manipulating git metadata…I’m glad I didn’t have to :P
In short, “git cherry-pick” works between different repository and we can use it for this case.
Let me write down the procedure from creating new repo to move it. Suppose I already have the nova repository having own patches which is on the github or any git server (https://github.com/dummy/nova.git) in this sample scenario.
Above url is dummy and read this article while replacing this url with the url for repository you want to pick up the code for other repository
Initialize new repository
~$ mkdir driver_dedicated~$ cd driver_dedicated~/driver_dedicated$ git init
Add original repository url as remote git server
~/driver_dedicated$ git remote add org http://github.com/dummy/nova.git~/driver_dedicated$ git fetch org
Find commit id you want to move from original repository
Do something to find/create commit id list you want to move. If you want to move sequential commits, you can use shell script without saying
Move commit from original to new repository
~/driver_dedicated$ git cherry-pick <commit-id A>
~/driver_dedicated$ git cherry-pick <commit-id C>
~/driver_dedicated$ git cherry-pick <commit-id F>
~/driver_dedicated$ git cherry-pick <commit-id G>
If some commits include the code you don’t want to move, you can edit the commit by “git reset HEAD^” , “ do change/delete” and then “git commit” again.
Summary
“git cherry-pick” is usually used for other branch in same repository to merge other branch’s change and this use case is not so popular (or I just don’t know world :P). But actually cherry-pick works well between even different repositories and cherry-pick with different repository would be good solution for this use case.