Git list of staged files
Asked Answered
H

2

258

I staged a lot of files using git add, and now I want to see all the files I have staged, without untracked files or changed, but unstaged files.

How do I do that? When using git diff --cached I can see the changes of what I just staged. So then I tried using git status --cached, but that --cached unfortunately doesn't work on git status.

Hypercritical answered 9/11, 2015 at 13:52 Comment(3)
simply typing git status gives you a list of staged files, a list of modified yet unstaged files, and a list of untracked files.Conant
similar to: #1588346Teshatesla
@houtanb, git status shows you a diff. (It doesn't show you all staged files).Kenon
H
417

The best way to do this is by running the command:

git diff --name-only --cached

When you check the manual you will likely find the following:

--name-only
    Show only names of changed files.

And on the example part of the manual:

git diff --cached
    Changes between the index and your current HEAD.

Combined together, you get the changes between the index and your current HEAD and Show only names of changed files.

--staged is also available as an alias for --cached above in more recent Git versions.

NB: This can be combined with --diff-filter to only show (filter) staged files that are added (A), modified (M) etc.

git diff --name-only --cached --diff-filter=AM
Hypercritical answered 9/11, 2015 at 13:52 Comment(9)
This is really useful because it allows you to get a list of filenames that you can then (in my case) pipe to a linter in a pre-commit hook.Gourami
If you add files with git add -N [files], this includes those too, even though they are not actually staged for commit yet. As such, it isn't quite exactly what we want for a pre-commit hook.Bonspiel
re "last commit"; Misleading. Better to say git diff --cached is short for git diff --cached headKenon
This is great. How can I get it to display the paths relative to my current working directory instead of relative to the root directory of the repository?Vegetal
Most of the time, you would remove deleted files from the list because you're passing this file list to a command that expects that all files exist. To remove deleted files from the list, you could do : git --no-pager diff --name-only --cached --diff-filter=AM.Jalousie
@Jalousie great point, and it can also be interesting for only added --diff-filer=A. I added this to the answer. Also, since you write --no-pager you may be interested in git diff-index which I suspect is more of a plumbing commandHerlindaherm
I'd suggest using --diff-filter=d to only ignore deleted files, as you have renames and stuff in there you should be considering as well.Pentode
@Bonspiel With git 2.34.1 files added with -N are not listed. Perhaps this has been fixed since your comment?Waldheim
I'm not sure when this occurred, but the pager seems to be used by default on macOS. Providing -P or --no-pager directly after git will resolve this. Does it make sense to update the main question? Or do other platforms treat this different?Klepac
H
-8

You can try using git ls-files -s

Hamforrd answered 19/1, 2021 at 5:23 Comment(5)
This seems to list all tracked files (staged or or unstaged), not the staged files (that the question is about). This does not answer the question: "to see all the files I have staged ... without ... changed but unstaged files."). This answer is blatantly wrong. Why is this being voted up when it doesn't answer the question (it answers some other question)?Salutatory
This does not answer the question and is completely wrong. It answers some other question. Can you delete it, please? Thanks in advance.Salutatory
Amplifying information: #28159825Costive
that's doesn't do what is asked in the questions.Woodnote
But anyway it sounds logical. What do I need? List files! What do I use? "ls-files". What do I need? Staged files! What do I use? "-s". Also "-s" is "--stage" git help. So it is git's fault.Caviness

© 2022 - 2024 — McMap. All rights reserved.