Skip to content

Instantly share code, notes, and snippets.

@darcher-
Last active November 2, 2022 15:46
Show Gist options
  • Save darcher-/f96b7723458809b819775c7b7df737a2 to your computer and use it in GitHub Desktop.
Save darcher-/f96b7723458809b819775c7b7df737a2 to your computer and use it in GitHub Desktop.
A list of `git` default configurations and alias shortcut commands.

Global defaults

use alias[key] = "!f() { 〈 command 〉 }; f" to include complex commands

# .git/config

[alias]
	log = log --oneline --graph
	lgc = log -l HEAD --stats
	caa = commit -a --amend -C HEAD
	usr = config --global -l
	cmd = config --get-regexp alias
	dst = reflog expire --dry-run --expire-unreachable=30.days refs/stash
	sba = '!f() { git subtree add --prefix $2 $1 main --squash; }; f'
	bcn = '!f() { git branch --merged ${1-main} | grep -v "${1-main}$" | xargs -r git branch -d; }; f'
	fsh = '!f() { git fetch origin && git rebase origin/$(git branch --show-current); }; f'
	fpc = '!f() { git push -u -F origin $(git branch --show-current); }; f'
	fnd = '!f() { git !git rev-list --all | xargs git grep -F $1; }; f'
	dbm = '!f() { git fetch -p && for branch in `git branch -vv --no-color | grep ": gone]" | awk "{print $1}"`; do git branch -D $branch; done; }; f'
	ubm = '!f() { git for-each-ref --format "%(refname:short)" refs/heads | grep -v main | xargs git branch -D; }; f'
	bin = '!f() { git reset bin/** && git clean bin/** -fxd; }; f'
	new = !sh -c 'git log $1@{1}..$1@{0} "$@"'
	sbf = !sh -c 'git diff "$@" | grep "^[+-]" | sort --key=1.2 | uniq -u -s1'

branch.autosetuprebase always rebase new branches

git config --global branch.autosetuprebase always

pull.rebase always rebase pulls

git config --global pull.rebase true

push.default always push current branch

git config --global push.default current

Global shortcuts

usr get global user configs list

git config --global -l

cmd get global alias list

git config --get-regexp alias

refresh pull remote changes into local branch

git fetch origin && git rebase origin/$(git branch --show-current);

reindex reindex branch commit history

git reset $(git merge-base origin/master $(git rev-parse --abbrev-ref HEAD))

shove force push current local branch changes to remote origin

git push -u -F origin $(git branch --show-current)

history get local branch commit history

git log --oneline --graph

stats get local branch last commit in history

git log -l HEAD --stats

find get commits in history that have matches term or phrase in message

git !git rev-list --all | xargs git grep -F 'some phrase or term'

debranch remove all branches that have been merged into master.

git fetch -p && for branch in `git branch -vv --no-color | grep ": gone]" | awk "{print $1}"`; do git branch -D $branch; done

unbranch remove all branches that are not master.

git for-each-ref --format '%(refname:short)' refs/heads | grep -v "master\|main" | xargs git branch -D

destash expire stash older than 30 days

git reflog expire --dry-run --expire-unreachable=30.days refs/stash

dropbin unstage & discard entire /bin directory

git reset bin/** && git clean bin/** -fxd

amend takes all uncommitted and un-staged changes, currently in the working directory, and add them to the previous commit.

git commit -a --amend -C HEAD
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment