Skip to content

Instantly share code, notes, and snippets.

@antonkomarev
Last active May 18, 2026 11:11
Show Gist options
  • Select an option

  • Save antonkomarev/7195a4b0cd63cf4e2515256050c7e895 to your computer and use it in GitHub Desktop.

Select an option

Save antonkomarev/7195a4b0cd63cf4e2515256050c7e895 to your computer and use it in GitHub Desktop.
Useful git aliases to speed up development
[alias]
f = "!f(){ b=$(git default); cur=$(git symbolic-ref --short HEAD 2>/dev/null); if [ \"$cur\" = \"$b\" ]; then git pull --ff-only; else git fetch origin $b:$b; fi; }; f"
go = "!f(){ branch=${1:-$(git default)}; git switch \"$branch\" && git pull && git cleanup; }; f"
rh = "!git rebase -i HEAD~$(git count)"
ru = "!b=$(git default) && git rebase -i $b"
ra = "!git rebase --abort"
pf = "!git push --force-with-lease"
nah = "!f(){ git reset --hard; git clean -df; if [ -d \".git/rebase-apply\" ] || [ -d \".git/rebase-merge\" ]; then git rebase --abort; fi; }; f"
# === secondary tool functions ===
# Resolves the remote's default branch (main/master/...)
# Requires one-time setup per repo: git remote set-head origin --auto
default = "!git symbolic-ref --short refs/remotes/origin/HEAD 2>/dev/null | sed 's@^origin/@@' || echo master"
cleanup = "!git fetch -p && git for-each-ref --format='%(refname:short) %(upstream:track)' refs/heads | awk '$2==\"[gone]\"{print $1}' | while read -r b; do git branch -D \"$b\"; done"
count = "!b=$(git default) && git rev-list --count HEAD ^$(git merge-base HEAD $b)"

All commands below are mnemonics — each letter stands for a word, so the alias hints at what it does (e.g. pf = push force, ru = rebase upstream).

git go — Get Origin

  1. Switches to a selected branch (default branch by default — main or master, auto-detected)
  2. Pulls changes from the remote server
  3. Deletes all local branches which were deleted remotely
git go

Same command, but with another branch name to switch to:

git go feature/custom-branch-name

git f — Fetch

Fetches the latest changes for the default branch (main/master) from the remote.

  • If you are currently on the default branch, runs git pull --ff-only to update your working tree.
  • If you are on any other branch, fast-forwards the local default branch without switching to it — so you can rebase onto an up-to-date base without leaving your feature branch.
git f

git rh — Rebase Head

Opens an interactive rebase for all commits in the current branch that are not yet in the default branch, automatically calculating the number of commits to rebase.
Useful for squashing or editing only your new commits.

git rh

git ru — Rebase Upstream

Opens an interactive rebase of the current branch on top of the default branch, using it as the fixed base.
All commits in the branch will be reordered relative to the default branch.

git ru

git ra — Rebase Abort

Aborts an in-progress rebase and returns the branch to its pre-rebase state.

git ra

git pf — Push Force

Force push with --force-with-lease flag — safer than plain --force because it refuses to overwrite remote commits you haven't seen.

git pf

git nah — nah, forget it

Discards all local changes and aborts any in-progress rebase:

  1. Resets the working tree to HEAD (git reset --hard)
  2. Removes untracked files and directories (git clean -df)
  3. If a rebase is in progress, aborts it
git nah

Secondary / helper commands

git default

Prints the name of the remote's default branch (main, master, etc.).
Used internally by the other aliases so they work regardless of branch naming convention.
Requires a one-time setup per repo so Git knows which branch is the default:

git remote set-head origin --auto

If the remote HEAD is not set, falls back to master.

git default

git cleanup

Prunes remote-tracking references and deletes all local branches whose upstream has been removed (shown as : gone] in git branch -vv).
Called automatically by git go, but can be run on its own.

git cleanup

git count

Prints the number of commits on the current branch that are not yet in the default branch.
Used internally by git rh to figure out how far back to rebase.

git count
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment