0

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 <some.name@company.com>
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?

Ajay Kumar
  • 2,906
  • 3
  • 23
  • 46
  • 3
    What does it show? what are you expecting? – evolutionxbox Jul 21 '21 at 13:04
  • @AjayKumar : if from your `master` branch, you run : `git log --graph --oneline --name-status --first-parent`, do you see the list of 30+ files you expect spread over several commits ? – LeGEC Jul 22 '21 at 14:02

2 Answers2

3

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.

eftshift0
  • 26,375
  • 3
  • 36
  • 60
  • 2
    I agree with your answer, `git show --first-parent --name-only master` should also work – LeGEC Jul 21 '21 at 13:32
  • Oh, that makes sense. Great tip!... actually, you should provide it as a separate answer. I would upvote it – eftshift0 Jul 21 '21 at 13:33
  • I tried both, as per the comment here and this answer but shows only two files at the project root level only. While I am expecting (and I know) 30+ files. How can I make it recursive? – Ajay Kumar Jul 21 '21 at 13:42
  • But what do you mean recursive? What you are asking there is for the changes between the last revision and your current revision... IOW **the list of all the files which were new/changed in the last commit in "master" branch** (if you are standing on master, of course... if you are somewhere else use the one where I provided `master~ master`), which is what you asked for. Where were those 30+ files modified? Maybe adding a little `git log --oneline --graph master` into the question would help us understand what you mean. – eftshift0 Jul 21 '21 at 14:28
  • Not too sure it that helps. I ran these commands from the directory where my .git directory is for the project. Sorry for my limited knowledge in git commands. – Ajay Kumar Jul 21 '21 at 18:12
  • @eftshitf0 : no need for another answer, you correctly pointed out the OP was on a merge commit. I'll let you edit your answer if you want to mention `git show --first-parent` – LeGEC Jul 22 '21 at 12:33
  • @AjayKumar : what are the two files at the project root level ? can you add the output of your command to your question ? also : what elements make you sure that you should see 30+ files there ? – LeGEC Jul 22 '21 at 12:35
1

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.

matt
  • 515,959
  • 87
  • 875
  • 1,141