Is it possible to merge from one branch to another in git with only certain files?

Let’s say I have two branches

  • A
  • B

I want to merge the changes in A to B. However, A has some files that I changed as well as some files that someone else changed. For example, in A, there are 2 files that changed.

  • path/to/JavaFile1.java (I changed this file)
  • path/to/another/JavaFile2.java (another user changed this file)

On merging from A to B, I find that there are conflicts with the file that someone else changed (in the running example, JavaFile2.java). Now, I want the merge to proceed so as to

  • “ignore” or “skip” the file that was changed by someone else (I don’t know how to resolve that conflict)
  • I want the merge to be such that when someone else merges, they too will also have to deal with the conflict and decide what to do.

I am not sure if a resolution of using mine or theirs is appropriate, because on the next merge, it might never consider the conflict again (git will mark the conflicts as resolved in future merges).

I suppose I can cherry-pick too, but on our team, we have a policy to merge forward (e.g. from A to B) and cherry-pick backwards (e.g. from B to A).

Is it possible to do as I stated? We also have a policy that a developer must merge their own commits, but, unfortunately, this developer is not available for the next two weeks (vacation).

  • Git merge from a specific folder only
  • Reading GIT Merge Markers
  • Completely manual Mercurial merge
  • How do I stash manual resolutions from an uncommitted git merge for later?
  • Github enforce --no-ff when merging
  • I cannot commit changes after merge in SVN
  • How can one determine the committer of a cherry-pick in Git?
  • Attempting to rebase gives endless merge conflicts
  • One Solution collect form web for “Is it possible to merge from one branch to another in git with only certain files?”

    For me the conceptually easiest way to deal with this would be to perform the merge and then reset the file in question to the commit in B which was at the HEAD of that branch before the merge.

    The command you would use to reset the file modified by another user is:

    git checkout <SHA-1> path/to/another/JavaFile2.java
    

    where <SHA-1> is the hash of the most recent commit on B before you did the merge (i.e. the version you want to keep).

    If there is a merge conflict in this file during the merge, then you can reset it using the above command and commit. If the merge completes without a conflict, then you can still run the above command, and you can amend the merge commit:

    git add path/to/another/JavaFile2.java
    git commit --amend
    
    Git Baby is a git and github fan, let's start git clone.