How to make git status show only staged files
Asked Answered
S

7

109

I would like to get a list of only the staged filenames. I can't find the equivalent flag for --name-only for the git status command. What is a good alternative?

The file list will be piped to php -l (PHP lint syntax checker).

Solution: the complete command

git diff --name-only --cached | xargs -l php -l
Sorb answered 24/12, 2010 at 7:52 Comment(2)
If you're running that command manually, it sounds like you might want to put it into a pre-commit hook. kernel.org/pub/software/scm/git/docs/githooks.htmlGangplank
I think this is a better answer to this https://mcmap.net/q/41279/-is-it-possible-to-git-status-only-modified-filesBesmear
S
161

Use git diff --name-only (with --cached to get the staged files)

Selfimportant answered 24/12, 2010 at 7:59 Comment(1)
Instead of --name-only you can use --name-status that also displays if the file was modified, added or deleted.Apply
D
27

The accepted answer won't let you know what kind of changes were there.

Yes, If you are not syntax checker but an ordinary person with a repository full of unstaged files, and you still want to know what will happen to staged files - there is another command:

git status --short | grep '^[MARCD]'

which leads to something like:

M  dir/modified_file
A  dir/new_file
R  dir/renamed -> dir/renamed_to
C  dir/copied_file
D  dir/deleted_file

Obviously, this files were staged, and after git commit:
deleted_file will be deleted,
new_file will be added,
renamed_file will become a renamed_to.

Here is an explanation of short-format output: https://git-scm.com/docs/git-status#_short_format

Dolley answered 6/6, 2018 at 13:54 Comment(3)
1. in my version (2.25.0.windows.1) there's a space before "M" and "D". 2. There's also another state - "??" which I'm not sure how to interpret.Thallic
@Thallic the space indicates that it is not staged. This answer is correct because it matches on the first character in the line. If the first char is set then it's a staged file. If it's a space it's ignored.Barometer
This is genius! - there should be a git flag for similar thingEmrick
E
9

Inspired by @coffman21's answer I have setup the following alias in my .zshrc

alias gst="git status"
alias gst-staged="git status --short | grep '^\w.'"
alias gst-unstaged="git status  --short | grep '^\W.'"
alias gst-unstaged-tracked="git status  --short | grep '^\s.'"
alias gst-untracked="git status --short | grep '^??'"

or

alias gst="git status"
alias staged="git status --short | grep '^\w.'"
alias unstaged="git status  --short | grep '^\W.'"
alias unstaged-tracked="git status  --short | grep '^\s.'"
alias untracked="git status --short | grep '^??'"

It might be of use to anyone else. So adding it to the stack of answers.

Emrick answered 9/9, 2020 at 21:2 Comment(3)
for consistent coloring you can add a . in grep, to color both 2 initial chars, like: grep '^\w.', grep '^\W.', grep '^\s.'Dorisdorisa
no need on the last one, though; it already has two chars ??Dorisdorisa
That has now been fixed. Thanks againEmrick
C
5

to view staged files with code changes

git diff --staged   

or using --cached which is synonym for --staged

git diff --cached

or to view only file names without code changes

git diff --staged --name-only  

git-diff manual

Cytosine answered 14/2, 2020 at 14:41 Comment(0)
O
2

To view which files are staged ,

git ls-files
Orthodontics answered 27/2, 2021 at 7:11 Comment(1)
This seems to only show committed files in the current directory, not staged.Eschatology
D
2

Show only staged files

git status --porcelain --untracked-files=all | grep '^[A|M|D|R]'
  • --porcelain for parsing-friendly output
  • --untracked-files=all show all "untracked" files. Shows the files that are staged for commit.
  • grep '^[A|M|D|R]' filter the output for files that are
    • ^ Match from the start of a newline. The first character of a line indicates the status in the staging area, the second in the working tree.
    • A added
    • M modified
    • D deleted
    • R renamed

This was based on this comment

Dirigible answered 5/6, 2021 at 0:0 Comment(0)
T
0

from @velocity
git diff --staged is exactly what I wanted. If anyone is looking to make this into a shortcut like git ds in your bashrc please see this example:

git() {
    if [[ $@ == "ds" ]]; then
        command git diff --staged
    else
        command git "$@"
    fi
}
Thresathresh answered 3/11, 2021 at 21:44 Comment(1)
Git has its own aliasing functionality -- you can add [alias] (newline) ds = diff --staged to ~/.gitconfig instead of doing this bashrc method.Inhumanity

© 2022 - 2024 — McMap. All rights reserved.