Trick grep / egrep to always display filename

If you use grep / egrep on a single file, it will display the matched patterns, but not the filename nor the line number.

This can be boring in case you encapsulate a grep / egrep call inside i.e a find:

find ./mydir -type f -name '*.msg' -exec grep 'mysearch' {} \;

In that case it will only print the match and you will not know in which file.

The trick is to add a dummy filename as a second file to search. Doing so will trick grep / egrep into thinking you are searching in multiples files, thus displaying the filename on each match.
/dev/null is the perfect target for the second dummy file as it will never match anything.

Example:

find ./mydir -type f -name ‘*.msg’ -exec grep ‘mysearch’ {} /dev/null \;

Bonus / reminder:

In grep / egrep use the -n option to also display the match line number

Example:

find ./mydir -type f -name ‘*.msg’ -exec grep -n ‘mysearch’ {} /dev/null \;

#sysadmin #tips #tricks #grep #egrep

Git remove commit from a pushed pull request

If ever you have to remove a commit from a pushed pull request, here is a simple way to do it. Bonus: it’s not adding a commit for the removal.

First be sure to be on the good branch

git checkout target-pull-request-branch

Then have a watch at the PRs commit tab and note the commits hash you want to remove (one or more)

Replace X by that number in the following command, which will make and interactive rebase for the X last commits

git rebase -i HEAD~X

You are now going to have an interactive text view, in which all the aimed commits are preceded by the word ‘pick’

Using the view like a text editor, replace the ‘pick’ word by ‘drop’ on the lines containing the targeted commit’s hashes .

Exemple

Replaced pick with drop for commits 4aa3149 and 2031a79b:


Save and exit

In my case vim was the default text editor, so here you go escape colon w q

Push force the rebased repository

git push --force

This will remove the targeted commits and the only trace will be in the PR comments. The commit page will look like the rebased one, without the discarded commits nor a mention to them.