gitgit-ls-filesgit-ls-remotegit-ls-tree

How to compare local and remote git files?


I need to list all tracked commited files that only exist locally. Let's say my local repo is a commit forward the remote repo (on github or gitlab) and these are my local tracked commited files:

a.txt
b.txt

Now imagine in my remote repo there is just one of those files:

a.txt

What git command could be used to list the diff between local and remote repos in terms of tracked commited files? To be cristal clear, which git command (git-ls-files, gt-ls-remote, git-ls-tree, etc) could generate the following output:

b.txt

EDIT 1: I have to do this without pulling commits from the remote repo.

EDIT 2: I need this to write a git hook to prevent pushes, but I'm not sure this is a good use case for git hooks.


Solution

  • I think I have figured it out:

    git fetch origin && comm -2 -3 \
      <(sort <(git ls-tree -r master --name-only)) \
      <(sort <(git ls-tree -r origin/master --name-only))
    

    Final result:

    b.txt
    

    Thank you torek and polareper. Your suggestions helped a lot.