简体   繁体   中英

Moving from one git branch to another and applying only my changes

I've forked a project (from branch A), and it seems that for some reason it was continued in another branch(B).

I tried to merge my fork (M) with the new branch (B), but that has resulted a lot of conflicts. The conflicts are because the new branch (B) has forked older branch (O) and part of the changes of A were not committed to B.

Now I would like all my changes (in branch M) to be merged with the new branch B with no conflicts, canceling all changes made by A to the origin O in my fork M.

在此处输入图片说明

what I want is in red. How do I do that?

One option would be to cherry-pick the range of commits in the M branch which occurred after the A commit, from the B branch:

git checkout B
git cherry-pick 2an38xma..f8s3kow2

Here 2an38xma is the SHA-1 hash, or unique identifier, of the A commit in the M branch. You can find this commit hash by typing git log from the A branch and scrolling down until you find the A commit. The range used in git cherry-pick says to apply one-by-one the commits after the A commit until the final commit f8s3kow2 in the M branch.

Keep in mind that you may get merge conflicts with each commit.

First make a copy of the original M branch with git branch M.old M so that you trivially can start over if things end up not like you want it.

Then you can rebase your M branch to start from B with

git rebase --onto B A M

This should give you exactly the red M branch in your drawing.

A here marks the start (not including) from where you want to consider changes and M the endpoint (including), which will be updated to the rebase result.

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