Skip to content

Instantly share code, notes, and snippets.

@jodyheavener
Created February 27, 2020 04:50
Show Gist options
  • Select an option

  • Save jodyheavener/ad97cccaf2010ffc3935beea98c9f717 to your computer and use it in GitHub Desktop.

Select an option

Save jodyheavener/ad97cccaf2010ffc3935beea98c9f717 to your computer and use it in GitHub Desktop.
Override git-push builtin to always use --force-with-lease any time --force is used, unless you also specify --seriously
function git {
# Only touch git if it's the push subcommand
if [[ "$1" == "push" ]]; then
force=false
override=false
for param in "$@"; do
if [[ $param == "--force" ]]; then force=true; fi
if [[ $param == "--seriously" ]]; then override=true; fi
done
# If we're using --force but not --seriously, change it to --force-with-lease
if [[ "$force" = true && "$override" = false ]]; then
echo -e "\033[0;33mDetected use of --force! Using --force-with-lease instead. If you're absolutely sure you can override with --force --seriously.\033[0m"
# Unset --force
for param; do
[[ "$param" = --force ]] || set -- "$@" "$param"; shift
done
# Replace it with --force-with-lease
set -- "push" "$@" "--force-with-lease"; shift
else
if [[ "$override" = true ]]; then
echo -e "\033[0;33mHeads up! Using --force and not --force-with-lease.\033[0m"
fi
# Unset --seriously or git will yell at us
for param; do
[[ "$param" = --seriously ]] || set -- "$@" "$param"; shift
done
fi
fi
command git "$@"
}
@fqlx
Copy link

fqlx commented May 5, 2022

@jodyheavener How do you install this with zsh ?

@ZevEisenberg
Copy link

Note that -f is a valid abbreviation of --force. This makes it work with both:

function git {
  # Only touch git if it's the push subcommand
  if [[ "$1" == "push" ]]; then
    force=false
    override=false

    for param in "$@"; do
      if [[ $param == "--force" || $param == "-f" ]]; then force=true; fi
      if [[ $param == "--seriously" ]]; then override=true; fi
    done

    # If we're using --force but not --seriously, change it to --force-with-lease
    if [[ "$force" = true && "$override" = false ]]; then
      echo -e "\033[0;33mDetected use of --force! Using --force-with-lease instead. If you're absolutely sure you can override with --force --seriously.\033[0m"

      # Unset --force
      for param; do
        [[ "$param" = --force ]] || set -- "$@" "$param"; shift
      done
      
      # Unset -f
      for param; do
        [[ "$param" = -f ]] || set -- "$@" "$param"; shift
      done

      # Replace it with --force-with-lease
      set -- "push" "$@" "--force-with-lease"; shift
    else
      if [[ "$override" = true ]]; then
        echo -e "\033[0;33mHeads up! Using --force and not --force-with-lease.\033[0m"
      fi

      # Unset --seriously or git will yell at us
      for param; do
        [[ "$param" = --seriously ]] || set -- "$@" "$param"; shift
      done
    fi
  fi

  command git "$@"
}

@ZevEisenberg
Copy link

@fqlx you install it by pasting it into your ~/.zshrc file, or equivalent if you use oh-my-zsh or some other layer on top of ~/.zshrc.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment