Git list of staged files

187,923

Solution 1

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.

Solution 2

You can try using git ls-files -s

Share:
187,923

Related videos on Youtube

Timon de Groot
Author by

Timon de Groot

Updated on August 19, 2022

Comments

  • Timon de Groot
    Timon de Groot over 1 year

    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.

    • houtanb
      houtanb over 8 years
      simply typing git status gives you a list of staged files, a list of modified yet unstaged files, and a list of untracked files.
  • walrus
    walrus over 6 years
    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.
  • Pistos
    Pistos about 5 years
    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.
  • Pacerier
    Pacerier almost 4 years
    re "last commit"; Misleading. Better to say git diff --cached is short for git diff --cached head
  • ibonyun
    ibonyun over 3 years
    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?
  • Peter Mortensen
    Peter Mortensen over 2 years
    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)?
  • Peter Mortensen
    Peter Mortensen over 2 years
    This does not answer the question and is completely wrong. It answers some other question. Can you delete it, please? Thanks in advance.
  • General Grievance
    General Grievance over 2 years
  • pyrsmk
    pyrsmk over 2 years
    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.

Related