11

Imagine that everytime I commit files, before I push them, I'd like to list them to check. How may I do that ?

I tried:

git ls-tree -r --name-only master
git ls-files -stage

If I edit a single file, add then commit it. If I try the above codes, it shows me all my files.

I want to list ONLY the files that will be pushed on the current commit.

PlayHardGoPro
  • 2,791
  • 10
  • 51
  • 90
  • For any commit, not just the current see https://stackoverflow.com/questions/424071/how-to-list-all-the-files-in-a-commit – Conrad Frix Sep 13 '17 at 19:14
  • In fact, each commit contains a copy of *every* file. If you want to see what's *different* from one commit to another, you must ask Git to compare one commit to another. – torek Sep 13 '17 at 19:38

3 Answers3

19

Git diff to the rescue on this one. You'll use the --name-only flag. To get the contents of the current commit, use this command:

#before stage
  git diff --name-only 
#staged changes before committing
  git diff --name-only --cached
#after committing
  git diff --name-only HEAD^ HEAD

If you want to see the files that you will be pushing if there is more than one commit, you'll need to specify your current branch head and the head on the remote

git diff --name-only remote/branch branch
LightBender
  • 4,046
  • 1
  • 15
  • 31
  • Everytime I write the `git diff ...` command, it shows nothing. Empty lines only. – PlayHardGoPro Sep 13 '17 at 19:28
  • 1
    @PlayHardGoPro Sorry, I forgot to include all three forms. I've updated my answer, depending on when you want the list, you'll need a slightly different syntax. – LightBender Sep 13 '17 at 19:50
  • Nice. the 3rd one worked fine. Just the file names ! – PlayHardGoPro Sep 13 '17 at 19:57
  • 1
    @PlayHardGoPro: note that comparing the commit identified by `remote/branch` with the commit identified by `branch` may not show you everything you will actually send. Consider, e.g., what happens if you first make a commit that replaces `README` with ten thousand copies of a photo of a frog, then make a second commit that replaces the bogus `README` with the original. Comparing the starting point to the ending point shows no changes, but `git push` will in fact push the multi-frog `README`, because that's in one of the *commits*. – torek Sep 13 '17 at 21:05
  • @torek makes an excellent point, this method will only provide a summary of the differences of the branch heads. – LightBender Sep 13 '17 at 21:13
  • 1
    Another note - make sure that you have run a `git fetch` to ensure that your local remote/branch copy is up to date. – Briana Swift Sep 14 '17 at 16:05
7

If you want to list files in the last commit (local or not), use this

git show --name-only
xvf
  • 330
  • 3
  • 13
  • 5
    Alternatively, use `--name-status` to also show the status of each file (e.g., M for modified, A for added, D for deleted), or `--stat` for a diffstat showing how many changes were in each file. – Lithis Sep 13 '17 at 19:13
1

As you say before the push, can i supposing that your work flow is git add then git commit then git push.

You can do the commit with the --short option link!

This will give you a output of all files change, add or delete in your that currently commit.

git commit --short -m "message for the commit"

enter image description here

Community
  • 1
  • 1
Angel
  • 1,670
  • 1
  • 11
  • 14