Engineering
The git aliases worth stealing
My `[alias]` block has grown to a few hundred lines over the years, and most of it is junk I wrote once and never used again.
My [alias] block has grown to a few hundred lines over the years, and most of it is junk I wrote once and never used again. The honest list, the aliases I'd miss, is short. Here are the ones that earn their keep, grouped by the job they do.
A git alias goes in your gitconfig under [alias]. Anything starting with ! runs as a shell command, which is how the more interesting ones work.
The one that prevents a disaster
If you steal nothing else, steal this:
enforce = push --force-with-lease
git push --force overwrites whatever is on the remote, including commits a teammate pushed thirty seconds ago that you never fetched. --force-with-lease refuses to push if the remote moved since you last saw it. It force-pushes your rewrite only when you're overwriting exactly the history you think you're overwriting. Aliasing it to something deliberate like enforce means you never reflexively type the unsafe version. After a rebase, git enforce is the move.
A log you can read
The default git log is a wall of text. These turn it into a graph:
lol = log --graph --decorate --date=relative --oneline
lola = log --graph --decorate --date=relative --oneline --all
lol shows the current branch as a graph with relative dates. lola shows every branch, which is how you see how things have diverged before you merge or rebase. If you want author and timestamp detail, a longer pretty-format version is worth keeping around too, but lol and lola cover ninety percent of the times you reach for the log.
Undo, without panic
Two aliases that map to the two things "undo" usually means:
unstage = reset HEAD
uncommit = reset --soft HEAD^
unstage reverses a git add and leaves your changes alone. uncommit removes the last commit but keeps every change in your working directory, which is exactly what you want when you committed too early or with the wrong message. Note the --soft. It moves the branch pointer back without touching your files, so nothing is lost.
Amend, every flavor
You almost never want plain --amend. You usually want one specific variant, so name them:
amend = commit --amend
amendfile = commit --amend --no-edit
amendfile adds your staged changes to the previous commit without reopening the message editor, which is the one you'll use most: you forgot a file, you stage it, you fold it in, you move on.
See what's coming and going
Before you pull or push, it's nice to know what's about to happen:
incomming = "!git remote update -p; git log ..@{u}"
outgoing = log @{u}..
incomming shows commits on the upstream that you don't have yet. outgoing shows commits you have that the upstream doesn't, the ones a push would send. Two questions you should ask before every sync, and now they're one word each.
Park a file git keeps re-touching
Sometimes a tracked file has local tweaks you never want to commit, like a config with your own values. Tell git to stop noticing:
ignore = update-index --assume-unchanged
unignore = update-index --no-assume-unchanged
git ignore path/to/config makes git pretend the file is unchanged. This is not .gitignore, which is for untracked files. It's a local-only flag for tracked files, and it lives only in your repo. Use it for that one settings file, not as a general habit, because a future you will forget it's set and wonder why a change won't stage.
Quick work-in-progress commits
For when you need to stash work into history fast, usually before switching context:
wip = "!git add -A && git commit -m \"WIP - $(git rev-parse --abbrev-ref HEAD)\""
git wip stages everything and commits it with a message tagging the current branch. It's a deliberately ugly commit you'll squash or amend later, not something that survives to the pull request.
The point of an alias isn't to save keystrokes. It's to make the safe version of a command the one your fingers reach for by default, and to turn a multi-flag incantation you'd otherwise google into a single word you trust. Steal the ones that do that. Skip the rest, including most of mine.