When working with git it may often be convenient to get a (pure) list of files to give to some other command (e.g. astyle). This may be implemented as a trigger on the server but that has its pros and cons. Listing the modified files and processing them locally is the safest way as this gives the ability to revert or correct unwanted changes.

Here’s how we can list all the files currently changed, i.e. all the files in the working tree:
git diff --name-only

To list files which have been added to the index (“staged”), but not the currently modified files, do:
git diff --name-only --cached

To list both files in the index and the working trees:
git diff --name-only --cached | xargs ls -l && git diff --name-only | xargs ls -l

To give the files from the above list to ls for processing, e.g. if you want to see the sizes or the date stamps of the files:
git diff --name-only | xargs ls -l

To display a list of all files which are untracked:
git clean --dry-run | awk '{print $3;}'

Tagged with:

One Response to HowTo: Listing (changed/untracked) files in git

  1. Jonathan Gala says:

    These are handy commands, thanks.

    When running your last command (to show untracked files) I had instances where the raw output included “Would not remove …”, which meant the awk portion was listing “remove”.

    Might be nice to exclude those lines, like this:

    git clean –dry-run | grep -v ” not ” | awk ‘{print $3}’

Leave a Reply

Your email address will not be published. Required fields are marked *

Set your Twitter account name in your settings to use the TwitterBar Section.