Ajay Kumar
Ajay Kumar

Reputation: 3282

Git - List all files last committed in master branch

My Question is very similar to this - May I list the files of the current commit?

Problem Statement: I am trying to get the list of all the files which were new/changed in the last commit in "master" branch.

What I tried: Ran these commands from the directory where my .git directory is for the project. git show --name-only and git show --name-only master but doesn't show the expected result.

It shows me this:

Merge: f733602 4d98b76
Author: Some Name <[email protected]>
Date:   Thu Jul 1 11:34:49 2020 -0700

    Merge pull request #28 from secret/my_rqd_files

What I am expecting is - it should show the list of all the files with their full path in the repo.

Question: What should I do or what I am doing wrong?

Upvotes: 0

Views: 686

Answers (2)

matt
matt

Reputation: 536027

You are misinterpreting what a commit is. On the one hand you say:

List all files last committed in master branch

On the other hand you say:

I am trying to get the list of all the files which were new/changed in the last commit in "master" branch.

Those are two totally different things. A commit in Git is not a set of new/changed files. It is a snapshot of all your files.

Thus, you can see a list of all the files in the most recent master commit by saying:

git ls-tree --name-only --full-name master

But if your goal is to see what changed in that commit with respect to the previous commit, then you need to ask Git to generate a diff from the previous commit:

git diff --name-status master~1 master

That shows a compact list of files along with their status (i.e. was the file added (A) or modified (M)). Example:

% git diff --name-status master~1 master
M   a
A   what

I edited the file a and added the file what.

Upvotes: 1

eftshift0
eftshift0

Reputation: 30317

You are trying it fine, but it is a merge revision. This works a little different. Try

git diff --name-only HEAD~

Or

git diff --name-only master~ master

My hunch is that, given that it is a merge revision, git show would only display changes introduced in the revision itself that are not coming from the branches being merged.... things like conflict resolutions, for example.... plus additional changes that you might introduce by hand.

Upvotes: 3

Related Questions