Skip to content

Instantly share code, notes, and snippets.

@undergroundwires
Last active August 18, 2025 09:49
Show Gist options
  • Save undergroundwires/5f5d4cdd8973a72f03c8588a2d033dbb to your computer and use it in GitHub Desktop.
Save undergroundwires/5f5d4cdd8973a72f03c8588a2d033dbb to your computer and use it in GitHub Desktop.
Helpful git aliases
#!/usr/bin/env bash
# bash: `bash ./alias.sh` (requires sudo in macOS)
# zsh: `zsh ./alias.sh`
main() {
local -ra aliases=(
# gitgo : Alias for git add, commit with amend, update commit date and force push
"alias gitgo='git add . && git commit --amend --no-edit --reset-author && git push -f'"
# gitc : Alias for counting total characters in last commi heading
"alias gitc='git log --pretty=format:%Creset%s --no-merges -1 | wc -c'"
# gita : Alias for amending to latest commit
"alias gita='git add . && git commit --amend --no-edit --allow-empty'"
)
# Process each alias for each shell config file
while IFS= read -r file; do
for alias in "${aliases[@]}"; do
set_persistent_alias "$alias" "$file"
done
done < <(get_alias_files)
if is_windows; then
windows_init_path
fi
}
set_persistent_alias() {
local -r alias="$1"
local -r file="$2"
if ! create_file "$file"; then
log_error "Failed to create the file: $file."
return 1
fi
if grep -Fxq "$alias" "$file"; then
echo "[$file] Alias already exists: $alias"
else
command="echo \"$alias\" >> \"$file\""
if type "sudo" &> /dev/null; then # Git Bash on Windows does not have sudo
command="sudo $command"
fi
if eval "$command"; then
echo "[$file] Saved alias"
else
log_error "[$file] Failed to save alias"
fi
fi
# shellcheck disable=SC1090
source "$file"
if is_windows; then
windows_set_alias "$alias"
fi
}
windows_set_alias() {
local -r alias="$1"
# Initialize Windows environment
local batch_dir="$HOME/bin"
create_dir "$batch_dir"
local shell_cmd
if command_exists 'bash'; then
shell_cmd="bash"
elif command_exists 'sh'; then
shell_cmd="sh"
else
log_error "No shell found (bash or sh)"
return 1
fi
local alias_name
alias_name=$(echo "$alias" | sed -n "s/alias \([^=]*\)=.*/\1/p")
if [[ -z "$alias_name" ]]; then
log_error "Failed to parse alias name: $alias"
return 1
fi
# Extract the alias command - everything after 'alias name='
local alias_command
alias_command=$(echo "$alias" | sed "s/alias $alias_name=//")
# Remove the outer quotes if present
if [[ "$alias_command" =~ ^\'.*\'$ ]]; then
alias_command="${alias_command:1:-1}"
elif [[ "$alias_command" =~ ^\".*\"$ ]]; then
alias_command="${alias_command:1:-1}"
fi
if [[ -z "$alias_command" ]]; then
log_error "Failed to parse alias command: $alias"
return 1
fi
# Create a batch file for the alias
local batch_file="$batch_dir/$alias_name.bat"
if ! create_file "$batch_file"; then
log_error "Failed to create the batch file: $batch_file."
return 1
fi
# Escape special characters for batch files
local escaped_command
escaped_command=$(echo "$alias_command" | sed 's/"/\\"/g')
# Write the batch file content
cat > "$batch_file" << EOF
@echo off
$shell_cmd -c "$escaped_command"
EOF
echo "[$batch_file] Created Windows batch alias"
}
windows_init_path() {
local unix_batch_dir="$HOME/bin"
if [[ ":$PATH:" != *":$unix_batch_dir:"* ]]; then
echo "Adding $unix_batch_dir to PATH for current session"
export PATH="$unix_batch_dir:$PATH"
fi
if ! command_exists 'reg'; then
log_error 'Reg command is missing, cannot update PATH variable.'
return 1
fi
local -r windows_batch_dir=$(convert_to_windows_path "$unix_batch_dir")
local current_path
current_path=$(MSYS_NO_PATHCONV=1 reg query "HKCU\Environment" /v "PATH" 2>/dev/null | grep PATH | sed 's/.*REG_SZ//')
if [[ -n "$current_path" && "$current_path" != *"$windows_batch_dir"* ]]; then
echo "Adding $windows_batch_dir to PATH permanently (requires admin rights)"
MSYS_NO_PATHCONV=1 \
reg add "HKCU\Environment" \
/v "PATH" \
/t "REG_EXPAND_SZ" \
/d "$current_path;$windows_batch_dir" \
/f
fi
}
get_alias_files() {
local -a files=()
if command_exists 'zsh'; then
files+=("$HOME/.zshrc")
fi
if command_exists 'bash'; then
if [ "$(uname -s)" == "Darwin" ]; then
files+=("$HOME/.bash_profile")
else # tested on Windows
files+=("$HOME/.bashrc")
fi
fi
if [ ${#files[@]} -eq 0 ]; then
log_error 'Unkown shell'
exit 1
fi
printf "%s\n" "${files[@]}"
}
is_windows() {
local -r os=$(uname -s)
# Check for MINGW (with or without version info), CYGWIN, or Windows_NT
[[ "$os" =~ ^MINGW(32|64)_NT ]] || [[ "$os" =~ ^CYGWIN_ ]] || [[ "$os" == "Windows_NT" ]]
}
convert_to_windows_path() {
local unix_path="$1"
# Try to use cygpath if available
if command_exists 'cygpath'; then
cygpath -w "$unix_path"
return
fi
# Fallback to manual conversion
# Convert /c/Users/bob/bin to C:\Users\bin
echo "$unix_path" | sed -e 's|^/\([a-zA-Z]\)/|\1:\\|' -e 's|/|\\|g'
}
create_dir() {
local dir_path="$1"
if [ -d "$dir_path" ]; then
return 0
fi
echo "Creating directory: $dir_path"
if ! mkdir -p "$dir_path"; then
log_error "Failed to create the directory: $dir_path"
return 1
fi
return 0
}
create_file() {
local file_path="$1"
if [ -z "$file_path" ]; then
log_error 'Missing file path.'
return 1
fi
# If it's a directory, just create it
if [[ "$file_path" == */ ]]; then
if ! create_dir "$file_path"; then
return 1
fi
return 0
fi
local parent_dir
if ! parent_dir=$(dirname "$file_path"); then
log_error "Could not determine the parent directory for the path: $file_path"
return 1
fi
if ! create_dir "$parent_dir"; then
return 1
fi
if [ ! -f "$file_path" ]; then
echo "Creating file: $file_path"
if ! touch "$file_path"; then
log_error "Failed to create the file: $file_path"
return 1
fi
fi
}
command_exists() {
local -r command_name="$1"
command -v "$command_name" &> /dev/null
}
log_error() {
local -r message="$1"
>&2 echo "Error: $message"
}
main
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment