8

I want to run ediff to compare my current version of a file (in git) with a version of the file on a different branch.

I run M-x ediff-revision from within an open buffer for the file in question. I answer the two questions:

  • Compare revisions for file: I blank this answer (the default is the current buffer which is correct).
  • Revision 1 to compare: I accept the default again ("default file's latest version").
  • Revision 2 to compare: I enter a git branch name

This works great for viewing the differences between the two files. However, I want to be able to edit the current version (i.e., by hitting a or b in ediff to copy or remove blocks of diff) and the "current version" buffer is a read-only buffer with a manufactured name (file.~git-branch~). I want the "current version" buffer to be the read-write buffer that I started with, not a read-only buffer on the current branch's version.

2 Answers 2

8

Emacs is doing what you told it to do instead of what you meant. ediff-revision asks for three pieces of information:

  1. The file to work on — default: the file that the current buffer is visiting.
  2. The old revision — default: the latest revision.
  3. The new revision — default: the current state.

At step 1, you select the file, you aren't telling Emacs that you want to use the file as it is in the buffer. The default for step 2 is the latest revision, not the current working copy. The default for step 3 is the buffer content, which is the working copy if the buffer is not modified. With what you did, you're comparing what you've checked out (typically the latest revision on the current branch) with some other branch, not the working copy with some other branch.

You need to give the branch name as the first revision (step 2), and leave the third question blank to say you want to use the buffer state. You can only select the current buffer state for revision 2, not for revision 1 (this is built into ediff-vc-internal).

8

When using magit:

  1. Use l -al l to view the revisions of all branches in your history
  2. go to the line containing the revision you want to compare your current buffer content against.
  3. Type d and you are asked for which revision to compare the current working tree against (the default is the revision at point). Confirm.
  4. You'll get a diff overview over changes of all file differences between the current working tree and the revision. Put point on the file your interested in and hit e for invoking an ediff session on it.

Magit offers a number of options to display the revisions (step 1). You may choose If you have a very complex branch structure, you may want to use some more filtering to find the desired revision.

4
  • Great! This is exactly what I was looking for. Just a thing... What do you mean by -al ? I just normally do l l or l a on magit-status buffer.
    – nephewtom
    Commented Mar 28, 2017 at 18:30
  • This was for an older user interface layout of magit. In the current versions you can use l a. Magit's layout changed quite a few times over the last 3 years.
    – dfeich
    Commented Mar 30, 2017 at 5:09
  • The current interface currently has l L to see all local branches, and l b to see ALL branches. l a shows all references.
    – Renato
    Commented Jan 21, 2023 at 21:11
  • I can successfully see the current buffer and the selected revision side by side with ediff, but the commands ediff-copy-B-to-A and ediff-copy-A-to-B do not work: "ediff-copy-diff: buffer-read-only #<buffer [filename].~5a348c0^~>". Presumably this is because A and B are visiting commits rather than files, and Ediff doesn't know how to handle these cases? Is there a way to selectively copy changes to either buffer and then commit these changes?
    – Pablo
    Commented Jun 13, 2023 at 19:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.