as opposed to the long relative path?
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
I think cut is good for this.
git status -s | cut -c4-
To get just the filenames, as you requested:
git status --porcelain | sed -e 's!.*/!!'
I cannot see how this would be useful.
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
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/^...//
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.