DEV Community

Cover image for What are your git aliases?
Jose Angel Munoz
Jose Angel Munoz

Posted on • Updated on

What are your git aliases?

Introduction

I've been requested multiple times about sharing my .gitconfig to copy my aliases.

I use git aliases for two reasons:

  1. To improve productivity.
  2. To remember interesting git commands and learn from them.

My Gitconfig

These are my aliases included in my ~/.gitconfig file for your reference:

[alias]
    a = add .
    aliases = config --get-regexp alias
    alias = ! git config --get-regexp ^alias\\. | sed -e s/^alias.// -e s/\\ /\\ $(printf \"\\043\")--\\>\\ / | column -t -s $(printf \"\\043\") | sort -k 1
    ap = add . -p
    addups = remote add upstream
    bd = branch -d
    bi = bisect
    bl = branch -l
    blr = branch -a
    br = branch -r
    ca = commit -a
    cam = commit -a -m
    ci = commit -m
    cia = commit --author='imjoseangel <anotheremail@example.com>' -m
    cm = commit
    co = checkout
    colast = checkout -
    comments = commit -m 📒Comments
    count = rev-list --count devel
    db = branch -D
    forgetAbout = rm --cached
    formatting = commit -m 💅Formatting
    fp = fetch -p
    grep = grep -F
    laf = fsck --lost-found
    last = log -1 HEAD
    latest = log -5 --pretty --oneline
    ls = ls-files --others --exclude-standard -z
    mend = commit --amend
    nb = checkout -b
    op = gc --prune=now --aggressive
    pdo = push -d origin
    pf = push --force-with-lease
    po = push origin
    pou = push --set-upstream origin
    pr = pull --rebase
    pror = remote prune origin
    prud = pull --rebase upstream devel
    prum = pull --rebase upstream main
    prune = remote update --prune
    ptag = push origin --tags
    ra = rebase --abort
    rc = rebase --continue
    refactor = commit -m 👷Refactor
    remotes = remote -v
    renb = branch -m
    rh = reset --hard
    rhh = reset --hard HEAD
    ri = rebase -i upstream/devel
    rim = rebase -i upstream/main
    rl = reflog
    rp = repack -ad
    s = status -s
    search = rev-list --all
    sh = show
    short = shortlog -sn
    sign = commit --amend --no-edit --signoff
    st = status
    stashes = stash list
    tests = commit --allow empty -m ✅Tests
    tuto = help tutorial
    tuto2 = help tutorial-2
    unstash = stash pop
    vc = clean -dfx
    wow = log --all --graph --decorate --oneline --simplify-by-decoration
Enter fullscreen mode Exit fullscreen mode

Running git alias after adding to the .gitconfig shows the list of all the aliases as a reference list.

To get more info, just run git help <command or alias>. For instance:

git help st
'st' is aliased to 'status'
Enter fullscreen mode Exit fullscreen mode
git help status
Enter fullscreen mode Exit fullscreen mode

There are two aliases I find interesting for beginners:

git tuto
git tuto2
Enter fullscreen mode Exit fullscreen mode

Comments and suggestions with different approaches are always welcomed.

Latest comments (48)

Collapse
 
monikaderlutzki profile image
Monika Derlutzki

I dont use any :))

Collapse
 
imjoseangel profile image
Jose Angel Munoz

xD

Collapse
 
vikmstr profile image
Viktor Krejčíř

general ones:

alias gco="git checkout"
alias gpsp="git push -o ci.skip"
alias gswc="git switch -c"
alias gmwps="git push -o merge_request.create -o merge_request.target=master -o merge_request.merge_when_pipeline_succeeds"
alias gdfw="git diff --word-diff"
alias gmrt="git merge --strategy recursive -X theirs"
alias gbr="git branch --remotes --verbose --sort=-committerdate"
alias gbl="git branch --verbose --sort=-committerdate"
alias gps="git push"
alias gpl="git pull --rebase"
alias gpom="git push origin master"
alias gpomn="git push origin main"
alias gf="git fetch"
Enter fullscreen mode Exit fullscreen mode

And some specific commit messages (conventional commit is a thing):

