How can I run "git status" and just get the filenames
The output of git status --porcelain
, designed to be easy to parse in a script, outputs the full paths rather than relative paths regardless of where your current directory is within the tree.
Each line output by git status --porcelain
has two leading characters indicating the status of the file (e.g. whether it's untracked, modified, new, deleted, etc.) followed by a space, so if you just want the full paths of everything that would be mentioned in the output of git status
you can do:
git status --porcelain | sed s/^...//
I think cut
is good for this.
git status -s | cut -c4-
Aha, I think I just understood the question: you want basenames? Tack on | while read a; do basename "$a"; done
to any of the following:
how about
-
git diff --name-only
for changes relative to index
-
git diff --name-only --staged
for ... well staged chages :)
-
git diff --name-only HEAD
got both
A much simpler solution that is built-in to git
using ls-files
.
From the docs:
OPTIONS
-c --cached Show cached files in the output (default)
-d --deleted Show deleted files in the output
-m --modified Show modified files in the output
-o --others Show other (i.e. untracked) files in the output
-i --ignored Show only ignored files in the output. When showing files in the index, print only those matched by an exclude pattern. When showing "other" files, show only those matched by an exclude pattern. Standard ignore rules are not automatically activated, therefore at least one of the --exclude* options is required.
-s --stage Show staged contents' mode bits, object name and stage number in the output.
-u --unmerged Show unmerged files in the output (forces --stage)
Example showing flags can be combined as well:
git ls-files -dmo