how to use regex in git checkout command to specify file subsets
Say I am trying to checkout a certain set of git file, e.g. app/*^(.tmpl).html
,
how to throw this regex into git checkout branch -- (regex)
As mentioned in "Is there a way to use wildcards with git checkout
?"
Git does not deal with the wildcard, but your shell does.
As noted below, you would need to escape the wildcard character in order for git to interpret it: afile\*
, or use simple quotes.
The example section of git checkout
shows a wildcard usage:
rm hello.c
git checkout -- '*.c'
Note the quotes around
*.c
.
The filehello.c
will also be checked out, even though it is no longer in the working tree, because the file globbing is used to match entries in the index (not in the working tree by the shell).
An alternative, for instance, would be to use find (as in "“git add *.js
” did not add the files in sub-directories")
find . -name '*js' -exec git checkout {} \;
You can try your regex with find -regex
For instance, Tukaz uses (from the comments):
find ./packages/* -maxdepth 1 -name 'package.json' \ -exec git checkout {} \;
in order to checkout only the
package.json
of each project I have and omitdist
ornodemodules
sub-folders.
The more modern command (since 2014) would be the Git 2.23+ (Q3 2019) git restore
:
git restore -s=aBranch -- '*.c'
With '*.c' following the glob pattern described in pathspec
.
Just updating this answer but I was able to git checkout
with a wildcard so I believe this is possible now. I am running git version 1.9.3.
Git's pathspec specials are poorly advertised, but they're quite capable:
git checkout ':!*.tmpl.*' app/\*.html