0

I'm sure this is very basic, but I've read the docs, and still can't figure it out.

I have two branches, and I need to merge some changes in a couple of files in manually (don't ask). I am viewing the files side by side, with the difference highlighted, with:

git difftool branch1 branch2 -- path_to_file

This allows me to copy everything I need into the file that will be remaining (which is in branch1 fwiw), but the file is some long filename in /tmp/. I don't know how to actually save my edits to the file that I am editing (I'm using vim as the difftool, and I have set noreadonly, the issue is that it's a different file (something in /tmp/, as I said).

There are only a couple of changes, and I could just manually edit the file in branch1, but I'm sure that git diff must allow me to do this, and I'd like to learn to do it properly.

Alex
  • 2,270
  • 3
  • 33
  • 65
  • @RomainValeri Yes, basically, everything is a mess, and only this file needs to be merged. The branches differ in other ways, but only this file needs merging. – Alex Feb 11 '19 at 18:08

1 Answers1

0

I can just force vim to overwrite the original file when it saves... I guess this works fine.

Alex
  • 2,270
  • 3
  • 33
  • 65
  • As a slightly invalid generalization, `git diff` compares two commits, and `git difftool` takes such a comparison— *not* the actual files, just the comparison—and feeds it through some sort of display tool that the user prefers. This is not *meant* to edit files. You can do exactly what you are doing, but that's a bit like using a wrench as a hammer: it works, but it's the wrong tool. Unfortunately, in this case there's no single right tool, though using `vimdiff` directly might be easier (just use `git show` to extract the not-current-branch version of the file, to a temp name). – torek Feb 11 '19 at 19:28
  • @torek that makes sense, thank you. I guess what i was looking for was the functionality that the merge tool/process gives, but not in the context of a merge! – Alex Feb 11 '19 at 19:31
  • @torek That said, how would I get vimdiff to show the difference between the same file in different branches? – Alex Feb 11 '19 at 19:36
  • I don't actually use vimdiff (I just read diffs directly, normally...) but as I understand it, you run `vimdiff `. To get the version of `path2` from the commit identified by `branch1`, use `git show branch1:path2`. To get the version from the commit identified by `branch2`, use `git show branch2:path2`. Redirect these to temporary files so that vimdiff can see them. – torek Feb 11 '19 at 21:22
  • Note also that if you want a proper three-way merge, which requires *three* files—merge base, left / ours, and right / theirs "sides"—you can use `git merge-file`. As before you'll want to `git show` each of those committed contents to a temporary file. – torek Feb 11 '19 at 21:23