Skip to content

Instantly share code, notes, and snippets.

@binaryfunt
Last active August 1, 2024 15:44
Show Gist options
  • Save binaryfunt/7a855fb5754e1c6f9553860fe23fbf73 to your computer and use it in GitHub Desktop.
Save binaryfunt/7a855fb5754e1c6f9553860fe23fbf73 to your computer and use it in GitHub Desktop.
My gitconfig with useful aliases + custom scripts
# [filter "lfs"]
# clean = git-lfs clean -- %f
# smudge = git-lfs smudge -- %f
# process = git-lfs filter-process
# required = true
[core]
# editor = notepad
editor = code -w # -n --disable-extensions
[rerere]
enabled = true
autoupdate = true
[alias]
c = commit
co = checkout
st = status
fixup = commit --amend --no-edit
uncommit = reset --soft HEAD^
graph = log --decorate --oneline --graph --date-order
ga = log --all --decorate --oneline --graph --date-order
graph-simple = log --decorate --oneline --graph --first-parent --date-order
list-conflicts = diff --name-only --diff-filter=U
conflicts = diff --diff-filter=U
list-aliases = config --get-regexp ^alias\\.
log-today = log --author='binaryfunt' --since='6am' --graph --format=tformat:' %C(yellow)%h %C(dim white) %cr %C(green)%d%n%n %s%n%n' --all
# setup-triangular = !git config remote.pushdefault origin && git config push.default current
up = !git fetch && git reset --hard '@{u}'
fix-rebase = !git apply .git/rebase-apply/patch || git rebase --continue
autosquash = rebase --autosquash
# (for git <2.44.0:)
# autosquash = "!if [ -z \"$1\" ]; then echo 'Usage: git autosquash <base>'; exit 1; fi; GIT_SEQUENCE_EDITOR=: git rebase -i --autosquash \"$1\" #"
copr = checkout-pr
setup-fetch-upstream-tags = config --add remote.upstream.fetch "+refs/tags/*:refs/tags/*"
setup-push = config push.default upstream
#!/usr/bin/env bash
set -eo pipefail
USAGE='
Usage:
$ git checkout-pr [<remote>] <pr_number>
Checkout a PR branch. If a local branch with the name "pr-<num>" already exists, it will be checked out and force-pulled.
Default remote is "upstream"
'
if [[ -z "$1" ]]; then
echo "$USAGE"
exit 1
elif [[ -z "$2" ]]; then
remote="upstream"
pr_number="$1"
else
remote="$1"
pr_number="$2"
fi
branch="pr-${pr_number}"
git checkout -b "$branch" || git checkout "$branch"
git fetch "$remote" pull/"$pr_number"/head
git reset --hard FETCH_HEAD
#!/usr/bin/env bash
set -eo pipefail
USAGE='
Usage:
$ git diff-diffs <branch1_base> <branch1_tip> <branch2_base> <branch2_tip>
Opens (in VSCode) the diff of the git diffs for two different branches, given the commitishes of the base and tip of the two branches.
'
if [[ -z "$4" ]]; then
echo "$USAGE"
exit 1
fi
tmpdir="$(mktemp -d)"
before="${tmpdir}/before.diff"
after="${tmpdir}/after.diff"
git diff --minimal -U2 "${1}..${2}" > "$before"
git diff --minimal -U2 "${3}..${4}" > "$after"
# Remove uneccessary differences:
sed -i '/^index /d' "$before"
sed -i 's/^@@ .* @@/@@ \.\.\. @@/' "$before"
sed -i '/^index /d' "$after"
sed -i 's/^@@ .* @@/@@ \.\.\. @@/' "$after"
code --diff "$before" "$after"
#!/usr/bin/env bash
set -euo pipefail
if [[ -z "$1" ]]; then
# currently checked out branch
git fetch
git reset --hard "@{u}"
else
# specified branch
remote="$(git config "branch.${1}.remote")"
if [[ -z "$remote" ]]; then
echo "Branch '${1}' does not have a remote configured" >&2
exit 1
fi
tracking="$(git config "branch.${1}.merge")"
if [[ -z "$tracking" ]]; then
echo "Branch '${1}' is not tracking a remote branch" >&2
exit 1
fi
git fetch "$remote" "${tracking}:${1}"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment