Stephen Ostermiller's Blog

Useful Git Aliases

Here is a list of useful aliases for git that can be used on the command line.

All of them for /etc/gitconfig or ~/.gitconfig

[alias]
    a = !git add -A && git commit -m
    ap =  "!f(){ git add -A && git commit -m \"$1\" && git push; };f "
    b = branch
    br = branch
    bra = branch
    cl = clone
    co = checkout
    cob = checkout -b
    com = commit
    ct = commit
    df = diff
    dif = diff
    hist = log --oneline --graph --decorate --all
    lg = log --pretty=format:\"%C(magenta)%h%Creset -%C(red)%d%Creset %s %C(dim green)(%cr) [%an]\" --abbrev-commit -30
    main = !git checkout main && git pull
    nb = !git checkout main && git pull && git checkout -b
    open = "!$(which xdg-open 2>/dev/null || which open  2>/dev/null || which start  2>/dev/null) $(git config remote.${1:-origin}.url | sed -E \"s#:([^:]+)\\$#/\\\\1#;s#^(ssh://)?[a-z]+@#https://#g;s#\\\\.git\\$##g;s#^([^a-zA-Z]|([a-zA-Z]+[^a-zA-Z:]))#https://\\\\1#g\")"
    pr = "!$(which xdg-open 2>/dev/null || which open  2>/dev/null || which start  2>/dev/null) $(git config remote.${1:-origin}.url | sed -E \"s#:([^:]+)\\$#/\\\\1#;s#^(ssh://)?[a-z]+@#https://#g;s#\\\\.git\\$##g;s#^([^a-zA-Z]|([a-zA-Z]+[^a-zA-Z:]))#https://\\\\1#g;s#\\$#/compare/main...$(git symbolic-ref --short HEAD)#g\")"
    recent = branch --sort=-committerdate --format=\"%(committerdate:short) %(align:25)%(authorname)%(end) %(refname:short)\"
    s = status
    st = status
    stat = status
    undo = reset --soft HEAD~1

Most of these are short abbreviatons so you don't have to type the entire git command, but a few of them are more complicated and are explained in detail below:

Add and commit with a commit message

Use case: you want to commit everything.

Without the alias: git add -A && git commit -m 'Feature X'

With the alias: git a 'Feature X'

Installing the alias: git config --global alias.a '!git add -A && git commit -m'

Add, commit with a commit message, and push to origin

Use case: you want to commit everything and push your work

Without the alias: git add -A && git commit -m 'Feature X' && git push

With the alias: git ap 'Feature X'

Installing the alias: git config --global alias.ap '"!f(){ git add -A && git commit -m \"$1\" && git push; };f "'

Show the git log with branch history graphed

Use case: you want to see the the git log with colors showing which commits were made on branches.

Without the alias: git log --oneline --graph --decorate --all

With the alias: git hist

Installing the alias: git config --global alias.hist 'log --oneline --graph --decorate --all'

Show the git log with colors

Use case: prettier git log

Without the alias: git log --pretty=format:\"%C(magenta)%h%Creset -%C(red)%d%Creset %s %C(dim green)(%cr) [%an]\" --abbrev-commit -30

With the alias: git lg

Installing the alias: git config --global alias.lg '!git log --pretty=format:\"%C(magenta)%h%Creset -%C(red)%d%Creset %s %C(dim green)(%cr) [%an]\" --abbrev-commit -30'

Create a new branch with the latest main

Use case: Starting a new feature branch and you want to make sure you have the latest changes from upstream

Without the alias: git checkout main && git pull && git checkout -b my-branch-name

With the alias: git nb my-branch-name

Installing the alias: git config --global alias.nb '!git checkout main && git pull && git checkout -b'

Open the repository in your web browser

Use case: You want to see the repository's origin in your web browser, for example on Github.

Without the alias: Easiest just to bookmark it

With the alias: git open

Installing the alias: git config --global alias.open '"!$(which xdg-open 2>/dev/null || which open 2>/dev/null || which start 2>/dev/null) $(git config remote.${1:-origin}.url | sed -E \"s#:([^:]+)\\$#/\\\\1#;s#^(ssh://)?[a-z]+@#https://#g;s#\\\\.git\\$##g;s#^([^a-zA-Z]|([a-zA-Z]+[^a-zA-Z:]))#https://\\\\1#g\")"'

Open the pull request URL in your web browser

Use case: Create or view the pull request webpage, for example on Github

Without the alias: Easiest to search on Github

With the alias: git pr

Installing the alias: git config --global alias.pr '"!$(which xdg-open 2>/dev/null || which open 2>/dev/null || which start 2>/dev/null) $(git config remote.${1:-origin}.url | sed -E \"s#:([^:]+)\\$#/\\\\1#;s#^(ssh://)?[a-z]+@#https://#g;s#\\\\.git\\$##g;s#^([^a-zA-Z]|([a-zA-Z]+[^a-zA-Z:]))#https://\\\\1#g;s#\\$#/compare/main...$(git symbolic-ref --short HEAD)#g\")"'

View recently touched branches

Use case: View the list of branches, ordered by newest first

Without the alias: git branch --sort=-committerdate --format="%(committerdate:s hort) %(align:25)%(authorname)%(end) %(refname:short)"

With the alias: git recent or git recent -a to see all recent branches including remote branches

Installing the alias: git config --global alias.recent 'branch --sort=-committerdate --format=\"%(committerdate:short) %(align:25)%(authorname)%(end) %(refname:short)\"'

Undo the last commit

Use case: You committed the wrong thing but haven't pushed yet. You want to undo the commit and put the committed files back in the working set without losing the changes.

Without the alias: git reset --soft HEAD~1

With the alias: git undo

Installing the alias: git config --global alias.undo 'reset --soft HEAD~1'

Leave a comment

Your email address will not be published. Required fields are marked *