alias gcsmb='f() { git commit -S -m "[BUILD]: $1"  };f'
alias gcsmc='f() { git commit -S -m "[CHORE]: $1"  };f'
alias gcsmf='f() { git commit -S -m "[FEAT]: $1"  };f'
alias gcsmfx='f() { git commit -S -m "[FIX]: $1"  };f'
alias gcsmd='f() { git commit -S -m "[DOCS]: $1"  };f'
alias gcsmt='f() { git commit -S -m "[TESTS]: $1"  };f'
Enter fullscreen mode Exit fullscreen mode
Collapse
 
imjoseangel profile image
Jose Angel Munoz

Thanks for sharing @vikmstr !!!

Collapse
 
vikmstr profile image
Viktor Krejčíř

using zsh

Collapse
 
hasii2011 profile image
Humberto A Sanchez II

Really like this discussion. Added many of these to my .gitconfig

Collapse
 
imjoseangel profile image
Jose Angel Munoz

Thanks @hasii2011

Collapse
 
killshot13 profile image
Michael R. • Edited

Wow, I think @tpenguinltg probably has some of the most elaborate ones I've seen, nice config you have there!

And all of you have shared practical examples; hats off to the author and others who shared their aliases. I will add mine here, though most of them are a bit generic and unimaginative. 😅


I do not take credit for these all myself, many I have adopted and modified along the way to fit my needs, and I hope you all can find something useful. Cheers!

Collapse
 
imjoseangel profile image
Jose Angel Munoz

Thanks for sharing @killshot13 !!

Collapse
 
loebkes profile image
Lui • Edited

I didn't read all that were postet but here are mine that I love the most.

alias editaliases='vi ~/.zsh_aliases'
alias loadaliases='source ~/.zsh_aliases'

alias gb='git branch'
alias gs='git status'
alias ga='git add'
alias gclean='git clean -f -d'
alias gcomm='git commit -m'
alias gmerge='git merge --no-ff -e'
alias gpush='git push'
alias pfusch='git push -f'
alias grsh='git reset HEAD --hard'
alias gback='git switch -'

alias glog="git --no-pager log $1 --pretty=format:'%Cgreen%h%Creset - %<(75,trunc)%s %C(bold blue)%<(15,trunc)<%an>%Creset %Cgreen%cr%Creset %C(yellow)%d%Creset' --abbrev-commit --date=relative"
alias glogl="glog -10"
alias glogy="glog --since=yesterday.midnight"
alias glogt="git log --graph --pretty=format:'%Cgreen%h%Creset - %s %C(bold blue)<%an>%Creset %Cgreen%cr%Creset %C(yellow)%d%Creset' --abbrev-commit --date=relative"
alias glogtl='glogt -10'
alias glogf="git log --pretty=format:'%Cgreen%h%Creset - %<(75,trunc)%s %C(bold blue)%<(15,trunc)<%an>%Creset %Cgreen%cr%Creset %C(yellow)%d%Creset' --abbrev-commit --date=relative --follow"

alias gfiles='git diff-tree --no-commit-id --name-only -r'
alias gitrect="git reset HEAD --hard"
Enter fullscreen mode Exit fullscreen mode

Most used by me is glogl (git log last) and glog -100 (git log last 100)

It creates an output like:

Image description

Collapse
 
imjoseangel profile image
Jose Angel Munoz

Thanks @loebkes for sharing. Really cool one.

Collapse
 
jessekphillips profile image
Jesse Phillips
Collapse
 
imjoseangel profile image
Jose Angel Munoz

Thank you @jessekphillips

Collapse
 
imjoseangel profile image
Jose Angel Munoz

Wow! didn't know that. Thanks @natescode !!

Collapse
 
netch80 profile image
Valentin Nechayev

NB "git wip" conflicts with standard alias setting for Gerrit, for which injection of an alias series "ready", "wip", etc. is suggested (and supported by a local installer).

Collapse
 
netch80 profile image
Valentin Nechayev

The thing that confuses is that alias set is very uneven in sense of pressings. If you press something on keyboard, you anyway press some sequences, and all git commands start with "git " - already 4 presses... After it, difference, for example, between "git br" = "git branch -r" and "git b -r" where "b" is already aliased as "branch" is nearly void, compared with any other difference. This suggests the alias set is accrued historically without substantial reconcerning.

Alias "db" is dangerous. I prefer to write such actions only explicitly. Same for "op": it's normal to refer to something done a few days ago, and for "vc". (BTW do you use IDE? Something like "git clean -dfx -e .vscode" would be better.)

What is use case for "search"?

"last" could be extended with "--stat" to show changes, because anyway only a single commit is printed.

OTOH, it clearly exposes some manners and developing approaches - as presence of "main" and "devel" branches. For me, it showed some features I was unaware or forgot them due to standing out of usual habits... thanks for this.

Formatting was broken - e.g. what is "git alias" is unparseable (seems the website markdown was active). If it allows editing the post, please reconsider.

To compare, my typical set (with comments):

   ae = add -e

Maybe more specific to my manner, but it is typical to cache only some changes among ones in file (others could be for later commits, not committed as debug, etc.) - just interactive mode is not enough. Git's recalculating of chunk line count works well enough.

   amsh = am --show-current-patch

In rebase, sometimes it is needed to see full patch to apply now.

    b = branch

Just shortening for all branch related commands.

    cbr = rev-parse --abbrev-ref HEAD

"current branch" in an easy solitary way.

   cia = commit --amend
   ciah = commit --amend -C HEAD

To adjust the last commit with catch-up changes.

    ci = commit
   co = checkout

Habitual from previous VCSes. To edit commit message is more useful because it never should be one-liner in a final version.

    dc = diff --cached

Obviously needed before commit is created from parts.

    cp = cherry-pick
   cpa = cherry-pick --abort
   cpc = cherry-pick --continue
   cpnx = cherry-pick -n -x
   cpx = cherry-pick -x

Maybe "cp -n", etc. is enough but often used in some flows.

    lggo = log --graph --oneline
    lg = log --graph
    lgf = log --graph --pretty=fuller --topo-order
    lgs = log --graph --stat
    logf = log --pretty=fuller --topo-order
    lpf = log -M -p --pretty=fuller
    lp = log -M -p
    lss = log -M --shortstat
    lstf = log -M --stat --pretty=fuller --topo-order
    lst = log -M --stat
    lggo = log --graph --oneline
    lg = log --graph
    lgf = log --graph --pretty=fuller --topo-order
    lgs = log --graph --stat
    logf = log --pretty=fuller --topo-order
    lpf = log -M -p --pretty=fuller
    lp = log -M -p
    lss = log -M --shortstat
    lstf = log -M --stat --pretty=fuller --topo-order
    lst = log -M --stat
Enter fullscreen mode Exit fullscreen mode

Convenience for log view styles.

    pura = pull --rebase --autostash
Enter fullscreen mode Exit fullscreen mode

For some flows, merge is impossible - it's required to actualize the working state regularly.

    rba = rebase --abort
    rbas = rebase --autostash
    rbc = rebase --continue
    rbi = rebase -i
    rbias = rebase -i --autostash
    rbs = rebase --skip
Enter fullscreen mode Exit fullscreen mode

Interactive rebase helpers in different styles to keep propositions clear.

    startempty = commit --allow-empty -m 'Initial empty'
Enter fullscreen mode Exit fullscreen mode

