0

I have 2 branches: main and other-branch

A -- B --> main
 \
   -- C -- D ... --> other-branch
  • commit B -> changed a file called main.txt
  • commits C, D ... -> change a file called other.txt multiple times

When I run git diff --name-only other-branch main it returns me both main.txt and other.txt, but I would like it to return only the diff to the common ancestor (C diff A), meaning it would exclude main.txt

Is there a way of doing that in a simple command?

Vitor Durante
  • 950
  • 8
  • 25
  • 1
    Try triple dots: https://stackoverflow.com/a/7256391/7976758 (https://stackoverflow.com/search?q=%5Bgit%5D+triple+dots): `git diff main...other-branch` or `git diff other-branch...main` – phd Nov 09 '22 at 20:31
  • 1
    I think you want `git diff --name-only main...other-branch` - this will show you the differences between `other-branch` (D) and the last common commit (A). – CryptoFool Nov 09 '22 at 20:32

1 Answers1

1

You want to see the changes between the merge-base of main and other branch. The merge-base is A and can be obtained with command git merge-base main other-branch. Now you need to diff other-branch against A:

git diff "$(git merge-base master other-branch)" other-branch

This is a lot to type and very confusing. Luckily, Git has a convenient shortcut for exactly that task:

git diff master...other-branch
knittl
  • 246,190
  • 53
  • 318
  • 364