Skip to content

Instantly share code, notes, and snippets.

@aaronedev
Last active September 26, 2025 09:00
Show Gist options
  • Select an option

  • Save aaronedev/aadc30adad46f4f16163dd09b53303e4 to your computer and use it in GitHub Desktop.

Select an option

Save aaronedev/aadc30adad46f4f16163dd09b53303e4 to your computer and use it in GitHub Desktop.
post-commit hook to push to all other remotes defined like codeberg or gitlab for example
#!/bin/bash
# Post-commit hook to automatically push to GitLab and Codeberg after local commits
# This runs after each local commit
#
# Toggle remotes by setting environment variables:
# PUSH_GITLAB=false git commit -m "message" # Disable GitLab push
# PUSH_CODEBERG=false git commit -m "message" # Disable Codeberg push
# PUSH_REMOTES=false git commit -m "message" # Disable all remote pushes
#
# Or set permanently in your shell profile:
# export PUSH_GITLAB=false
# export PUSH_CODEBERG=false
# Toggle settings (default to true if not set)
PUSH_GITLAB=${PUSH_GITLAB:-true}
PUSH_CODEBERG=${PUSH_CODEBERG:-true}
PUSH_REMOTES=${PUSH_REMOTES:-true}
# Override individual settings if PUSH_REMOTES is false
if [[ "$PUSH_REMOTES" == "false" ]]; then
PUSH_GITLAB=false
PUSH_CODEBERG=false
fi
# Colors for output (only used when stdout is a TTY)
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
if [[ -t 1 ]]; then
USE_COLOR=true
else
USE_COLOR=false
fi
log_and_print() {
local level=$1
shift
local message=$*
echo "$(date): $message" >>"$HOOK_LOG"
if [[ "$USE_COLOR" == true ]]; then
local color=$NC
case $level in
info) color=$BLUE ;;
success) color=$GREEN ;;
warn) color=$YELLOW ;;
error) color=$RED ;;
esac
printf "%b%s%b\n" "$color" "$message" "$NC"
else
echo "$message"
fi
}
# Log file for hook activity
HOOK_LOG=".git/hooks/post-commit.log"
# Log status (silent to terminal)
if [[ "$PUSH_GITLAB" == "true" && "$PUSH_CODEBERG" == "true" ]]; then
log_and_print info "Auto-pushing to GitLab and Codeberg..."
elif [[ "$PUSH_GITLAB" == "true" ]]; then
log_and_print info "Auto-pushing to GitLab only..."
elif [[ "$PUSH_CODEBERG" == "true" ]]; then
log_and_print info "Auto-pushing to Codeberg only..."
else
log_and_print warn "Remote pushing disabled"
exit 0
fi
# Get current branch
current_branch=$(git branch --show-current)
# Push to GitLab
if [[ "$PUSH_GITLAB" == "true" ]]; then
log_and_print info "Pushing branch $current_branch to GitLab..."
if git push gitlab "$current_branch" >/dev/null 2>&1; then
log_and_print success "Pushed $current_branch to GitLab"
else
log_and_print error "Failed to push $current_branch to GitLab"
fi
if git push gitlab --tags >/dev/null 2>&1; then
log_and_print success "Tags synced to GitLab"
else
log_and_print warn "Unable to push tags to GitLab"
fi
fi
# Push to Codeberg
if [[ "$PUSH_CODEBERG" == "true" ]]; then
log_and_print info "Pushing branch $current_branch to Codeberg..."
if git push codeberg "$current_branch" >/dev/null 2>&1; then
log_and_print success "Pushed $current_branch to Codeberg"
else
log_and_print error "Failed to push $current_branch to Codeberg"
fi
if git push codeberg --tags >/dev/null 2>&1; then
log_and_print success "Tags synced to Codeberg"
else
log_and_print warn "Unable to push tags to Codeberg"
fi
fi
log_and_print success "Post-commit hook complete"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment