Skip to content

Instantly share code, notes, and snippets.

@sunfarm
Last active March 30, 2019 15:16
Show Gist options
  • Save sunfarm/1856087ce8add0bc0cd65d17d7101f21 to your computer and use it in GitHub Desktop.
Save sunfarm/1856087ce8add0bc0cd65d17d7101f21 to your computer and use it in GitHub Desktop.
Git config
# Some of these commands are pretty esoteric and may not make sense to others. Feel free to ask me about them though!
[user]
# name = YOUR_NAME
# email = YOUR_EMAIL
[push]
default = simple
[alias]
### Branching ###
# Rebuild a release branch. Silently fails if not named properly
rebuild = "!f() { BRANCH=${1:-$(git bb)}; [[ "$BRANCH" =~ ^(tmp)|(release)/.* ]] && git checkout master && git pull && git b -D $BRANCH && git checkout -b $BRANCH && git push --set-upstream origin +$BRANCH; }; f"
# branches-active -- I mark branches for deletion by prefixing them with xxx- and this command doesn't list these branches
ba = "!f() { git b | grep -v xxx-; }; f"
# Add, commit
ac = "!f() { git add -A; git commit -m "\"$1\""; }; f"
uncommit = reset HEAD^
logme = !git log --author=\"$(git config user.name)\"
# Shows commits that are not present on origin
loglocal = log origin..HEAD
# Log current branch -- Show commits only on current branch
logcb = log --first-parent --no-merges
ls = log --oneline
untrack = rm -r --cached
b = branch
co = checkout
bb = symbolic-ref --short HEAD
sl = stash list
git = !exec git
take = !git-take
# Print the name of the current branch
# List branches that have been merged into current branch
merged = !"git log --first-parent --pretty=format:'%s' --merges --grep $(git bb) | cut -d\\' -f2 | sort -u"
# stash-export = !"mkdir -p stashexport && git stash list | sed \\'s/\//\_/g\\' "
# | sed 's/ /\_/g' | awk -F ":" '{ system("git stash show -p " $1 " >> stashexport/" $1$2$3 ".diff" ) }'
folders = !"git status | cut -s -d/ -f1-3 | cut -d: -f2- | sed -e 's/^[[:space:]]*//' | sort -u"
# Use regex to refer to named stashes
sgrep = "!f() { ref=$(git --no-pager stash list | grep "$1" | cut -d: -f1 | head -n1); echo ${ref:-<no_match>}; }; f"
# When there is no result, the output would be an empty string causing the show/apply/drop command to act on the first stash stash@{0}
# ref=$(...); echo ${ref:-<no_match>};
sshow = "!f() { git stash show $(git sgrep "$1") -p; }; f"
sapply = "!f() { git stash apply $(git sgrep "$1"); }; f"
sdrop = "!f() { git stash drop $(git sgrep "$1"); }; f"
# Get non-merge commits only on the target branch $1 and apply them cherry-picked to the current branch. Take commits since $2 inclusive
# TODO: make work with less than the 9-char hash e.g. 11292160d
cp = "!f() { git cherry-pick -x $(git log --format='%h' --first-parent --no-merges $1 | python -c \"import sys;l=[line.strip() for line in sys.stdin];print(' '.join(reversed(l[:l.index('$2'[:7])+1])))\"); }; f"
logc = "!f() { git log --format='%h' --grep $2 $1; }; f"
show-msg = "!git --no-pager log -1 --pretty=format:%s"
cherry-list = "!f() { git log --format='%h' --first-parent --no-merges $1 | python -c \"import sys;l=[line.strip() for line in sys.stdin];print(' '.join(reversed(l[:l.index('$2'[:7])+1] if '$2' else l)))\"; }; f"
cherry-echo = "!f() { for commit in $(git cherry-list "$1" "$2"); do msg=$(git show-msg $commit); echo ""; echo "Orig: $commit"; echo "Msg: $msg"; echo "New: ${msg/$3/$4}"; done }; f"
cherry-replace = "!f() { for commit in $(git cherry-list "$1" "$2"); do msg=$(git show-msg $commit); git cherry-pick -n $commit; git commit -m "\"${msg/$3/$4}\""; done; }; f"
cherry-ticket = "!f() { for commit in $(git cherry-list "$1" "$2"); do msg=$(git show-msg $commit); git cherry-pick -n $commit; git commit -m "\"$3 $msg\""; done; }; f"
# Get the child commit of the current commit.
# Use $1 instead of 'HEAD' if given. Use $2 instead of curent branch if given.
child = "!git log --format=%H --reverse --ancestry-path ${1:-HEAD}..${2:\"$(git rev-parse --abbrev-ref HEAD)\"} | head -1"
[init]
templatedir = ~/.git-templates
[pull]
rebase = true
[core]
editor = /Applications/MacVim.app/Contents/MacOS/Vim
excludesfile = /Users/nat/.gitignore_global
pager = tig
[color]
pager = false
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment