Omkar Neogi
Omkar Neogi

Reputation: 735

See (list) files changed between local and remote branch - git

How do I list files that are "diff"ed between my current local and the corresponding remote branch?

The situation is that I had made a push to remote earlier and since then have rebased off master + squashed some commits.

On doing a git status, I get

Your branch and 'origin/YourBranch' have diverged, and have 11 and 2 different commits each, respectively.

I do not want to do a "git diff origin/YourBranch YourBranch" because the number of changes made is large. After confirming that the files which have changed between local and remote makes sense, I shall do a force push.

Upvotes: 16

Views: 12182

Answers (2)

VonC
VonC

Reputation: 1324377

As noted by biffen in their comment:

--name-status and --stat are similar to --name-only but give a bit more info about each file.

With Git 2.46 (Q3 2024), batch 8, adds details on those commands: the documentation for "git diff --name-only"(man) has been clarified that it is about showing the names in the post-image tree.

See commit 4986662 (17 May 2024) by Junio C Hamano (gitster).
(Merged by Junio C Hamano -- gitster -- in commit 6e95dce, 28 May 2024)

diff: document what --name-only shows

The "--name-only" option is about showing the name of each file in the post-image tree that got changed and nothing else (like "was it created?").
Unlike the "--name-status" option that tells how the change happened (e.g., renamed with similarity), it does not give anything else, like the name of the corresponding file in the old tree.

For example, if you start from a clean checkout that has a file whose name is COPYING, here is what you would see:

$ git mv COPYING RENAMING
$ git diff -M --name-only HEAD
RENAMING
$ git diff -M --name-status HEAD
R100  COPYING RENAMING

Lack of the description of this fact has confused readers in the past.
Even back when dda2d79 ("[PATCH] Clean up diff option descriptions.", 2005-07-13, Git v0.99.1 -- merge) documented "--name-only", "git diff"(man) already supported the renames, so in a sense, from day one, this should have been documented more clearly but it wasn't.

Belatedly clarify it.

diff-options now includes in its man page:

Show only the name of each changed file in the post-image tree.
The file names are often encoded in UTF-8.
[...]
Show only the name(s) and status of each changed file.

The post-image tree is an important concept because --name-only shows the file names as they appear after the changes have been applied. This means that if a file was renamed, only the new name is shown.

As shown in the commit above:

git mv COPYING RENAMING
git diff -M --name-only HEAD

Output:

RENAMING

Here, RENAMING is the file name in the post-image tree.

More details: git diff --name-status origin/YourBranch YourBranch

You would get:

A   newfile.txt
M   modifiedfile.txt
D   deletedfile.txt
R100    COPYING    RENAMING

Upvotes: 1

Jonathan.Brink
Jonathan.Brink

Reputation: 25383

To list just file names, use the name-only flag like this:

git diff --name-only origin/YourBranch YourBranch

name-only will (from the Git doc):

Show only names of changed files

BTW, this can also be used for other commands, such as show.

Upvotes: 22

Related Questions