Problem
I have 3 branches, main, dev, and feature. Both feature and dev have modified file myFile. I am currently on the feature branch with a clean working tree, and I want to merge dev into feature. However, there are merge conflicts in myFile.
I want to combine the changes to myFile from dev and feature and end up with conflict markers in the file.
Setup
Here are the steps to follow to set this situation up.
mkdir testcreate a repocd testgit initgit checkout -b maincheckout the main branchtouch myFilecheck in a filegit add myFilegit commit -m "add myFile"git checkout -b devcreate a dev branchecho 'aaa' > myFilemodify the filegit commit -am "change myFile"touch fileBdo some other changesgit add fileBgit commit -m "add fileB"git checkout -b feature maincreate a feature branchecho 'bbb' > myFilemodify the file differentlygit commit -am "change myFile"touch fileCdo some other changesgit add fileCgit commit -m "add fileC"
Now everything is set up and your history looks like this:
It's time to pull the changes in from dev into feature for myFile only, leaving the rest of the file tree untouched.
git merge devon no, conflicts!git status
On branch feature
You have unmerged paths.
(fix conflicts and run "git commit")
(use "git merge --abort" to abort the merge)
Changes to be committed:
new file: fileB
Unmerged paths:
(use "git add <file>..." to mark resolution)
both modified: myFile
The goal is to change the contents of myFile to
<<<<<<< HEAD
bbb
=======
aaa
>>>>>>> dev
without modifying any other files at all.
How do you do that? Look at the "working but not nice solutions" below to see some methods for how to achieve this. But the point of this question is to discover if there is an easier or simpler way to do it, because I don't think the ways I listed below are very simple.
Failed Solutions
❌ git checkout dev -- myFile
This just gives me the exact file that is in the dev branch, removing any changes that have been made on the feature branch.
❌git diff + git apply (reference)
git diff feature dev -- myFile > temp.patch.git apply temp.patch.rm temp.patch
This also ends up with a file that has no conflict markers in it.
Bonus note: if the order is wrong (i.e. git diff dev feature -- myFile > temp.patch) then it won't work at all.
❌git cherry-pick
This is often recommended for this kind of fix, but there's no commit to cherry pick here. I need a file, not a commit. There could be dozens or hundreds of commits that touched myFile.
Working but not nice solutions
😖 New branch + copy file + merge
git checkout maingit branch deletemegit checkout deletemegit checkout dev -- myFilegit commit -m "grab myFile from dev"git checkout featuregit merge deleteme
(Then later on remember to git branch -D deleteme)
This is kind of terrible and muddies up the history. Plus you have to create a temporary branch.
😑 3-way merge with common ancestor
git show dev:myFile > dev-versiongit show main:myFile > main-versiongit merge-file myFile main-version dev-versionrm dev-version main-version
This is slightly shorter than the above method, but requires temporary files.
😨 Merge the branches and then reset or revert everything else
I don't even want to think about doing this.
Question
Is there any way to do this that doesn't involve creating temporary files/branches?

git inita repository, show us the commands to get into your situation, and then show us the result you'd like. Also, please explain the purpose, that might help us understand.