1

I know there are a lot of questions about diffing a file in two git branches, but this is NOT what I'm asking.

I want to diff two different files in two different git branches.

The use-case is: I've got a Java file (Request.java) that was converted to Kotlin (Request.kt).

In the current branch (current) there is the Kotlin version, in the default branch (master), there is the Java version.

I want to diff the two files to see what changed, but Git doesn't know they are the same files (most of the contents have changed, as well as the name -- Git did not recognize the modification as a renaming).

What can I do?

EDIT. This is NOT a duplicate of the question that was tagged. This asks about two DIFFERENT files, not how diffing the SAME file in TWO branches.

CLOVIS
  • 771
  • 8
  • 24
  • 2
    Possible duplicate of [How to compare files from two different branches?](https://stackoverflow.com/questions/4099742/how-to-compare-files-from-two-different-branches) – Nisarg Sep 26 '18 at 13:59
  • git diff current:Request.kt master:Request.java – phd Sep 26 '18 at 17:00
  • @phd managed to make it work with `git diff [name of the current branch]:./Request.kt master:./Request.java` (notice `./`). You can add your comment as an answer, I'll accept it. – CLOVIS Sep 27 '18 at 15:17
  • @Nisarg As I said in the first line of the question, this is different because I'm searching for diffing two DIFFERENT files instead of the diffing the same file in two different branches, what you can see in the question you linked to. Please, read the question before saying it's a duplicate. In this case it turns out it was trivial -- then just say what it is *then* say it's a duplicate. – CLOVIS Sep 27 '18 at 15:19
  • It's still a diff, it's answered in https://stackoverflow.com/a/49652444/7976758 as ***Option 2***. – phd Sep 27 '18 at 15:31

1 Answers1

0

This asks about two DIFFERENT files, not how diffing the SAME file in TWO branches.

Using git worktree, you can checkout the second branch in its own separate folder.

You can then compare two different files with git diff --no-index:

git diff --no-index -- path/to/current/Request.kt /path/to/main/Request.java

However, make sure to use Git 2.36 (Q2 2022), which includes a new built-in userdiff driver for kotlin.

See commit 09188ed (12 Mar 2022) by Jaydeep P Das (JDeepD).
(Merged by Junio C Hamano -- gitster -- in commit 3ece3cb, 23 Mar 2022)

userdiff: add builtin diff driver for kotlin language.

Signed-off-by: Jaydeep P Das
Acked-by: Johannes Sixt

The xfuncname pattern finds func/class declarations in diffs to display as a hunk header.
The word_regex pattern finds individual tokens in Kotlin code to generate appropriate diffs.

This patch adds xfuncname regex and word_regex for Kotlin language.

gitattributes now includes in its man page:

  • kotlin suitable for source code in the Kotlin language.
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250