Skip to content

Instantly share code, notes, and snippets.

@shyamzzp
Created March 28, 2026 16:24
Show Gist options
  • Select an option

  • Save shyamzzp/161056cc818cbf8f7ab889ccbdb848e3 to your computer and use it in GitHub Desktop.

Select an option

Save shyamzzp/161056cc818cbf8f7ab889ccbdb848e3 to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
set -euo pipefail
SHORTCUTS_DIR="${HOME}/.config/git-shortcuts"
SHORTCUTS_FILE="${SHORTCUTS_DIR}/git-shortcuts.sh"
SOURCE_LINE='[ -f "$HOME/.config/git-shortcuts/git-shortcuts.sh" ] && source "$HOME/.config/git-shortcuts/git-shortcuts.sh"'
backup_if_exists() {
local file_path="$1"
if [ -f "${file_path}" ]; then
local backup_path
backup_path="${file_path}.backup.$(date +%Y%m%d%H%M%S)"
cp "${file_path}" "${backup_path}"
echo "Backed up ${file_path} -> ${backup_path}"
fi
}
ensure_source_line() {
local rc_file="$1"
if [ ! -f "${rc_file}" ]; then
touch "${rc_file}"
fi
if ! grep -Fqx "${SOURCE_LINE}" "${rc_file}"; then
printf '\n%s\n' "${SOURCE_LINE}" >> "${rc_file}"
echo "Updated ${rc_file}"
fi
}
write_shortcuts_file() {
cat > "${SHORTCUTS_FILE}" <<'EOF'
# shellcheck shell=bash
# Standalone git shortcuts that do not rely on ~/.gitconfig aliases.
git() {
if [ "$#" -eq 0 ]; then
command git
return
fi
local subcommand="$1"
shift
case "${subcommand}" in
co) command git checkout "$@" ;;
cob) command git checkout -b "$@" ;;
br) command git branch "$@" ;;
sw) command git switch "$@" ;;
swc) command git switch -c "$@" ;;
st) command git status -sb "$@" ;;
ss) command git status --short "$@" ;;
d) command git diff "$@" ;;
ds) command git diff --staged "$@" ;;
a) command git add "$@" ;;
aa) command git add --all "$@" ;;
unstage) command git reset HEAD -- "$@" ;;
undo) command git reset --soft HEAD~1 "$@" ;;
cm) command git commit -m "$@" ;;
cma) command git commit --amend "$@" ;;
cman) command git commit --amend --no-edit "$@" ;;
can) command git commit --amend --no-edit "$@" ;;
f) command git fetch --all --prune "$@" ;;
pl) command git pull --rebase "$@" ;;
ps) command git push "$@" ;;
pso) command git push -u origin HEAD "$@" ;;
rb) command git rebase "$@" ;;
rbi) command git rebase -i "$@" ;;
up)
local branch
branch="$(command git branch --show-current 2>/dev/null || true)"
if [ -z "${branch}" ]; then
echo "No branch detected."
return 1
fi
command git fetch origin && command git rebase "origin/${branch}"
;;
last) command git log -1 HEAD --stat "$@" ;;
l) command git log --oneline --decorate -n 20 "$@" ;;
lg) command git log --graph --decorate --oneline --all "$@" ;;
who) command git shortlog -sn --all "$@" ;;
bclean)
command git branch --merged | grep -v "*" | grep -v " main$" | grep -v " master$" | xargs -n 1 command git branch -d
;;
cop) command git checkout main && command git pull ;;
df) command git diff main --name-only "$@" ;;
nuke)
local branch
branch="${1:-$(command git branch --show-current)}"
if [ -z "${branch}" ]; then
echo "No branch detected"
return 1
fi
if [ "${branch}" = "main" ] || [ "${branch}" = "master" ]; then
echo "Resetting protected branch: ${branch} to origin/${branch}"
command git fetch origin
command git reset --hard "origin/${branch}"
command git clean -fd
return 0
fi
command git reset --hard
command git clean -fd
command git checkout main
command git branch -D "${branch}"
command git push origin --delete "${branch}"
command git fetch origin
command git reset --hard origin/main
command git clean -fd
;;
*)
command git "${subcommand}" "$@"
;;
esac
}
EOF
}
echo "Installing standalone git shortcuts for user: ${USER}"
mkdir -p "${SHORTCUTS_DIR}"
backup_if_exists "${SHORTCUTS_FILE}"
write_shortcuts_file
chmod 0644 "${SHORTCUTS_FILE}"
ensure_source_line "${HOME}/.zshrc"
ensure_source_line "${HOME}/.bashrc"
echo
echo "Installed shortcuts file: ${SHORTCUTS_FILE}"
echo "Done. Open a new shell or run: source ~/.zshrc"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment