How can I run "git status" and just get the filenames

GitVersion Control

Git Problem Overview


as opposed to the long relative path?

Git Solutions


Solution 1 - Git

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/^...//

Solution 2 - Git

I think cut is good for this.

git status -s | cut -c4-

Solution 3 - Git

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

Solution 4 - Git

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

Solution 5 - Git

git status will always walk down (edit and up) the tree and display relative paths. If you only want the file in the directory you are in, See this related answer

Solution 6 - Git

Get the name of modified files

git status --porcelain|awk '{if($1=="M") {print "basename " $2}}'|sh

I use similar script to copy my modified files to a remote server, as below:

git status --porcelain|awk '{if($1=="M") {print "scp " $2 " account_name@server_ip:~/my_codebase/$(dirname " $2 ")/;"} }'|sh

Solution 7 - Git

git status outputs relative paths so if you cd to the same directory as the file (under your working directory's root) before running git status it will only output the basenames of added/staged files.

cd /long/path/to/my/repo/root/dir
"stuff" > ./newfile.txt
> git status
# On branch master
# Changes not staged for commit:
#   (use "git add <file>..." to update what will be committed)
#   (use "git checkout -- <file>..." to discard changes in working directory)
#
#       modified:   /long/path/to/my/repo/root/dir/plus/some/more/levels/of/directory/structure/inside/it/changed_file.txt
#
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#       long/path/to/my/repo/root/dir/plus/some/even/more/levels/of/directory/structure/inside/it/but_another_new_file.txt
#       newfile.txt
no changes added to commit (use "git add" and/or "git commit -a")

ie, 'newfile.txt' is listed without the full path because you're in the same path as it.

Solution 8 - Git

In addition to the accepted answer, another way of doing it is with awk

git status --porcelain | awk '{ print $2 }'

The $2 selects second the column of each line.

Solution 9 - Git

If you're looking for only the filenames without the path mumbo-jumbo the following should work:

git status --porcelain | cut -c 3- | xargs basename -a

This script gets the shortened format of git status, cuts each line's 3rd column to the end and feeds it to the basename command which outputs only the filename from the full path.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionmyusuf3View Question on Stackoverflow
Solution 1 - GitMark LongairView Answer on Stackoverflow
Solution 2 - GitearlonrailsView Answer on Stackoverflow
Solution 3 - GitseheView Answer on Stackoverflow
Solution 4 - GitPeter BenjaminView Answer on Stackoverflow
Solution 5 - GitDan McClainView Answer on Stackoverflow
Solution 6 - GitYou-Liang ShihView Answer on Stackoverflow
Solution 7 - GitStassa PatsantzisView Answer on Stackoverflow
Solution 8 - GitGabrielView Answer on Stackoverflow
Solution 9 - GitShababb KarimView Answer on Stackoverflow