Skip to content

Instantly share code, notes, and snippets.

@cesalazar
Last active April 21, 2024 12:13
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cesalazar/eff84a1389d05911767122ab78890dcc to your computer and use it in GitHub Desktop.
Save cesalazar/eff84a1389d05911767122ab78890dcc to your computer and use it in GitHub Desktop.
My git aliases
# Use in ~/.gitconfig like this:
#
# [include]
# path = ~/.gitaliases
[alias]
# Attempt to write to the clipboard using xsel or xclip; fails silently
set-clipboard = !xsel -ib &> /dev/null || \
xclip -selection clipboard -in &> /dev/null
# Attempt to paginate using bat or $PAGER, falling back to less
pager = !bat 2> /dev/null || ${PAGER:-false} || less -FgiMRwXz-4
# Get the origin URL from the current repository config
get-origin-url = !git config --get remote.origin.url \
| sed -e 's/^git@/https:\\/\\//' -e 's/:\\(\\w\\)/\\/\\1/' -e 's/\\.git$//'
# Drop all local changes, returning to upstream
nuke-local = reset --hard @{u}
recover-stash = !gitk --all $(git fsck --no-reflog \
| awk '/dangling commit/ {print $3}')
# (b)ranch (n)ame
bn = rev-parse --abbrev-ref HEAD
# (b)ranch (n)ame (c)opy
bnc = !git bn | tr -d '\n' | git set-clipboard
# (br)anch
br = branch
# (c)ommit (a)mmend (edit last commit message)
ca = commit --amend
# (c)ommit (a)mmend (n)o-edit (reuse last commit message)
can = commit --amend --no-edit
# (c)heck(o)ut
co = checkout
# (c)heckout (p)ull(r)equest (dettached head using the GitHub PR number)
cpr = !git fetch origin "pull/$@/head" && git checkout FETCH_HEAD && echo PR
# (d)iff (c)ached (staged files)
dc = !git di --cached
# (d)iff (h)ead
dh = !git diff "HEAD~${1:-1}" "${@//$1/}" && echo "" > /dev/null
# (di)ff
di = "!giff(){ git diff "$@" \
':(exclude)*composer.lock' ':(exclude)*package-lock.json' \
':(exclude)*yarn.lock'; unset -f giff; }; giff"
# (di)ff (s)tash (diff against the stash)
dis = "!gifs(){ git diff \"stash@{${1:-0}}\"; unset -f gifs; }; gifs"
# (d)iff (t)ool (most likely meld)
dt = difftool
# (e)dit (a)liases
ea = !${EDITOR:-vim} ~/.gitaliases
# (e)dit (c)onfig (global)
ec = config -e --global
# (e)dit (c)onfig (l)ocal
ecl = config -e
# I don't recall :(
fc = diff-tree --no-commit-id --name-only
# (f)ollow (f)ile (h)istory
ffh = log --follow -p --
# (l)ist (a)liases
la = "!sed -n '/\\[alias\\]/,$ {//n;p;}' ~/.gitaliases | git pager"
# (l)og (g)raph
lg = log --graph --all --decorate --oneline
# (l)ist (h)idden
lsh = !git ls-files -v | grep --no-ignore-case "^h" | awk '{print $2}'
# (o)pen (i)ssues in browser
oi = !git get-origin-url | xargs -I % xdg-open '%/issues'
# (o)pen (c)hanged (f)iles
ocf = !${EDITOR:-vim} $(git diff --name-only)
# (o)pen (o)rigin in browser
oo = !git get-origin-url | xargs xdg-open
# (o)pen (p)ulls in browser
op = !git get-origin-url | xargs -I % xdg-open '%/pulls'
# (o)pen (t)icket in browser (orderbid only)
ot = !git rev-parse --abbrev-ref HEAD \
| grep -Eoi '(CON|FEW|MINI|PIN|PRO)-[[:digit:]]{1,}' \
| xargs -I % xdg-open 'https://orderbird.atlassian.net/browse/%'
# (p)ush (t)ag
pt = push origin
# (p)ush (t)ag (d)elete
ptd = push --delete origin
# (r)eset (h)ead (remove staged files from the stage)
rh = reset HEAD
# (s)tash (l)ist
sl = stash list
# (s)hort s(t)atus (requires the alias below: sta)
st = !git sta -uno --short
# (s)hort s(t)atus (a)ll (includes hidden files)
# The last echo prevents showing the -uno argument
sta = !git -c color.status=always status --short "$1" && git lsh \
| echo "$(wc -l) files assumed as unchanged" && echo "" &> /dev/null
# (s)tash (p)ush (m)essage (follow with a message in single quotes)
spm = stash push -m
# (s)ubmodule (u)pdate (i)nit
sui = submodule update --init
# (s)ubmodule (u)pdate (r)emote (m)erge (update submodule from remote)
surm = submodule update --init --remote --merge
# (t)ag (l)ist
tl = tag --list
# (u)pdate-index (a)ssume-unchanged ("hide" a file by assuming unchanged)
ua = update-index --assume-unchanged
# (u)pdate (d)evelop (and return to the previous branch)
ud = !git co develop && git pull && git co -
# (u)pdate-(i)ndex (r)ecursive ("unhide" all files assumed as unchanged)
uir = !git lsh | xargs -L1 git update-index --no-assume-unchanged
# (u)pdate-index (r)eally ("unhide" a file assumed as unchanged)
un = update-index --no-assume-unchanged
# All the fuzzy commands require fzf
# https://github.com/junegunn/fzf
# Allows to pick files showing their diff in a panel
fzf-diff-preview = !fzf --preview-window=bottom:nohidden --preview \
'echo {} | cut -f 3 -d \" \" | xargs git diff --color=always'
# (f)uzzy (a)dd
fa = !git status --short \
| git fzf-diff-preview \
| awk '{print $(NF)}' \
| xargs git add 2>&-
# (f)uzzy (b)ranch (d)elete
fbd = !git branch | fzf | tr -d '*' | awk '{print $1}' \
| xargs -L1 git branch -D 2>&-
# (f)uzzy (c)heck(o)ut
fco = !git branch -a \
| fzf --no-multi --preview-window=bottom --preview \
'git diff --color=always {+1}' \
| sed 's~\\(remotes/\\)\\?origin/~~' \
| xargs git co
# (f)uzzy (d)iff
fd = !git -c status.relativePaths=true status --short -uno \
| git fzf-diff-preview
# (f)uzzy (f)ile (r)eset
ffr = !git status --short -uno \
| git fzf-diff-preview \
| awk '{print $(NF)}' \
| xargs git co --
# (f)uzzy (l)og
fl = !git log --oneline \
| fzf --preview-window=bottom --preview 'git show --color=always {+1}'
# (f)uzzy (p)ick (f)ile
fpf = !git status --short \
| grep -v 'assumed as unchanged' \
| fzf \
| awk '{print $NF}' \
| tr '\n' ' ' \
| xsel -ib
# (f)uzzy (s)tash (a)pply
fsa = !git fsl | awk '{print $1}' | tr -d ':' \
| xargs -I % git stash apply %
# (f)uzzy (s)tash (d)rop
fsd = !git fsl | awk '{print $1}' | tr -d ':' | sort -r \
| xargs -I % git stash drop %
# (f)uzzy (s)tash (l)ist
fsl = !git stash list \
| fzf --preview-window=bottom --preview \
'echo {} | cut -f 1 -d \":\" | xargs git stash show $1 -p --color=always'
# (f)uzzy (s)tatus (o)pen (pick files from the status to open)
fso = !git -c color.status=never status --short "$1" \
| grep -Ev 'files assumed' \
| fzf \
| awk '{print $2}' \
| xargs -o "${EDITOR:-vim}"
# (f)uzzy (s)tash (p)ush
fsp = !git status --short \
| grep -v "??" \
| git fzf-diff-preview \
| awk '{print $2}' \
| tr '\n' ' ' \
| xargs -I % sh -c 'git stash push -m \"$1\" %' sh
# (f)uzzy (t)ag (d)elete
ftd = !git tag --list \
| fzf \
| xargs -I % git tag -d %
# (f)uzzy (u)pdate-index (a)ssume-unchanged
fua = !git status --short \
| grep -v "?" \
| git fzf-diff-preview \
| awk '{print $2}' \
| xargs -L1 git ua
# (f)uzzy (u)pdate-index (n)o-assume-unchanged
fun = !git lsh | fzf | xargs -L1 git un
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment