Some one-liners and other useful scripts

A collection of commands and scripts I keep coming back to. Nothing groundbreaking, but saves me from Googling the same things over and over.

Uppercase a substring in a file

Need to find a pattern and make it uppercase? The \U modifier does the trick:

gsed -i.bak -E 's/(example)/\U\1/g' /path/to/file

One catch: macOS’s built-in sed doesn’t support \U. You’ll need GNU sed:

brew install gnu-sed

Fix wrong Git commit author

We’ve all been there — you push a bunch of commits only to realize they’re attributed to the wrong account. Maybe you forgot to set up the local config for a work repo.

git-filter-repo can fix this:

git filter-repo --force --commit-callback '
if commit.author_name == b"<OLD_AUTHOR_NAME>":
    commit.author_name = b"<NEW_AUTHOR_NAME>"
    commit.author_email = b"<NEW_AUTHOR_EMAIL>"
    commit.committer_name = b"<NEW_AUTHOR_NAME>"
    commit.committer_email = b"<NEW_AUTHOR_EMAIL>"
'

Install it first:

brew install git-filter-repo

Warning: This rewrites history. Don’t do this on shared branches without coordinating with your team.

Pro Tip: Prevent this by always setting repo-specific configs:

git config user.name "Work Username"
git config user.email "work@company.com"

Stage only tracked files

Sometimes you have new files you’re not ready to commit yet. This stages only modified and deleted files:

git add -u

Works with paths too: git add -u src/

Preview stash before applying

I never trust git stash pop. What if it’s the wrong stash? What if there are conflicts?

First, preview the changes:

git stash show -p           # full diff
git stash show -p stash@{2} # specific stash
git stash show              # just file names

Then apply (not pop!) and verify:

git stash apply

Only after confirming everything looks good:

git stash drop stash@{0}

Or if you want to nuke all stashes: git stash clear

Delete branch locally and remotely

The two-step dance:

git branch -d feature-branch
git push origin --delete feature-branch

Use -D instead of -d if the branch isn’t fully merged.

I got tired of typing both, so I added this to .gitconfig:

[alias]
    nuke = "!f() { git branch -d $1 && git push origin --delete $1; }; f"

Now it’s just git nuke feature-branch.

Generate random secrets

This trips people up: “32-character secret” and “32-bit secret” are very different things.

openssl rand -hex 4   # 32-bit  = 8 hex chars
openssl rand -hex 8   # 64-bit  = 16 hex chars
openssl rand -hex 16  # 128-bit = 32 hex chars
openssl rand -hex 32  # 256-bit = 64 hex chars

Each hex character encodes 4 bits. So a “32-character hex secret” is actually 128 bits — that’s 2¹²⁸ combinations vs 2³². Big difference.

Same thing in Python:

import secrets
secrets.token_hex(16)  # 32 hex chars = 128 bits

Or Node:

require('crypto').randomBytes(16).toString('hex')

Fix Colima after crash

Classic scenario: laptop dies from low battery, you plug it in, and Colima refuses to start. The network socket is stuck in a broken state.

pkill -9 -f "colima|lima|socket_vmnet|vde_vmnet" 2>/dev/null
rm -rf ~/.colima/_lima/_networks/
rm -f ~/.colima/_lima/colima/*.sock ~/.colima/_lima/colima/*.pid
colima start

This happens often enough that I have an alias:

alias colima-fix='pkill -9 -f "colima|lima" 2>/dev/null; rm -rf ~/.colima/_lima/_networks/ ~/.colima/_lima/colima/*.sock ~/.colima/_lima/colima/*.pid; colima start'

Fix PostgreSQL after crash

Same story with Postgres via Homebrew — a stale pidfile blocks startup.

The quick fix everyone knows:

rm -f /usr/local/var/postgresql@14/postmaster.pid
brew services restart postgresql@14

But what if Postgres is actually running? Deleting the pidfile would be bad. Here’s a safer version I use:

pg-fix() {
  local pidfile="/usr/local/var/postgresql@14/postmaster.pid"

  if [[ ! -f "$pidfile" ]]; then
    echo "No stale pidfile found"
    brew services restart postgresql@14
    return
  fi

  local pid=$(head -1 "$pidfile")
  if ps -p "$pid" > /dev/null 2>&1; then
    echo "PostgreSQL is actually running (PID $pid)"
    return 1
  fi

  echo "Removing stale pidfile..."
  rm -f "$pidfile"
  brew services restart postgresql@14
}

Checks if the process exists before touching anything.