This post from @b0rk inspired me to document my favorite git
aliases. They’re published at @nonrational/dotfiles.
I’ve also included some cursory frequency analysis listing what commands and aliases I use most often.
Alias Groups
All my aliases live in .gitconfig
. Those that require multi-line shell operations delegate to .githelpers
. h/t to Gary Bernhardt’s dotfiles
for establishing this excellent pattern back in 2015.
Short Core Commands
There are ~6 commands that I use dozens of times a day, and I’ve shortened them to 1-2 characters each.
git st # `status`
git stp # `status --porcelain`
git ci # `commit`
git aa # `add --all`
git co # `checkout`
git pp # `push`
Fetching Remotes
git pb # Print the likely* remote origin primary branch; usually `main`, but not always.
git f # Fetch all remote branches, tags and prune deleted tags
git up # Requires `hub`. Fetch, then run `hub sync` to fast-forward all local branches to match the latest state on the remote
Staging Files
git aww # Stage all files matching a pattern, e.g., `git aww css`
git mod # List all modified file paths, for use with `xargs`, e.g., `git mod | xargs git add`
git del # List all deleted files
git new # List all new, untracked files
Pretty Logs
The value of log --graph
has been diminished since I started using squashed merge commits, but it’s still useful when working with multiple nested branches.
git r # Recent log for the current branch
git l # Long (paged) log for the current branch
git ra # Recent log for all branches
git la # Long (pages) log for all branches
Example Output
* 13f7f08 (5 months) <github-actions[bot]> (HEAD -> master, origin/master, origin/HEAD) Update .github/CODEOWNERS
* fec2a08 (11 months) <Sora Morimoto> Merge pull request #43 from mainendra/master
|\
| * bef06f3 (11 months) <Mainendra> Cleanup
| * 4a91207 (11 months) <Mainendra> Combined conditions
| * 607e4c7 (11 months) <Mainendra> Added support for aarch64
* | 9172559 (11 months) <Sora Morimoto> Merge pull request #39 from danielbodart/master
|\ \
| |/
|/|
| * 7e24eda (12 months) <Daniel Bodart> Add support for running in a windows bash prompt (i.e Git for Windows)
|/
| * f3cbb6c (1 year) <renovate[bot]> (origin/renovate/configure) Add renovate.json
|/
* 0e14a50 (1 year) <Sora Morimoto> Merge pull request #35 from benperiton/patch-1
|\
| * 2a8f4be (2 years) <Ben Periton> Update exec-env
* | ef5e9fb (1 year) <Sora Morimoto> Delete .github/dependabot.yml
|/
* 87442fe (2 years) <Sora Morimoto> Merge pull request #30 from asdf-community/dependabot/github_actions/actions/checkout-3
|\
| * bf496aa (2 years) <dependabot[bot]> Bump actions/checkout from 2 to 3
|/
* 3b58b46 (2 years) <Sora Morimoto> Make formatting available under the test directory
* 6685b18 (3 years) <Sora Morimoto> More cleanup
* 79500fe (3 years) <Sora Morimoto> Merge pull request #27 from kealjones-wk/patch-1
Working with PRs
git bro # Requires `gh`; BROwse - Open the current branch in a web browser.
git pro # Requires `gh`; Pull Request Open - Open the current branch's PR in the browser, using `gh`.
# If there is no current PR open, fall back to browse.
Working with Branches
git bc # Print the current branch name
git rio # Rebase Interactive Origin - Rebase the current branch interactively against `origin/main`.
git sww # SWitch Where - Switch (a.k.a. checkout) the most recently active branch matching a pattern.
git bcp # Branch Copy - Copy the current branch branch name to the clipboard.
git bmv # Branch Move - Rename the current branch
git bmx # Branch Move Pattern - Apply a sed-like command to rename the current branch.
git buu # Branch Unset Upstream - Unset the upstream for the current branch, if renaming a branch post-push.
git lb # List Branches by relative time since commit. Useful when juggling several branches in conjunction with `git sww`.
More “Ignore” Options
Sometimes I want to preserve a change locally that I never intend to commit.
git disregard # Ignore any changes to a tracked file.
git dissed # List all files that have been disregarded.
git attend # Stop ignoring changes to a file that was previously disregarded.
git is-it-just-me # Ignore an untracked file without adding it to `.gitignore`.
Keep Things Tidy
I often work on several branches at the same time and stale branches inevitably clutter up my local environment.
git uww # Unstage files matching a pattern
git nuke # Forcibly `reset` and `clean` everything
git broom # Remove all local branches with my personal prefix that do not exist on origin.
git spring-cleaning # Remove all branches with my personal prefix, both remote and local.
git everybody-out # Remove all local branches that do not begin with my personal prefix.
Keep Things Fun
git yolo # Commit everything (or nothing!) using a random message from whatthecommit.com
Frequency Analysis
My top 50 git commands, with aliases explained.
# Print the top 50 most frequently used `git` commands
history \
| awk '/[0-9] +git / {print $2,$3}' \
| tr -d ';' \
| sort | uniq -c | sort -r \
| head -n50
My top 50 git commands, with aliases explained.
6388 git st # Fewer characters to type than `status`.
1619 git ci # Fewer characters to type than `commit`.
1545 git r # Show the last 10 commits with `pretty_git_log`.
1507 git aa # Stage all changes in the current directory and subdirectories.
1469 git co # Fewer characters to type than `checkout`.
1277 git pp # Fewer characters to type than `push`.
932 git up # Use `hub sync` to fetch and fast-forward *all* local branches, not just the current one.
434 git f # Fetch everything from origin, including tags, and prune deleted branches.
287 git diff
250 git reset
241 git push
211 git nuke # Unstage and discard all changes, including deleting untracked files.
177 git merge
173 git add
141 git rio # Rebase the current branch interactively against origin/main.
139 git branch
132 git aww # Stage all modified or added files matching a pattern.
131 git stash
126 git sww # Switch to a branch matching a pattern.
99 git pull
90 git amend # Fewer characters to type than `commit --amend`.
86 git grep
85 git dfo # List only the files that differ between the current HEAD and origin/main.
82 git lb # List local branches with a relative modified date, recently modified first.
81 git broom # Remove all local branches that do not exist on origin.
78 git ri # Fewer characters to type than `rebase --interactive`.
56 git rebase
53 git show
45 git l # Show a paginated log with `pretty_git_log`.
26 git brr # List local branches, recently modified first.
20 git df # Fewer characters to type than `diff --name-only`.