More in Git
Undo the last commit but keep the changes
Git
Moves HEAD back one commit and leaves everything staged, which is what you usually want after committing too early.
Find the commit that introduced a bug
Git
Binary search across history. Mark a known good and a known bad commit and git narrows it down for you.
Amend a commit without changing its message
Git
Adds forgotten files to the previous commit and leaves the message exactly as it was.
Recover a branch you deleted
Git
The reflog remembers where HEAD has been for about ninety days, so a deleted branch is almost never really gone.
Search the whole history for a string
Git
Finds every commit whose diff added or removed a piece of text, including in files long since deleted.
Stop tracking a file without deleting it
Git
For the config file that should never have been committed. Add it to .gitignore afterwards.
Show what a merge will do before doing it
Git
Lists the commits and the files a merge would bring in, so a surprise is less likely.
Random picks
Open an SSH tunnel to a remote database
Networking
Forwards a local port through a bastion so a client can connect as if the database were local.
Find what is taking up space in an image
Containers
Prints the size each layer adds, which usually points straight at the offending RUN line.
Count lines of code by extension
Shell
A quick census of a repository without installing a tool for it.
Generate a strong random secret
Security
For session keys and tokens. Never reach for a password generator or a UUID for this.
Retry a fetch with exponential backoff
JavaScript
Backs off on each failure and adds jitter, so a recovering service is not hit by every client at the same moment.
A dataclass with validation
Python
__post_init__ runs after the generated constructor, which is where cheap invariants belong.
Show which process is holding a port
Shell
Answers 'address already in use' without guessing.
Prepared statement with PDO
PHP
Placeholders rather than interpolation. This is the whole of SQL injection defence in PHP.