A starter set of Git aliases
Short names for the Git commands you type all day, such as a one-line status, a graph log, and a safe force push. One bash script adds them all to your global config.
Not checked by a trusted editor yet Last edited Outdated?
What this changes
Adds or overwrites 11 aliases (st, sw, swc, lg, last, unstage, amend, undo, pushf, recent, aliases) in your global Git config, ~/.gitconfig. Nothing else is changed.
How to undo it
Remove one with git config --global --unset alias.st. git config --global --remove-section alias removes all aliases at once, including ones you had before, so back up ~/.gitconfig first.
A Git alias is a shortcut: after git config --global alias.st "status --short --branch", typing git st runs the longer command. Aliases are stored in the [alias] section of your global .gitconfig, so they work in every repository.
The aliases
git st: compact status with the branch name and how far ahead or behind the remote it is.git sw/git swc name: switch to a branch / create one and switch to it.git lg: one line per commit, as a graph, across all branches.git last: the latest commit with the files it changed.git unstage file: take a file out of the staging area and keep the edits.git amend: add staged changes to the last commit without editing its message. Use it only before pushing.git undo: undo the last commit and keep its changes staged.git pushf: force push that refuses to overwrite commits you have not seen.git recent: local branches sorted by most recent commit, with how long ago.git aliases: list every alias you have defined.
The script
#!/usr/bin/env bash
set -euo pipefail
add_alias() {
git config --global "alias.$1" "$2"
printf ' git %-8s -> git %s\n' "$1" "$2"
}
echo "Adding Git aliases:"
add_alias st "status --short --branch"
add_alias sw "switch"
add_alias swc "switch -c"
add_alias lg "log --oneline --graph --decorate --all"
add_alias last "log -1 --stat"
add_alias unstage "restore --staged"
add_alias amend "commit --amend --no-edit"
add_alias undo "reset --soft HEAD~1"
add_alias pushf "push --force-with-lease"
add_alias recent "branch --sort=-committerdate --format='%(committerdate:relative)%09%(refname:short)'"
add_alias aliases "config --get-regexp ^alias\\."
echo "Done. Try: git st"
Run it with bash git-aliases.sh. Running it again just sets the same values, and any aliases of your own with other names are left alone.
Try them
git st
git lg
git recent
git aliases
Change or remove an alias
git config --global alias.lg "log --oneline --graph --decorate"
git config --global --unset alias.undo
You can also open the file directly with git config --global --edit.
Good to know
- Aliases cannot replace built-in commands: an alias named
statusis ignored. - Arguments after an alias are passed through, so
git lg -5shows five commits. - An alias starting with
!runs as a shell command, for examplegit config --global alias.root "!pwd". Be careful with those, because they can run anything. - Aliases are personal. In shared docs and scripts, write the full command so everyone can follow it.
Did this work for you?
No votes yet. Be the first.
If it did not work, the report form below tells the editors what went wrong.
Repos in the wild
- git/git git/git The source code of Git itself, mirrored on GitHub. Useful for reading the official documentation sources and seeing how commands are implemented. GitHub
- lazygit jesseduffield/lazygit A terminal user interface for Git written in Go. Stage lines, rebase, resolve conflicts, and browse history with keyboard shortcuts instead of long commands. GitHub
- GitHub CLI (gh) cli/cli GitHub's official command-line tool. Create and check out pull requests, manage issues, view Actions runs, and sign in to GitHub from the terminal. GitHub
- git-extras tj/git-extras A collection of extra Git subcommands for common chores, such as summarizing a repository, deleting merged branches, and undoing commits. GitHub
History
Every saved edit is kept. The plot on the sleeve draws one line per edit. Open any edit to see exactly what changed.
- Commitpedia +2,736 chars