Skip to content

Instantly share code, notes, and snippets.

@dsc
Created February 10, 2023 21:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dsc/99efeb1e61c11fe88cd3242626375997 to your computer and use it in GitHub Desktop.
Save dsc/99efeb1e61c11fe88cd3242626375997 to your computer and use it in GitHub Desktop.
[alias]
### REMEMBER ###
# When using a function-wrapper, like !"f () { echo foo; }; f" ...
#
# 1) You *MUST* use DOUBLE QUOTES around the value.
#
# 2) You *MUST* to double-escape any regex-escape (eg. \.\1\w) that is NOT a normal escape.
# So '\t\n' is ok, but '\1' needs to become '\\1'. (I think lines are fed to sh, which
# means eval strips off a layer of escaping even in single-quoted strings!)
h = help
he = help
hel = help
s = status
st = status
r = remote -v
f = fetch
fe = fetch
u = pull
ul = pull
p = push
pu = push
cl = clone
co = checkout
c = commit
ci = commit
### branches ###
b = branch
br = branch
## remotes: print "name TAB repo" for each remote
remotes = "!f () { local URLTYPE=${1:-fetch}; git remote -v | grep $URLTYPE | awk -F'[ \t]+' '{ print $1 \"\t\" $2 }'; }; f"
rem = !git remotes
## update: remote update -- fetch updates for everybody
update = remote update
up = !git update
## prune-all: clean up stale remote branches
prune-all = !git remote | xargs -n 1 git remote prune
## branch-name: the name of the current branch, without the leading '*'
branch-name = "!f () { git branch --no-color $* | cut -d' ' -f2 | tr -d '[:space:]'; }; f"
bn = !git branch-name
## branch-remote: remote name this branch is tracking
branch-remote = !git config --get "branch.$(git branch-name).remote"
brem = !git branch-remote
## branch-repo: remote repo url this branch is tracking
branch-repo = !git remote -v | fgrep 'fetch' | grep -P "^$(git branch-remote)"'\\b' | awk -F'[ \t]+' '{ print $2 }'
brepo = !git branch-repo
## track: checkout and track a new branch
track = checkout --track -b
tr = !git track
## upstream: set upstream branch
upstream = "!f () { local b=${1:-$(git branch-name)}; local r=${2:-origin/$b}; shift 2; git branch --set-upstream $b $r $*; }; f"
## mods: recursive-update all submodules
mods = !git submodule update --init --recursive
### mutate! ###
## rm to be less lazy and kill directories by default
# rm = rm -r
## unstage: unstage a file (remove it from the index) -- opposite of "git add"
unstage = rm -r --cached
un = !git unstage
## missing: files no longer present in the working tree
missing = diff --name-only --diff-filter=D
## unstage-missing: unstage missing files
unstage-missing = "!f () { git diff --name-only --diff-filter=D -z $* | xargs -0 git rm --cached; }; f"
um = !git unstage-missing
## undo: dump uncommitted changes
undo = !git checkout HEAD --
## amend: combine current changes with previous commit
amend = "!f () { local args=$*; [ -z \"$args\" ] && args='-a'; git commit --amend -C HEAD $args; }; f"
ca = !git amend
## checkpoint: a minor commit represented by a dummy message, for ease of finding them later.
checkpoint = commit -a -m "checkpoint"
ch = !git checkpoint
## ignore: edit then stage .gitignore
ignore-edit = "!mate -w .gitignore && git add .gitignore"
ige = !git ignore-edit
ig = !git ignore-edit
ignore = !git ignore-edit
## ignoring: appends to .gitignore
ignore-append = "!f () { local GIT_IGNORE=\"$([ -e .gitignore ] && echo . || git root)/.gitignore\"; for arg in $*; do echo $arg >> $GIT_IGNORE; done; }; f"
iga = !git ignore-append
igd = !git ignore-append
## ignore-stage: stages .gitignore, then dumps it to stderr
# ignore-stage = "![ -e .gitignore ] && { git add .gitignore; cat .gitignore >&2; }"
# igs = !git ignore-stage
### repo meta ###
## root: root repository directory
root = !git rev-parse --show-toplevel 2>/dev/null
## repo: repository url of first listed remote (which is hopefully the primary)
repo = !git remote -v | fgrep 'origin' | fgrep 'fetch' | head -n1 | awk -F'[ \t]+' '{ print $2 }'
## url: repository url made suitable for a browser
url = !git repo | sed -E -e 's=^[^@]+@([^:]+):=http://\\1/=' -e 's/^git:/http:/' -e 's/\\.git$//'
## home: open repository url in a browser
home = !open $(git url)
## rev: short version of the HEAD revision
rev = "!f () { local args=$*; [ x$args = x ] && args=HEAD; git rev-parse --short $args; }; f"
### changes happen ###
d = diff
df = diff
## who: what
who = shortlog -s --
## log-graph: make a pretty graph of the commit log
graph = log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit --date=relative
## graphviz = !"f() { echo 'digraph git {' ; git log --pretty='format: %h -> { %p }' \"$@\" | sed 's/[0-9a-f][0-9a-f]*/\"&\"/g' ; echo '}'; }; f"
# Examples:
# $ git graphviz HEAD~100..HEAD~60 | dotty /dev/stdin
# $ git graphviz --first-parent master | dotty /dev/stdin
### alias aliases ###
# See also: https://git.wiki.kernel.org/index.php/Aliases
## aliases: list all aliases.
aliases = !git config --get-regexp alias | ssed -R -e 's/\\t/\\\\t/g' -e 's/^alias\\.(\\S+?)\\s+(.*)$/\\1\t"\\2"/' | awk -F'\\t' '{ printf \"%-16s%s\\n\", $1, $2 }' | grep -v -E '^aliases'
a = !git aliases
as = !git aliases | sort
alias-grep = !git aliases | grep -P
ag = !git alias-grep
### !!! ### actung! these break hardlinks to ~/.gitconfig ?! ### !!! ###
# alias = !sh -c '[ $# = 2 ] && git config --global alias.\"$1\" \"$2\" && exit 0 || echo \"usage: git alias <new alias> <original command>\" >&2 && exit 1' -
# alias = "!function f() { test $# -ne 2 && echo 'usage: git alias ALIAS COMMAND' >&2 && exit 1; git config --global alias.\"$1\" \"$2\"; }; f"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment