Skip to content

Instantly share code, notes, and snippets.

@wojteklu
Last active September 5, 2023 00:58
Show Gist options
  • Star 18 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save wojteklu/eb5ef6085ea531640e8f964f6159dde8 to your computer and use it in GitHub Desktop.
Save wojteklu/eb5ef6085ea531640e8f964f6159dde8 to your computer and use it in GitHub Desktop.
Perfect git environment on OS X

Perfect git environment on OS X

Global alias

Add the following to your ~/.bashrc:

alias g='git'

Branch auto-completion

Install bash-completion

brew install bash-completion

Add the following to your ~/.bash_profile:

if [ -f $(brew --prefix)/etc/bash_completion ]; then
	source $(brew --prefix)/etc/bash_completion
fi

Git excludes

Create ~/.gitexcludes file and paste:

.DS_Store

Git aliases

Add the following aliases into your ~/.gitconfig file:

[alias]
	a =  add
	f = fetch
	rb = rebase
	me = merge
	co = checkout
	br = branch
	ci = commit
	cm = !git add -A && git commit -m
	amend = commit -a --amend
	rm = "!f() { git branch -D ${1-develop}; }; f"
	s = status
	cob = checkout -b
	cp = cherry-pick
	last = log -1 HEAD
	l = log --oneline
	g = log --oneline --graph
	cl = clean -fxd
	grep = !git ls-files | grep -i
	gs = diff --staged
	# compare two branches
	cmp = "!f() { git log --no-merges ${1}..; }; f"
	# pull with rebase & removes not-existing remote-tracking branches
	up = !git pull --rebase --prune
	# quick save
	save = !git add -A && git commit -m 'SAVEPOINT'
	undo = reset HEAD~1 --mixed
	# reset with undo using reflog :)
	wipe = !git add -A && git commit -qm 'WIPE SAVEPOINT' && git reset HEAD~1 --hard
	# delete alread-merged branches
	rmb = "!f() { git branch --merged ${1} | grep -v ${1} | xargs -n 1 git branch -d; }; f"
	# list unpushed commits
	unpushed = log --branches --not --remotes --color --graph --abbrev-commit \
		--pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset%s %Cgreen(%cr) %C(bold blue)%Creset'
	alias = !git config --list | grep 'alias\\.' \
		| sed 's/alias\\.\\([^=]*\\)=\\(.*\\)/\\1\\\t /' | sort

Global git commit hooks

  • Enable git templates:
git config --global init.templatedir '~/.git-templates'
  • Create a directory to hold the global hooks:
mkdir -p ~/.git-templates/hooks
  • Add the following hook into your ~/.git-templates/hooks/pre-commit. It prevents from pushing commit containing word #DANGER#.
#!/bin/sh

if git rev-parse --verify HEAD >/dev/null 2>&1
then
	against=HEAD
fi

marker="#DANGER#"

exec 1>&2

if test $(git diff --cached -z $against | grep $marker | wc -c) != 0
then
    cat <<\EOF
    Error: Invalid debug markers in code:
EOF
    echo `git diff --name-only --cached -z $against -G $marker`
    exit 1
fi
  • Make sure the hook is executable:
chmod +x ~/.git-templates/hooks/post-commit
  • Re-initialize git in each existing repo:
git init
@kiraka42
Copy link

kiraka42 commented Nov 9, 2022

What about Windows ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment