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
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"