brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: git aliases turn your long commands into short ones

Git
~/tips/tip-git-alias-shortcuts

git config --global alias.last "log -1 --stat"

Defines your own git subcommands for commands you type often.

Some git commands you type twenty times a day, with the same options every time. An alias gives them a short name that works in every repository.

git config --global alias.last "log -1 --stat"
git config --global alias.unstage "restore --staged"

Now git last shows the last commit with its files and git unstage file.txt takes a file out of the index. Extra arguments are added at the end, so git last HEAD~2 works too.

An alias that starts with an exclamation mark runs as a shell command instead of a git subcommand. That lets you chain things:

git config --global alias.cleanup '!git fetch -p && git branch -vv'

Shell aliases run from the top of the repository, not from the folder you’re in, which matters when the command uses relative paths. GIT_PREFIX holds the folder you started in if you need it.

All aliases live in the [alias] section of ~/.gitconfig, and git config --get-regexp ^alias lists them.

You can’t override a built-in command. An alias named log or status is silently ignored, which I find easy to forget. Also keep in mind that aliases are personal. A colleague doesn’t have your git last, so in docs and scripts for others, write the full command.

Further reading: git-config documentation: alias, Pro Git: Git Aliases

More Git tips git show-branch main feature git log --stat -5 git merge --no-ff feature -m "Merge feature" git merge --squash feature git merge --abort All Git tips (144) All topics

Link