Git aliases for a better workflow

tl;dr I have a set of aliases which speed up and simplify my daily workflow a lot.

Git offers to feature to add custom aliases. As I wrote in a previous post about bash aliases, I like not having to remember bulky commands.

The regular Git configuration file includes aliases. So in order to type git st instead of git status from now on, you may run the command git config --global alias.st "status" to add the alias to the global configuration file. Or edit the configuration file directly, which is located in ~/.config/git/config on Ubuntu.

My complete and up-to-date list of Git aliases is published on GitHub.

Shortcuts

st = status
br = branch
co = checkout
cm = commit
df = diff
com = checkout main
### remove however has a short name only ¯\_(ツ)_/¯
remove = rm

Catch typos

I catch common typos like git doff instead of git diff.

idff = diff
doff = diff
dif = diff
tcommit = commit
comimt = commit
megre = merge
emrge = merge
merte = merge
pish = push
psuh = push

Use speaking aliases for commands with angular arguments

### create new branch based on current branch: git copybranch <newname>
copybranch = checkout -b
### show all existing local and remote branches
remotebranches = branch -a
### show branches which are merged into the current branch already
mergedbranches = branch -a --merged
### add only selected parts to the next commit
addpartial = add -p
### change the last commit
amend = commit --amend
### remove connections to branches which are removed on remote origin already
pruneremote = remote prune origin
### rebase the last 9 commits of the current branch
rebaselatest = rebase -i --autosquash HEAD~9
### rebase all commits of the current branch
rebaseall = rebase -i --autosquash --root
### rebase branch against main/master
remaster = pull --rebase origin master
rebasemaster = pull --rebase origin master
rebasemain = pull --rebase origin main
### show the last commit
last = cat-file commit HEAD
### remove file from git
remove = rm

Enhance log output

### show a nicely readdable log
log-tree = log --graph --decorate --pretty=oneline --abbrev-commit
log-nice = log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit --date=relative
### show recent commits
log-short = log -9 --pretty=format:"%C(yellow)%h %Cred%ad %Cblue%an%Cgreen%d %Creset%s" --date=short
### show my own commits only
log-me = !git log --author $(git config user.email) --pretty=format:'%h %cd %s' --date=short
### show my own commits of yesterday
log-standup = !git log --since yesterday --author $(git config user.email) --pretty=short --oneline
### show my own commits of last week
log-retro = !git log --since '1 week ago' --author $(git config user.email) --pretty=short --oneline

Undo

A great feature of git is to reset and undo changes instantly… when you remember the correct arguments. I use git unadd <filename> instead of git reset HEAD -- <filename>.

### remove files from stage (marked for next commit): git unadd <filename>
unstage = reset HEAD --
unadd = reset HEAD --
### remove all files from stage
clearstage = reset HEAD
### run gits internal housekeeping tasks to reduce disk space etc
cleanup = gc
### remove all files which are not comitted already, like temporary files
removeuntrackedfiles = !git clean -f -d

Add complex commands

### remove branch locally and on origin remote: git removebranch branchname
removebranch = !sh -c 'git branch -D $1 && git push origin :$1' -
### Change author in last commit: git amendauthor "John Doe <doe@example.com>"
amendauthor = "!sh -c 'git commit --amend --author=\"$1\"' -"
### Set date in last commit to today
amenddatetonow = "!sh -c 'git commit --no-edit --amend --date=\"$(date -R)\"'"
### Show a diff of commit subjects for two branches: git showcommitdifference branchname
showcommitdifference = "!f() { git log --left-right --graph --cherry-pick --oneline ...${1-master}; }; f"

Show existing aliases

aliases = config --get-regexp alias

Shortcut routine commit messages

I have aliases for often used commit messages like linter fixes.

commitcgl = commit -m '[TASK] Apply to coding guidelines'

Update: I also have some bash aliases with shortcuts for git commands I use every minute. To make short commands even shorter.

The shortest status information

Instead of typing git status or my alias git st I only type st.

### status
alias st='git status'
### all branches
alias gb='git branch -a'
### all merged braches
alias gbm='git branch -a --merged'
### short list of the latest commits
alias gls='git log -7 --oneline'

Catch typos

And again, make typing errros like gut status work as well.

alias got='git'
alias gut='git'

⌛ Warning! This post is old. The information may be outdated.

No comments on this notepad. If you would like to add something, then don't hesitate to contact me via E-Mail, Mastodon or other social profiles.