How to merge specific files from Git branches

后端 未结 11 1869
长发绾君心
长发绾君心 2020-11-30 16:23

I have 2 git branches branch1 and branch2 and I want to merge file.py in branch2 into file.py in branch1 and only that file.

In essence I just want to work on the fi

11条回答
  •  心在旅途
    2020-11-30 16:59

    To merge only the changes from branch2's file.py, make the other changes go away.

    git checkout -B wip branch2
    git read-tree branch1
    git checkout branch2 file.py
    git commit -m'merging only file.py history from branch2 into branch1'
    git checkout branch1
    git merge wip
    

    Merge will never even look at any other file. You might need to '-f' the checkouts if the trees are different enough.

    Note that this will leave branch1 looking as if everything in branch2's history to that point has been merged, which may not be what you want. A better version of the first checkout above is probably

    git checkout -B wip `git merge-base branch1 branch2`
    

    in which case the commit message should probably also be

    git commit -m"merging only $(git rev-parse branch2):file.py into branch1"
    

提交回复
热议问题