2

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.

Fernando Costa
  • 669
  • 1
  • 9
  • 31
  • 1
    Note that, aside from getting (or listing with `git ls-remote`) *commits* **from** some other repository, there is no such thing as a "remote repo" in Git: all commits are stored *locally*. You just connect to some other Git (a *remote*) and collect their commits, locally, via `git fetch`, and now you have them ... local! I like to think of Git as The Borg from Star Trek TNG: you introduce it to some other technology, and it just copies it all over. – torek Mar 14 '20 at 21:51
  • This make sense, but I need to compare commits that I didn't pull from a remote repo yet. – Fernando Costa Mar 14 '20 at 23:22
  • 2
    You do not have to run `git pull`. You have to run `git fetch`. That gets the commits into your repository but *does not touch any of your branches*. – torek Mar 15 '20 at 08:26
  • 1
    Also worth a side note: a *tracked* file is not a *committed* file. A tracked file is one that is *currently in the index and work-tree*. The index contents change as you select commits, use `git add`, or use `git rm`. Committed files go into the index and work-tree when you `git checkout` that commit, so at that point they're also tracked, but when you're inspecting individual commits, the tracked-ness of a file is irrelevant: all that matters is whether that commit contains that file. – torek Mar 15 '20 at 08:29
  • Yeap, you are right. I will restate the question, removing the term tracked. On the other hand, I think fetching changes doesn't solve my problem since I need to compare actual commits. Maybe I posted the wrong question. I need this because I intend to write a git hook to prevent pushes based on the list of files already commited by other team members. – Fernando Costa Mar 15 '20 at 10:08
  • I find the updated question less clear: are you asking about *committed* files, or *files in some Git repository's work-tree?* – torek Mar 15 '20 at 11:50
  • I added the term commited. Take a look now. Is is it still confusing? – Fernando Costa Mar 15 '20 at 13:07
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/209663/discussion-between-fernando-costa-and-torek). – Fernando Costa Mar 15 '20 at 13:24

2 Answers2

1

diff -y <(git ls-files) <(git ls-tree -r master --name-only )

I am taking the diff of the files in master to local (ls-files).1

2240
  • 1,547
  • 2
  • 12
  • 30
1

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.

Fernando Costa
  • 669
  • 1
  • 9
  • 31