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

2

15

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.

Smasher answered 19/9, 2018 at 13:37 Comment(3)
Try git diff --name-only commit1 commit2Austin
I was of the opinion that since I am comparing a file between two branches (remote and local), it would not be considered as commits, but as branches. However, I was able to look at the hash of the latest commit on remote and pass that instead of commit1. By doing a git log, I found the hash of the local branch's latest commit, which represented commit2. So this did work.Smasher
Git will handle any valid SHA1 reference (commit hash) or pointer to it (branch/tag).Austin
C
20

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.

Cherry answered 19/9, 2018 at 13:44 Comment(3)
--name-status and --stat are similar to --name-only but give a bit more info about each file (just FYI).Lambskin
Didn't work for me. fatal: ambiguous argument 'origin/branch': unknown revision or path not in the working tree.Troutman
@Troutman Perhaps you need to download the branch first with git fetch?Cherry
W
0

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
Wordplay answered 21/7 at 18:49 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.