简体   繁体   中英

How do I move a set of changes from one git repro to another — the repros aren't clones

Here's an interesting problem... I have two git repros on my machine. They both were migrated from another source control system and the first one wasn't converted exactly like it should have been. The second one works much better.

I have a bunch of changes in the first repro that I want to move to the other. While the code is essentially the same between the two repros, they were created indepedently of each other and therefore have a different set of history. (Code changes are the same, but the change numbers and other metadata is completely different) How do I move my code to the new git repro?

Edit: I can't really just "copy" my files because the two repros are stuck in different points in time. The "broken" repro with my code is forever stuck in the past.

You can perform a history graft to allow you to move the history from the 'old' repository to the new repository.

Let's say that you want to get changes from branch foo in repository X, and apply it to master in repository Y. First, let's get X added as a remote to Y so we can manipulate both histories:

git remote add tmp-X ~/repository-X
git fetch tmp-X

Now that both histories are in one repository, we can just cherry-pick to take the changes we want. Let's say the history looks like this in master :

commit 0d39ac6df9f5d6e7b50d670d705fa4aae049b70d
Author: Joe User <user@example.com>
Date:   Sat Oct 13 03:55:24 2012 -0400

    Common parent commit

And in tmp-X/master :

commit be79703c19475118b72655d1ea5d1957f3edea58
Author: Joe User <user@example.com>
Date:   Sat Oct 13 03:57:29 2012 -0400

    Another change I want to keep

commit 2a1e95c6cb458718448581664d9f8e6a0b260968
Author: Joe User <user@example.com>
Date:   Sat Oct 13 03:56:00 2012 -0400

    Change I want to keep

commit 0fdda21de3382acb005e9511e6ce95007f20e687
Author: Joe User <user@example.com>
Date:   Sat Oct 13 03:55:24 2012 -0400

    Common parent commit

    [some kind of junk in the commit message that changes the IDs]

We can git checkout master , then:

git cherry-pick 0fdda21..tmp-X

And now master looks like:

commit b2246afa547ee52028331d2aa59400bca1265581
Author: Joe User <user@example.com>
Date:   Sat Oct 13 03:57:29 2012 -0400

    Another change I want to keep

commit da36f4b6728aecfd3da7c63d9b1bd430c864858b
Author: Joe User <user@example.com>
Date:   Sat Oct 13 03:56:00 2012 -0400

    Change I want to keep

commit 0d39ac6df9f5d6e7b50d670d705fa4aae049b70d
Author: Joe User <user@example.com>
Date:   Sat Oct 13 03:55:24 2012 -0400

    Common parent commit

If you've had other changes in mainline, you might also find it helpful to branch off from a historical commit in mainline that corresponds to the base commit ( 0fdda21 above), cherry-pick to this branch, then merge this temporary branch into mainline.

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