Jacko
Jacko

Reputation: 13205

GIT: I want to unstage all files matching a certain pattern

I would like to run

git reset *.foo

but this errors out.

I think I need to use a pipe, but I'm not sure how to do this.

Thanks!

Upvotes: 14

Views: 16901

Answers (10)

Annibale
Annibale

Reputation: 301

Now git restore is perfectly working, thus for me the easiest has been:

git restore '*.foo'

like in @César Noreña answer.

However, another pretty easy and very flexible way would be:

git diff --name-only --relative | grep '.foo' | xargs git restore

and it is flexible because you can use all grep options, or even replace grep with something else.

Upvotes: 10

César N.
César N.

Reputation: 499

You can try restore the files with git restore '*.foo'

Upvotes: 2

Sina Madani
Sina Madani

Reputation: 1324

Simply use git reset *mypattern*

EDIT: Also try git restore, but be VERY careful as it seems to be bugged at the time of writing.

Upvotes: 3

Daniel Sokolowski
Daniel Sokolowski

Reputation: 12488

White space in filename was causing problems using the git diff approaches but the following worked:

find ./ -name "*.foo" -exec git reset {} \;

Execution is verbose if there are many files to be unstaged.

Upvotes: 1

Johannes Fahrenkrug
Johannes Fahrenkrug

Reputation: 44836

If you want to checkout (undo changes) of unstaged modified files matching a given pattern, this works:

macOS:

git checkout $(git st -s | sed -E 's/^.{2}//' | grep '\.foo$')

Unix:

git checkout $(git st -s | sed -r 's/^.{2}//' | grep '\.foo$')

I've only tested this with M modified files. YMMV if you have renamed/deleted/conflicted files as well.

Upvotes: 0

kevins
kevins

Reputation: 482

E.g. I want to match all "migrations" in path.

git diff --name-only | grep migrations | xargs git checkout

Upvotes: 1

User123456
User123456

Reputation: 691

This should work in cygwin and unix env

git reset $(git diff --name-only --cached | grep *.foo)

Upvotes: 6

cdhowie
cdhowie

Reputation: 169468

for i in `git status --porcelain | grep '^D.*\.foo$' | sed 's/^D \+//'`; do
    git reset HEAD "$i"
    git checkout "$i"
done

Upvotes: 9

Mot
Mot

Reputation: 29600

In a Git GUI application like SmartGit I would filter the displayed files by the pattern *.foo, press Ctrl+A to select all the filtered files and invoke the Unstage command.

Upvotes: 2

JaredPar
JaredPar

Reputation: 755557

If you are using Powershell the following will work.

gci -re -in *foo | %{ git reset $_ } 

Upvotes: 7

Related Questions