Search code examples
gitgit-diff

List git changed files excluding what changed in the parent branch


I want to supply my linter tool with files that changed in my branch. The list should include:

  • files committed in my branch since branched from its parent
  • files changed and staged
  • files changed but not staged yet

It should not include files that are changed in the parent branch since changing off. Thus if my branch doesn't have the latest commits from the parent branch that shouldn't matter.

What I tried so far:

  1. git diff --name-only gives my only what's changed, but not committed/staged
  2. git diff --name-only --staged gives my only what's staged, but not committed/changed
  3. git diff --name-only main... gives my only what's committed but not changed/staged
  4. git diff --name-only main gives everything (committed/staged/changed), but also includes what changed in main

In some way I already have my list if I combine the output of 1, 2 & 3. But it would be nice to have a single command. Is there something like that?


Solution

  • You're so close. git diff main... is defined as

    git diff A...B is equivalent to git diff $(git merge-base A B) B

    and you don't want it comparing against the second tip, your currently-checked-out commit, you want the default comparison, against the work tree, so you want to leave the last B off there.

    git diff $(git merge-base main @)
    

    What's confusing you may be that in revision expressions a missing revision name generally defaults to HEAD aka @, whereas in a command argument list a missing argument that isn't necessarily a revision name does not default to HEAD. So when you say main... it's using HEAD as the B rev above.