gitfishgit-stash

Git stash: How to stash tracked files into a single stash via script?


I often do something like git add/checkout/stash -- (find -name "*.extension"). For example, to revert all text files or all images.

It works fine with add and checkout. When using add, untracked files are added, too. When using checkout or stash, untracked files are listed as errors ('<file>' did not match any file(s) known to git).

When using checkout, there's at least the workaround to do this:

for file in (find -name "*.extension")
    git checkout -- $file
end

But when using stash, this would create one new stash per file.

I would expect it to work in a way that git stash tracked-file untracked-file is the same as git stash tracked-file.

Is this possible? Or is there a workaround?


Solution

  • Git supports globs, so for the particular pattern in question you can simply use

    git stash -- "*.extension"
    

    If you want to use a more complicated pattern, then you could use git ls-files to get the list of files in the index and then filter that. You might find comm useful.