Do the merge with --no-commit
. Once the automatic part of the merge is finished, git stops (the same way it would if it cannot complete the merge itself) and you may now alter the merge result in whatever way you like, including checking out the desired version of file A:
git checkout branch -- A
or:
git checkout --ours -- A
(etc).
Once the tree has been adjusted to your satisfaction (if you have automated tests, use them!), git add
if needed, and git commit
the result to complete the merge.
(You can automate some or all of this, if you like.)
Edit to add example. Let's say you want to merge dev
into master
, but keep master
's version of xyz.html
. (I removed xyz.html
in branch dev
. I also added file foo
, and made no other changes.) Let's also use --no-ff
to force a real merge. (Otherwise git may be able to do a fast-forward merge, if branch dev
is exclusively ahead of master
. In this case our --no-commit
has no effect, since git won't attempt a commit anyway.)
$ git checkout master
Switched to branch 'master'
$ git merge --no-ff --no-commit dev
Removing xyz.html
Automatic merge went well; stopped before committing as requested
$ git checkout master -- xyz.html
(You can check out any particular version of xyz.html
: the above just gets the version from the tip of branch master
. Since you are actually on branch master
there are many other ways to spell this; that's just the most explicit I could think of.)
$ git status
On branch master
All conflicts fixed but you are still merging.
(use "git commit" to conclude merge)
Changes to be committed:
new file: foo
$ git commit
(at this point the editor opens, as with any other commit; writing out the file and exiting produces)
[master 9746054] Merge branch 'dev'
$