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

耗尽温柔 提交于 2019-12-01 03:37:48
for i in `git status --porcelain | grep '^D.*\.foo$' | sed 's/^D \+//'`; do
    git reset HEAD "$i"
    git checkout "$i"
done

If you are using Powershell the following will work.

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

This should work in cygwin and unix env

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

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.

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

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

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.

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.

Simply use git reset *mypattern*

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!