Developer cheat sheets

The commands and patterns I reach for most: git, LINQ, EF Core, SQL, shell and HTTP.

Developer tools Code snippets

Git

git status -sbShort status with the branch
git switch -c feature/xCreate and switch to a new branch
git add -pStage changes piece by piece
git commit --amend --no-editAdd to the last commit without changing its message (only if not pushed)
git rebase -i HEAD~3Reorder, squash or reword the last three commits
git stash push -m "wip"Put work aside with a name
git log --oneline --graph --decorateCompact history as a graph
git restore --staged <file>Remove a file from the staging area
git reflogFind commits you thought were lost
git bisect startBinary search for the commit that broke something

C# and LINQ

items.Where(x => x.Active)Filter
items.Select(x => x.Name)Project to something else
items.OrderBy(x => x.Date).ThenBy(x => x.Id)Sort by several keys
items.GroupBy(x => x.Type)Group into buckets
items.Any(x => x.Late)Is there at least one match?
items.FirstOrDefault(x => x.Id == id)First match or null
items.Chunk(100)Split into batches of 100
a.Zip(b)Pair two sequences item by item
$"{price:N2} {date:yyyy-MM-dd}"Format numbers and dates in a string

EF Core

db.Items.AsNoTracking()Faster reads when you will not save changes
query.Include(x => x.Lines)Load related rows in the same query
query.AsSplitQuery()Avoid huge joins with several collections
query.ExecuteUpdateAsync(s => s.SetProperty(x => x.Count, x => x.Count + 1))Update in the database in one statement, safely under load
query.ExecuteDeleteAsync()Delete rows in one statement without loading them
dotnet ef migrations add NameCreate a migration from model changes
dotnet ef database updateApply pending migrations

SQL

SELECT … FROM … WHERE … GROUP BY … HAVING … ORDER BY …The order a query is written in
WITH x AS (SELECT …) SELECT … FROM xName a sub-query so the main one reads clearly
ROW_NUMBER() OVER (PARTITION BY a ORDER BY b)Number rows within each group
WHERE EXISTS (SELECT 1 FROM t WHERE …)Test for related rows, often faster than IN
COALESCE(a, b, 0)First value that is not null
CASE WHEN a > 0 THEN 'y' ELSE 'n' ENDIf-then-else inside a query

Shell and tools

grep -rn "text" .Search text in files, recursively, with line numbers
find . -name "*.log" -mtime +7Find log files older than seven days
tar -czf out.tar.gz folder/Pack a folder into a compressed archive
curl -i -X POST -d @body.json urlSend a POST with a JSON body and show the headers
Get-ChildItem -Recurse -Filter *.csPowerShell: list C# files recursively
netstat -ano | findstr :5000Windows: which process uses port 5000

SQL joins

JoinReturns
INNER JOINOnly rows with a match on both sides
LEFT JOINAll rows from the left, matches from the right (or null)
RIGHT JOINAll rows from the right, matches from the left (or null)
FULL JOINAll rows from both, matched where possible
CROSS JOINEvery row paired with every row

HTTP methods

MethodSafeIdempotentTypical use
GET✓✓Read a resource
HEAD✓✓Read only the headers
OPTIONS✓✓Ask what is allowed
POST✗✗Create something or start an action
PUT✗✓Replace a resource completely
PATCH✗✗Change part of a resource
DELETE✗✓Remove a resource

Check the documentation for your version before relying on a flag or an API. Rebase and amend rewrite history, so only use them on commits you have not shared.

↑ ↓ to choose, Enter to open, Esc to close

Rejoining the server...

Rejoin failed... trying again in seconds.

Failed to rejoin.
Please retry or reload the page.

The session has been paused by the server.

Failed to resume the session.
Please retry or reload the page.