简体   繁体   中英

Merging a modified file from one branch, to another branch with additional commits Git

I am trying to merge some changes made to a particular file (GitBasics.docx shown by change 2), to another branch.

Below are the three recent commits from branch develop (contains the file to which I made changes):

commit c5fab21e2837808fbc1a4d351b9d605ae856477e
Author: <>
Date:   Tue Jul 10 17:25:40 2018 +0530

    change 3

commit 872926a8a356183e46d281a14578f3895438ae72
Author: <>
Date:   Mon Jul 9 12:58:33 2018 +0530

    change 2 (made to Git Basics.docx)

commit 25ce9bcf0b2c44e29e148d11ea89df508758d014
Author: <>
Date:   Thu Jul 5 20:22:21 2018 +0530

    change 1

And below are the two recent commits from branch test (to which I've made additional changes):

commit c9f0ef332e1f5e9f398179372222d2ac5580ee96
Author: <>
Date:   Fri Jul 6 19:41:55 2018 +0530

    change 4

commit 25ce9bcf0b2c44e29e148d11ea89df508758d014
Author: <>
Date:   Thu Jul 5 20:22:21 2018 +0530

    change 1

As you can probably see, change 1 is where I created branch test . I would like to merge change 2 from develop to test . I won't be able to use git merge because develop also has additional commits (change 4) different from test and I do not want them merged.

By doing some research I came across the command git checkout source_branch <paths> which serves my purpose currently but I also learned that this method is simply replacing the file in the new branch, instead of merging. Hence if changes (to the same file) were made on both branches, I would lose them on the branch the file was being copied to.

The other option I believe is git cherry-pick but frankly I am unable to understand how to use this command from the information given online. How can I use this command, or are there any other ways to achieve what I am trying to accomplish?

I was referring to http://schacon.github.io/git/git-cherry-pick.html for info on git cherry-pick , I didn't think the provided solution would be so simple. I am sharing the link in case it helps anybody else.

As question was changed so changed the answer. Use:

git checkout test
git cherry-pick 872926a8a

This will create copy of 872926a8a commit (change 2) in branch test.

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