Skip to content

Instantly share code, notes, and snippets.

@aks
Created June 22, 2023 22:08
Show Gist options
  • Save aks/c9c15e3920062d749af88542e5b123fb to your computer and use it in GitHub Desktop.
Save aks/c9c15e3920062d749af88542e5b123fb to your computer and use it in GitHub Desktop.
git-aliases.sh
# Git helpers
alias gadd='git add'
alias gci='git commit'
alias gco='git checkout'
alias gdiff='git diff'
alias gpull='git pull'
alias gpullhead='git pull origin $(git rev-parse --abbrev-ref HEAD)'
alias gphd='gpullhead'
alias gpush='git push'
alias grba='git rebase --abort'
alias grbc='git rebase --continue'
alias grbs='git rebase --skip'
alias gsf='git showfiles'
alias gst='git status -s'
alias gsta='git status'
alias ghelp="git_aliases | format_aliases"
# These are aliases to long-name functions, making them self-documenting
alias gscf='git_show_changed_files'
alias gsnmf='git_show_new_modified_files'
alias gscrsf='git_show_changed_rspec_files'
alias gscrbf='git_show_changed_ruby_files'
alias gscerbf='git_show_changed_erb_files'
alias gscjsf='git_show_changed_js_files'
alias gsccssf='git_show_changed_css_files'
alias gscwebf='git_show_changed_web_files'
alias grspec='git_show_changed_ruby_files | pass_as_args_to_rspec'
alias grucop='git_show_changed_ruby_files | pass_as_args_to_rubocop'
git_show_changed_files() {
if [[ -d .git ]]; then
git diff --name-status `git_default_branch`..
else
echo 1>&2 "Not a git repo: $PWD"
fi
}
git_default_branch() {
local name=`(git rev-parse --abbrev-ref origin/HEAD | xargs basename) 2>/dev/null`
echo "${name:-main}"
}
git_show_new_modified_files() {
git_show_changed_files | filter_non_deleted_git_files
}
git_show_changed_rspec_files() {
git_show_new_modified_files | filter_rspec_files
}
git_show_changed_ruby_files() {
git_show_new_modified_files | filter_ruby_files
}
git_show_changed_erb_files() {
git_show_new_modified_files | filter_erb_files
}
git_show_changed_js_files() {
git_show_new_modified_files | filter_js_files
}
git_show_changed_css_files() {
git_show_new_modified_files | filter_css_files
}
git_show_changed_web_files() {
git_show_new_modified_files | filter_web_files
}
pass_as_args_to_rubocop() {
pass_as_args_to_bundle_cmd 'rubocop'
}
pass_as_args_to_rspec() {
pass_as_args_to_bundle_cmd 'rspec'
}
pass_as_args_to_bundle_cmd() {
xargs bundle exec $1
}
filter_non_deleted_git_files() {
awk '/^[^D]/ {print $2}'
}
filter_rspec_files() {
egrep '^spec/.*_spec.rb'
}
filter_ruby_files() {
egrep '[.]rb$'
}
filter_erb_files() {
egrep '[.]erb$'
}
filter_css_files() {
egrep '[.](s?css\.erb|s?css)$'
}
filter_web_files() {
egrep '[.](html|s?css|html\.erb|s?css\.erb)'
}
filter_js_files() {
egrep '[.][jt]s$' # include typescript also
}
git_aliases() {
grep_aliases 'git|gscf|gsnmf|gsrsf|gsrbf|gphd'
}
format_aliases() {
ruby -p -e "\$_ = format('%10s = %s', *(\$_.split('=', 2)))"
}
grep_aliases() {
sorted_aliases | egrep "$1"
}
sorted_aliases() {
alias | sort_aliases
}
sort_aliases() {
sed -e 's/^alias //' | sort -t= -k1
}
unmerged_git_files() {
git ls-files -u | awk '{print $4}' | sort -u
}
alias vimum='vim `unmerged_git_files`'
alias gaddum='git add `unmerged_git_files`'
# Add in git-completion.bash
run_file ~/.git-completion.bash
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment