wortwart
wortwart

Reputation: 3360

How can I merge a single file in git?

In our git workflow (using GitHub Enterprise) we have feature branches that have to be merged into two different and independent branches (dev and master). That works well as long as there is no merge conflict.

For example, two feature branches merged into dev have conflicting changes for file F. Both changes should end up in dev, but I don't want dozens of unrelated changes from dev getting merged into my feature branch because they would end up later in master and that's not okay.

What we tried:

The best we came up with so far was to do the changes in GitHub and to revert the merge in the feature branch afterwards but that doesn't look like a clean and solid solution to me.

Upvotes: 2

Views: 6275

Answers (2)

The problem is, you do not merge files in git. You merge commits. Therefore, whatever the commit contains, it is supposed to be included in the merge except you can hand pick and edit final contents of the merge. But that is supposed to be used for resolving conflicts and that kind of staff.

Maybe you can split your commit in the feature branch into two commits, so that the first commit will only contain the changes to the file in question, and the second commit will add the rest. And you can only merge up to that commit.

Upvotes: 4

torek
torek

Reputation: 489918

Merging a file requires three versions, not two:

  • You need a "left side" or --ours or LOCAL version, whatever you'd like to call it. That's the one with your changes.
  • You need a "right side" or --theirs or REMOTE or other version: again, whatever you'd like to call it is not that important; it's the version of the file with their changes, whoever "they" are.
  • Finally—or really, first—you need a common starting point version of this file. That's the version both you and they started from, before you made your changes, and they made their changes.

You get these files from anywhere you'd like. When you run git merge, Git gets all three of these for you:

  • Your version is the current one, in your current commit, as seen in your working tree. You select this commit with git checkout or git switch.
  • Their version is the version of the file in the branch or commit you name on your command line: git merge theirbranch, for instance, uses the name theirbranch to look up the commit hash ID of the commit that contains their version of this file.
  • Git locates the merge base copy—the common starting point—automatically, using the commit graph. See Pretty Git branch graphs. You can have Git compute the merge base(s)—there can be more than one, though if there are several, things get a little sticky—using the git merge-base program, provided you know the right two commit hash IDs, or have names for them: Git itself runs git merge-base --all HEAD theirbranch, for instance.

If you'd like to merge just one file, you will have to locate all three copies of the file in some manner. Exactly how you do that is up to you. Then you simply use the git merge-file command, as described in its documentation.

Upvotes: 1

Related Questions