Skip to content

Instantly share code, notes, and snippets.

@qoomon
Last active March 27, 2024 09:51
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 qoomon/7b78dd3ea358befd051752ccb6f0e1a5 to your computer and use it in GitHub Desktop.
Save qoomon/7b78dd3ea358befd051752ccb6f0e1a5 to your computer and use it in GitHub Desktop.
Opinionated Git Config
#!/bin/bash
# git global config see $HOME/.gitconfig
# ensures to convert CRLF to LF when writing to database
git config --global core.autocrlf 'input'
git config --global core.editor '$EDITOR'
git config --global core.pager 'less -R -M'
git config --global color.ui 'auto'
git config --global merge.ff false
git config --global merge.tool 'vimdiff'
git config --global merge.conflictstyle 'diff3'
git config --global mergetool.prompt false
git config --global diff.tool 'vimdiff'
# automatically set upstream based on local branch name.
# goodbye `git push --set-upstream origin <branch>`
git config --global push.autoSetupRemote true
# automatically push all annotated tags that are reachable from the refs being pushed.
git config --global push.followTags true
# pull rebase by default.
# 'merges' means keep merged branch structure
git config --global pull.rebase 'merges'
# automatically stash changes on pull rebase.
# goodbye `git stash; git pull; git stash pop`
git config --global rebase.autoStash true
# sort tags by version.
# means sort each numeric version part separatly
git config --global tag.sort 'version:refname'
# git config --global commit.template ~/git_commit_template.txt
if [ "$(uname)" = "Darwin" ]; then
git config --global credential.helper osxkeychain
fi
#################
### Aliases
#################
# list all alias
git config --global alias.alias \
$'!git config --name-only --get-regexp ^alias\. | sed s/^alias\.// | sort | while read alias; do
printf "\e[1;34m${alias}\e[m " && git config --get alias.${alias}
done'
# amend to last commit
git config --global alias.amend $'commit --amend --no-edit'
# squash to given commit
git config --global alias.squash $'!f() {
git reset --soft "$1" -- && shift && git commit --amend "$@"
}; f'
# amend spesific commit
git config --global alias.fixup $'!f() {
TARGET=$(git rev-parse "$1")
git commit --fixup=$TARGET ${@:2} \\
&& EDITOR=true git rebase -i --autostash --autosquash $TARGET^
}; f'
# undo last commmit
git config --global alias.retract $'reset HEAD^'
# reset to upstream state
git config --global alias.rewind $'reset --hard @{upstream}'
# force push with lease
git config --global alias.thrust $'push --force-with-lease'
# open ignore file with $EDITOR
git config --global alias.ignore $'!f() {
if [ $# -eq 0 ]; then
${EDITOR:-vi} .gitignore
else
for IGNORE_PATH in "$@"
do
if !(grep -s "^${IGNORE_PATH}$" .gitignore >/dev/null); then
echo "$IGNORE_PATH" >> .gitignore
fi
done
fi
if [ -e .gitignore ]; then
git add .gitignore
fi
}; f'
# ignore changes of tracked file(s)
git config --global alias.ignore-change $'update-index --skip-worktree'
# colorized log graph
git config --global alias.graph \
$'log --graph --color=always
--date=format:\'%a %Y-%m-%d %H:%M\'
--pretty=tformat:\' %C(white)%s%C(reset)%C(auto)%d%C(reset)%n%C(dim white)•%C(reset) %C(dim blue)%h%C(reset) %C(dim cyan)%ad%C(reset) %C(dim green)%ar%C(reset) %C(dim white)%an%C(reset)\'
-m'
# get commit hash for HEAD by default or Branch or Tag
git config --global alias.hash $'!f() {
git rev-parse ${1:-HEAD}
}; f'
# git init with empty root commit
git config --global alias.bootstrap $'!git init && git commit -m root --allow-empty'
# short status
git config --global alias.situation $'status --short --branch'
# who am I
git config --global alias.whoami $'!f() {
printf "\e[1;34mname\e[0m " && git config user.name
printf "\e[1;34memail\e[0m " && git config user.email
}; f'
# execute for all git sub folders
git config --global alias.workspace $'!f() {
WORKSPACE=$PWD;
CMD="$@";
for repo in */; do
( cd $repo 2>/dev/null && git status >/dev/null 2>&1 && printf "\\e[34m${PWD#$WORKSPACE/}:\\e[39m\\n" && eval git $CMD && echo)
true
done
}; f'
############################################ Notes ############################################
#################
### Conditional Config Includes
#################
# git config --global "includeIf.gitdir:$PWD/.path" "$PWD/.gitconfig"
#
# git config --file "$PWD/.gitconfig" user.name "John Doe"
# git config --file "$PWD/.gitconfig" user.email "john.doe@example.com"
# git config --file "$PWD/.gitconfig" core.sshCommand "ssh -i ~/.ssh/id_rsa_example -F /dev/null"
#################
### Commit Signing Setup
#################
### GPG
# git config --global user.signingkey xxxxxxxx
# git config --global commit.gpgsign true # Always sign commits
### SSH
# git config --global gpg.format ssh
# git config --global user.signingkey ~/.ssh/id_ed25519.pub
# git config --global commit.gpgsign true # Always sign commits
#################
### Handy Commands
#################
## seach for changes
# git log --follow -G'<SEARCH_STRING>' --patch [ -- <FILE_PATH> ]
## show file in spesific version
# git show <COMMIT>:<FILE_PATH>
##################
# git commit --fixup=<commit>
# git rebase -- autosquash
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment