Prepare git-checkout statements by using `awk` Linux

By k, 8 November, 2019

Having a status like this:

$ git status
modified:   composer.json
modified:   composer.lock
deleted:    config/webform.webform.foo.yml
deleted:    config/webform.webform.bar.yml
deleted:    config/webform.webform.bazyml
modified:   config/core.extension.yml

If I want to revert only `webform` changes by running something like,

$ git checkout config/webform.webform.*

It won't work because those are deleted files, then I can use `awk` to create `checkout` statements,

$ git status | grep webform | awk  '{ print "git checkout " $2; }'
git checkout config/webform.webform.foo.yml
git checkout config/webform.webform.bar.yml
git checkout config/webform.webform.baz.yml

Then copy and paste once and enjoy your wonderful day!

EDIT:

As a heads-up from a friend of mine, if we include double quotes it will work, as below:

git checkout "config/webform.webform.*"
Updated 3 paths from the index

PD:

Ha! An extra trick, if you include `| xargs -0 bash -c ` at the end, it will get execute it right away:

git status | grep webform | awk  '{ print "git checkout " $2; }' | xargs -0 bash -c                              
Updated 1 path from the index
Updated 1 path from the index
Updated 1 path from the index