From 5a40cb969f146cb9c73af0bd2695625ef453cdf6 Mon Sep 17 00:00:00 2001 From: Alexander Hess Date: Thu, 11 Jun 2026 18:32:27 +0200 Subject: [PATCH] Add configuration for `git` - Add .config/git/config + Define aliases + Configure default behavior in various situations + Add user information with signing key - Add .config/git/commit_msg_template.txt - Add .config/git/ignore - Integrate `git` aliases into the shell --- .bashrc | 63 --------- .config/git/commit_msg_template.txt | 17 +++ .config/git/config | 194 ++++++++++++++++++++++++++++ .config/git/ignore | 6 + .config/shell/aliases | 102 +++++++++++++++ 5 files changed, 319 insertions(+), 63 deletions(-) create mode 100644 .config/git/commit_msg_template.txt create mode 100644 .config/git/config create mode 100644 .config/git/ignore diff --git a/.bashrc b/.bashrc index e67c28a..0b44402 100644 --- a/.bashrc +++ b/.bashrc @@ -66,7 +66,6 @@ fi # Enable tab completion - if ! shopt -oq posix; then if [ -f /usr/share/bash-completion/bash_completion ]; then . /usr/share/bash-completion/bash_completion @@ -74,65 +73,3 @@ if ! shopt -oq posix; then . /etc/bash_completion fi fi - -# Add tab completion for all aliases to commands with completion functions -# (must come after bash completions have been set up) -# Source: https://superuser.com/a/437508 -_alias_completion() { - local namespace="alias_completion" - # parse function based completion definitions, where capture group 2 => function and 3 => trigger - local compl_regex='complete( +[^ ]+)* -F ([^ ]+) ("[^"]+"|[^ ]+)' - # parse alias definitions, where capture group 1 => trigger, 2 => command, 3 => command arguments - local alias_regex="alias ([^=]+)='(\"[^\"]+\"|[^ ]+)(( +[^ ]+)*)'" - # create array of function completion triggers, keeping multi-word triggers together - eval "local completions=($(complete -p | sed -Ene "/$compl_regex/s//'\3'/p"))" - (( ${#completions[@]} == 0 )) && return 0 - # create temporary file for wrapper functions and completions - rm -f "/tmp/${namespace}-*.tmp" # preliminary cleanup - local tmp_file; tmp_file="$(mktemp "/tmp/${namespace}-${RANDOM}XXX.tmp")" || return 1 - local completion_loader; completion_loader="$(complete -p -D 2>/dev/null | sed -Ene 's/.* -F ([^ ]*).*/\1/p')" - # read in " '' ''" lines from defined aliases - local line; while read line; do - eval "local alias_tokens; alias_tokens=($line)" 2>/dev/null || continue # some alias arg patterns cause an eval parse error - local alias_name="${alias_tokens[0]}" alias_cmd="${alias_tokens[1]}" alias_args="${alias_tokens[2]# }" - # skip aliases to pipes, boolean control structures and other command lists - # (leveraging that eval errs out if $alias_args contains unquoted shell metacharacters) - eval "local alias_arg_words; alias_arg_words=($alias_args)" 2>/dev/null || continue - # avoid expanding wildcards - read -a alias_arg_words <<< "$alias_args" - # skip alias if there is no completion function triggered by the aliased command - if [[ ! " ${completions[*]} " =~ " $alias_cmd " ]]; then - if [[ -n "$completion_loader" ]]; then - # force loading of completions for the aliased command - eval "$completion_loader $alias_cmd" - # 124 means completion loader was successful - [[ $? -eq 124 ]] || continue - completions+=($alias_cmd) - else - continue - fi - fi - local new_completion="$(complete -p "$alias_cmd")" - # create a wrapper inserting the alias arguments if any - if [[ -n $alias_args ]]; then - local compl_func="${new_completion/#* -F /}"; compl_func="${compl_func%% *}" - # avoid recursive call loops by ignoring our own functions - if [[ "${compl_func#_$namespace::}" == $compl_func ]]; then - local compl_wrapper="_${namespace}::${alias_name}" - echo "function $compl_wrapper { - (( COMP_CWORD += ${#alias_arg_words[@]} )) - COMP_WORDS=($alias_cmd $alias_args \${COMP_WORDS[@]:1}) - (( COMP_POINT -= \${#COMP_LINE} )) - COMP_LINE=\${COMP_LINE/$alias_name/$alias_cmd $alias_args} - (( COMP_POINT += \${#COMP_LINE} )) - $compl_func - }" >> "$tmp_file" - new_completion="${new_completion/ -F $compl_func / -F $compl_wrapper }" - fi - fi - # replace completion trigger by alias - new_completion="${new_completion% *} $alias_name" - echo "$new_completion" >> "$tmp_file" - done < <(alias -p | sed -Ene "s/$alias_regex/\1 '\2' '\3'/p") - . "$tmp_file" && \rm -f "$tmp_file" -}; _alias_completion diff --git a/.config/git/commit_msg_template.txt b/.config/git/commit_msg_template.txt new file mode 100644 index 0000000..5e556d9 --- /dev/null +++ b/.config/git/commit_msg_template.txt @@ -0,0 +1,17 @@ +# SUBJECT -> What does the commit do? +# - Imperative mood, no "." at the end +# - Start with "Add", "Fix", "Make", "Remove", ... +#---------- 50 characters / 1 line ---------------| + + +# BODY -> The why and how +# - Use plain text without formatting +# + Use "-" and "+" as bullets +# - "See: URL" to link to external resources +#---------- 72 characters / multiple lines -----------------------------| + + +# ISSUE TRACKER -> Uncomment one of the lines below +# Closes #41 +# Fixes #42 +# Resolves #43 diff --git a/.config/git/config b/.config/git/config new file mode 100644 index 0000000..c293d03 --- /dev/null +++ b/.config/git/config @@ -0,0 +1,194 @@ +[alias] + +# ~/.config/shell/aliases contains short `git` aliases, +# for example, `gap` for `git add --patch` + +# Synonyms as "specified" in the `git status` header +discard = checkout -- +unstage = reset HEAD -- + +amend-commit = !git log -n 1 --pretty=tformat:%B | git commit -F - --amend # Keep the commit message + +current-branch = !git rev-parse --abbrev-ref HEAD + +project-root = rev-parse --show-toplevel + +uncommit = reset --soft HEAD~1 + +# Sync the working directory into the index +sync-deleted = !git ls-files -z --deleted | xargs -r -0 git rm +sync = !git sync-deleted && git add . --all + +# Make minimal diff the default +diff-minimal = diff --color-words=. --ws-error-highlight=all +diff-last-commit = diff --color-words=. --ws-error-highlight=all HEAD +diff-staged = diff --color-words=. --ws-error-highlight=all --staged + +# Clean the project folder with intuitive commands +clean-all = !git reset --hard && git clean-ignored && git clean-untracked +clean-ignored = "!f() { if [ -f .python-version ]; then mv .python-version .python-version.XYZ; fi; if [ -f .env ]; then mv .env .env.XYZ; fi; git clean -X -d -f "$@"; if [ -f .python-version.XYZ ]; then mv .python-version.XYZ .python-version; fi; if [ -f .env.XYZ ]; then mv .env.XYZ .env; fi }; f" +clean-untracked = !git clean -x -d -e ".python-version" -e ".env" -f # because the "-e" flag does not work with "-X" + +# Delete EVERYTHING not reachable from a branch from the repository +gc-everything = "!f() { git -c gc.reflogExpire=0 -c gc.reflogExpireUnreachable=0 -c gc.rerereresolved=0 -c gc.rerereunresolved=0 -c gc.pruneExpire=now gc "$@"; }; f" + +# Make the logs look nice by default +log-format1 = log --pretty='%C(auto)%h: %s%d%Creset%n%C(dim)%an @ %ad => %ar%n' --date=format:'%a %Y-%m-%d %H:%M:%S %z' +log-format2 = log --pretty='%C(auto)%h: %s%d%Creset%n%C(dim)%aN @ %ad => %ar%n%+b' --date=format:'%a %Y-%m-%d %H:%M:%S %z' +last-commit = !git log-format2 -1 -p --stat +history = !git log-format2 --graph +summary = !git log-format1 --graph +oneline = log --pretty='%C(auto)%h: %s%d' --graph + +# Search the repository +grep-code = grep --break --context 1 --full-name --heading --line-number --show-function +grep-log = log --all --regexp-ignore-case --pretty='%C(auto)%h: %s%+D%Creset%n%C(reverse)%C(dim)%aN @ %ad = %ar%Creset%n%+b' --date=format:'%a %Y-%m-%d %H:%M:%S' --grep +grep-text = grep --break --context 1 --full-name --heading --ignore-case --line-number + +# Prune local branches that were deleted remotely +prune-delete = "!git fetch --prune && git branch -vv | grep ': gone]' | awk '{print $1}' | xargs git branch -d" +prune-show = "!git fetch --prune && git branch -vv | grep ': gone]' | awk '{print $1}'" + +aliases = config --get-regexp 'alias.*' +aliases-internal = !git config --list | grep 'alias\\.' | sed 's/alias\\.\\([^=]*\\)=\\(.*\\)/\\1/' | sort # used in ~/.config/shell/aliases + + +[clean] + +requireforce = true + + +[color "branch"] + +current = cyan dim bold reverse +local = green bold +remote = red bold + + +[color "decorate"] + +HEAD = cyan dim bold reverse +branch = green bold +remoteBranch = red bold +stash = magenta dim bold reverse +tag = magenta bold + + +[color "diff"] + +context = white +frag = blue dim bold reverse +meta = yellow dim bold reverse +new = green bold +old = red bold +whitespace = red dim bold reverse + + +[color "grep"] + +context = white +filename = yellow dim bold reverse +function = white bold +linenumber = blue dim bold reverse +match = red bold +selected = white +separator = blue dim bold reverse + + +[color "interactive"] + +error = red dim bold reverse +header = white +help = yellow bold +prompt = white dim bold reverse + + +[color "status"] + +added = green bold +branch = cyan dim bold reverse +changed = yellow bold +header = white +localBranch = green bold +nobranch = red dim bold reverse +remoteBranch = red bold +unmerged = yellow dim bold reverse +untracked = red bold + + +[commit] + +cleanup = strip +gpgSign = true +template = ~/.config/git/commit_msg_template.txt +verbose = true + + +[core] + +editor = vim +excludesfile = ~/.config/git/ignore +pager = less --chop-long-lines --ignore-case --LONG-PROMPT --status-column --quit-if-one-screen +whitespace = -space-before-tab,tab-in-indent + + +[diff] + +renames = true +submodule = log + + +[help] + +autocorrect = 50 + + +[init] + +defaultBranch = main + + +[merge] + +conflictstyle = diff3 +ff = only + + +[pull] + +ff = only +rebase = true + + +[push] + +autoSetupRemote = true +default = upstream +recursesubmodules = check + + +[rerere] + +enabled = true + + +[url "https://bitbucket.org/"] + +insteadOf = bb: + + +[url "https://github.com/"] + +insteadOf = gh: + + +[url "https://gitlab.com/"] + +insteadOf = gl: + + +[user] + +name = Alexander Hess +email = alexander@webartifex.biz +signingKey = alexander@webartifex.biz diff --git a/.config/git/ignore b/.config/git/ignore new file mode 100644 index 0000000..a0ed808 --- /dev/null +++ b/.config/git/ignore @@ -0,0 +1,6 @@ +# Generic temporary files +*.backup +*.bak +*.orig +*.temp +*.tmp diff --git a/.config/shell/aliases b/.config/shell/aliases index eaf280d..ed85ac8 100644 --- a/.config/shell/aliases +++ b/.config/shell/aliases @@ -5,3 +5,105 @@ # Manage the bare `git` repository in ~/ holding the dotfiles alias dotfiles='git --git-dir=$XDG_DATA_HOME/dotfiles --work-tree=$HOME' + + +# Integrate `git` +if _command_exists git; then + + # Check if a "main" branch exists in place of a "master" branch + git_main_branch() { + if [ -n "$(git branch --list main)" ]; then + echo "main" + else + echo "master" + fi + } + + alias g='git' + + alias ga='git add' + alias gap='git add --patch' + + alias gbr='git branch' + alias gbra='git branch --all' + alias gbrd='git branch --delete' + alias gbrdd='git branch --delete --force' + alias gbrm='git branch --move' + + alias gci='git commit' + alias gcim='git commit --message' + + alias gcl='git clone' + + alias gco='git checkout' + alias gcob='git checkout -b' + alias gcod='git checkout develop' + alias gcom='git checkout $(git_main_branch)' + + alias gcp='git cherry-pick' + + alias gdf='git diff-minimal' + alias gdlc='git diff-last-commit' + alias gds='git diff-staged' + + alias gfe='git fetch' + + alias ghi='git history' + alias ghia='git history --all' + + alias glc='git last-commit' + + alias glg='git log' + alias glga='git log --all' + + alias gme='git merge' + alias gmea='git merge --abort' + alias gmec='git merge --continue' + alias gmeff='git merge --ff-only' + alias gmenoff='git merge --no-ff' + + alias gol='git oneline' + alias gola='git oneline --all' + + alias gpl='git pull' + alias gplrb='git pull --rebase' + + alias gps='git push' + alias gpsf='git push --force' + + alias grb='git rebase --committer-date-is-author-date' + alias grba='git rebase --abort' + alias grbc='git rebase --continue' + alias grbi='git rebase --interactive' + alias grbq='git rebase --quit' + alias grbs='git rebase --skip' + + alias grepc='git grep-code' + alias grepl='git grep-log' + alias grept='git grep-text' + + alias grl='git reflog' + + alias grm='git rm' + + alias grs='git reset' + + alias grv='git revert' + + alias gs='git status' + alias gss='git status --short' + + alias gsh='git show' + + alias gst='git stash' + alias gsta='git stash push --include-untracked' + alias gstam='git stash push --include-untracked --message' + alias gstapp='git stash apply' + alias gstl='git stash list' + alias gstp='git stash pop' + alias gstsh='git stash show' + + alias gsu='git summary' + alias gsua='git summary --all' + +fi