Rare but... for a new work, having basic empty commit is crucial to keep the first commit clear. Otherwise, complex dances with filtering are needed. (I'm wondering why it is not in Git base logic.)

I'd also note some crucial settings out of aliases:

merge.conflictstyle=diff3

To show base version for a conflict - crucial both for a human and automatic merging tools.

pager.status=true

Otherwise some tools avoid paging.

push.default=simple

Never push many-to-many without an explicit specification in a command.

pull.ff=only

Useful default to minimize underwater pull effects.

Collapse
 
imjoseangel profile image
Jose Angel Munoz

Thanks @netch80 I have reviewed the post and I some backslashes were missing. Thanks for your comments. Really helpful.

Collapse
 
goodevilgenius profile image
Dan Jones
[alias]
        line = log --oneline
        st = status -sb
        last = log -1 HEAD --stat
        fap = fetch --all --prune
        cb = checkout -b
        gc-full = gc --prune=now --aggressive
        search = "!f() { s=\"$1\"; shift; if (( $# > 0 )); then set -- @; fi; git rev-list \"$@\" | xargs git grep \"$s\" ; }; f"
        changed-files = "!f() { git log --name-only --pretty=oneline --full-index \"$1\" | grep -vE '^[0-9a-f]{40}'|sort|uniq;}; f"
        push-new = !git push -u upstream $(git rev-parse --abbrev-ref @)
Enter fullscreen mode Exit fullscreen mode

Also, for work specifically, my company works on GitHub, and I have a bash script to easily create a new PR from the current branch.

#!/bin/bash

set -eu

declare -r UPSTREAM_NAME=upstream
declare MERGE_BRANCH=main

declare -r BRANCH=$(git rev-parse --abbrev-ref HEAD)

declare UPSTREAM_URL=$(git remote get-url upstream)
UPSTREAM_URL=$(echo "$UPSTREAM_URL" | sed -E -e 's|^git@|https://|' -e 's|.com:|.com/|' -e 's/.git$//')

MERGE_BRANCH=${1:-$MERGE_BRANCH}

declare -r PR_URL="${UPSTREAM_URL}/compare/${MERGE_BRANCH}...${BRANCH}?expand=1"

echo $PR_URL
open $PR_URL
Enter fullscreen mode Exit fullscreen mode

I name it git-new-pr, and add it to my $PATH. Then, I can call git new-pr, and it'll automatically open up a page for a new PR in my browser.

This script is for macOS. So, the last line opens the URL in the default browser. If using Linux, this could be xdg-open $PR_URL. Also, a specific browser could be specified.

Most people would just use the GitHub cli for this, but I've never bothered installing it, and this works really well.

Collapse
 
imjoseangel profile image
Jose Angel Munoz

Thanks for sharing @goodevilgenius !!

Collapse
 
primo profile image
Primo • Edited

I have only two aliases:

[alias]
    changes=!git fetch && git log --name-status HEAD..
    fire=git add . && git commit -m "OMG, FIRE" && git push
Enter fullscreen mode Exit fullscreen mode
Collapse
 
imjoseangel profile image
Jose Angel Munoz

Haha! Love the second one. Thanks for sharing @primo

Collapse
 
imjoseangel profile image
Jose Angel Munoz

Nice ones also @ginomempin . Thanks for sharing!

Collapse
 
thisisobate profile image
Uchechukwu Obasi

Great aliases you've got!
I have just one git alias at the moment and that is:

[alias]
    ## sign your commit message
    csm = commit -s -m
Enter fullscreen mode Exit fullscreen mode

This comes very handy especially if you contribute to open source projects frequently.

Collapse
 
imjoseangel profile image
Jose Angel Munoz • Edited

Thanks @thisisobate ! Added to my list

Collapse
 
okbrown profile image
Orlando Brown

Sorry to be a Party Pooper!!
But why not use ZSH with the Oh ZSH add-on and it's various plugins. ohmyz.sh/

Even posted on Dev.to
dev.to/0xkoji/do-you-know-oh-my-zs...

Collapse
 
ehaynes99 profile image
Eric Haynes

Great list! Will definitely add a few of these.

A few more that I like:

# oops, missed a file in prior commit
ca = commit --amend --no-edit
fa = fetch --all --prune
# IMO this option should be the default for force pushing
pf = push --force-with-lease
# thing you do for first push of a branch
pu = push -u origin HEAD
# create a WIP commit without running precommit
wip = commit -m\"WIP\" --no-verify
# "uncommit"
pop = reset HEAD^
# list branches by most recent commit
recent = for-each-ref --sort=-committerdate --count=30 --format='%(refname:short)' refs/heads/
Enter fullscreen mode Exit fullscreen mode
Collapse
 
imjoseangel profile image
Jose Angel Munoz

Thanks for sharing @ehaynes99 ! I will take note of yours too.