Skip to content

My Git Alias

Here is a list of all the alias I use:

[alias]
# The aliases below are mainly for lazy people :-)
    a = add
    d = diff
    f = fetch --prune --force --tags # This fetches the remote changes, removes the local references that have been deleted and force-updates the tags
    l = log --branches --remotes # This forces the display of the branches and remote references in git log
    rb = rebase
    frb = !bash -c 'git f && git rb'
    rba = rebase --autostash
    frba = !bash -c 'git f && git rba'
    cit = commit
    co = checkout
    br = branch
    amendf = commit --amend --no-edit
    dt = difftool --dir-diff
    dtm = difftool --dir-diff master .
    dtmain = difftool --dir-diff main .
    dto = !sh -c 'git difftool --dir-diff $(git oldest-ancestor) .'
    #dt = !sh -c 'git difftool --dir-diff $(git oldest-ancestor-main) .'
# The aliases above are mainly for lazy people :-)

    # Show stash count when showing status
    s = status --show-stash

    # When pushing, use --force-with-lease to avoid overwriting other's commit
    pushf = push --force-with-lease

    # push the branch upstream
    pushup = !sh -c 'git push --set-upstream origin $(git branch | grep \"*\" | cut -d\" \" -f2-)'

    # Show a commit using difftool
    showtool = "!showci () { rev=${1:-HEAD}; git difftool --dir-diff $rev~1 $rev; }; showci $1"

    # display repo size (human readable)
    size = count-objects -vH

    # Get the oldest commit from which this branch was created (or rebased)
    # From https://stackoverflow.com/questions/1527234/finding-a-branch-point-with-git
    oldest-ancestor = !bash -c 'diff -u <(git rev-list --first-parent "${1:-master}") <(git rev-list --first-parent "${2:-HEAD}") | sed -ne \"s/^ //p\" | head -1' -
    # Same but fallback to main (not master)
    oldest-ancestor-main = !bash -c 'diff -u <(git rev-list --first-parent "${1:-main}") <(git rev-list --first-parent "${2:-HEAD}") | sed -ne \"s/^ //p\" | head -1' -

    # Search for given string in commits:
    search = log -p -S
    searchless = !bash -c 'git log -p -S "$0" | less --use-color -p "$0"'
    # Version that keeps coloring does not work fine...
    searchlesscolor = !bash -c 'git log --color -p -S "$0" | less -r -p "$0"'

    # List local branches
    list-local-branches = !bash -c 'git branch --format \"%(refname:short) %(upstream)\" | awk \"{if (!\\$2) print \\$1}\"'
    llb = list-local-branches
    # list remote branches
    list-remote-branches = !bash -c 'git branch --format \"%(refname:short) %(upstream)\" | awk \"{if (\\$2) print \\$1}\"'
    lrb = list-remote-branches

    # create a branch on top of current commit
    create-local-branch = "!bash -c 'if [[ "$0" = "bash" ]]; then echo "Branch Name needed!"; exit 1; else git co -b users/$(whoami)/$0; fi'"
    clb = create-local-branch

    # daum: Delete (branch) And (checkout, then) Update Master
    delete-and-update-master = !bash -c 'curr_branch=$(git rev-parse --abbrev-ref HEAD) && git co master && git f && git rebase && git branch -D $curr_branch'
    daum = delete-and-update-master
    # dauM: Delete (branch) And (checkout, then) Update Main
    delete-and-update-main = !bash -c 'curr_branch=$(git rev-parse --abbrev-ref HEAD) && git co main && git f && git rebase && git branch -D $curr_branch'
    daumain = delete-and-update-main
    # rboum: Rebase Branch on Updated Master
    rebase-branch-on-updated-master = !bash -c 'curr_branch=$(git rev-parse --abbrev-ref HEAD) && git co master && git f && git rebase && git co $curr_branch && git rebase master'
    rboum = rebase-branch-on-updated-master
    # rbouM: Rebase Branch on Updated Main
    rebase-branch-on-updated-main = !bash -c 'curr_branch=$(git rev-parse --abbrev-ref HEAD) && git co main && git f && git rebase && git co $curr_branch && git rebase main'
    rboumain = rebase-branch-on-updated-main

    # Rename current branch to new provided name and delete remote of old name (need extra pushup afterward)
    rename-branch-delete-remote = !bash -c 'old_name="$(git rev-parse --abbrev-ref HEAD)" && new_name="$0" && echo "old name: $old_name" && echo "new name: $new_name" && echo "If it is not good: [CTRL] + C, otherwise, hit ENTER" && read && git push origin --delete "$old_name" && git co -b "$new_name" && git branch -D "$old_name" ' # && git pushup
    rbdr = rename-branch-delete-remote

    # List branches with deleted upstream (see https://stackoverflow.com/a/64900370)
    gone-ls = ! git fetch -p && git for-each-ref --format '%(refname:short) %(upstream:track)' | awk '$2 == \"[gone]\" {print $1}'
    gone = gone-ls
    # Delete branches that has deleted upstream
    gone-delete = ! git gone | xargs -r git branch -D

# Those slX aliases allow to display the log in a short way (short-log), with a graph to better understand the link between branches (yours and remotes, for instance)
    # Simple short log
    sl = log --oneline --decorate --branches --graph --remotes
    # Simple short log with date
    sld = log --oneline --decorate --branches --graph --remotes --date=format:\"%Y-%m-%d %H:%M\" --pretty
    # Short log with author and date
    slt = log --pretty=tformat:\"%Cblue%h%Creset %cd <%Cgreen%<(10,ltrunc)%cN%Creset>%Cred%d%Creset %s\" --graph --branches --date=short
    # Short log with author, date and changed files
    sls = log --pretty=tformat:\"%Cblue%h%Creset %cd <%Cgreen%<(10,ltrunc)%cN%Creset>%Cred%d%Creset %C(yellow normal bold)%s%Creset\" --graph --branches --stat --date=short