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

10

14

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!

Fullblown answered 19/11, 2010 at 18:15 Comment(1)
Note: these files have been staged as deleted. i.e. the files are no longer in my working directory. So I guess I need something like git checkout *.fooFullblown
C
9
for i in `git status --porcelain | grep '^D.*\.foo$' | sed 's/^D \+//'`; do
    git reset HEAD "$i"
    git checkout "$i"
done
Corrigible answered 19/11, 2010 at 18:18 Comment(5)
Thanks. Now, the staged change was to delete these files. so, will this still work? the file is actually already deleted in the directoryFullblown
No, that will not work. I will update my answer with a new command.Corrigible
ahhhhh, I actually want to run this on files with pattern *.gen.csFullblown
Hmm, that should work. Can you verify that git status --porcelain outputs those files?Corrigible
git reset HEAD <file>; git checkout <file> is equivalent to git checkout HEAD <file>.Interdisciplinary
H
9

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.

Helaine answered 10/11, 2021 at 15:39 Comment(0)
M
7

If you are using Powershell the following will work.

gci -re -in *foo | %{ git reset $_ } 
Monomer answered 19/11, 2010 at 18:19 Comment(0)
S
6

This should work in cygwin and unix env

git reset $(git diff --name-only --cached | grep *.foo)
Shatzer answered 29/4, 2013 at 9:37 Comment(1)
Strangely with the asterisk this unstaged all files for me. Without the asterisk it complains about relative paths. This worked, git reset -- $(git diff --name-only --cached | grep .foo)Liquidation
C
3

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.

Clotilde answered 28/8, 2019 at 14:15 Comment(0)
J
2

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.

Jopa answered 19/11, 2010 at 18:23 Comment(0)
L
2

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

Lindbergh answered 19/10, 2021 at 19:55 Comment(0)
L
1

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

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

Libido answered 28/6, 2014 at 8:1 Comment(0)
C
1

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.

Comedietta answered 17/7, 2019 at 14:50 Comment(0)
F
0

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.

Fungus answered 11/1, 2018 at 16:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.