Contents

Defaulting git clone to shallow (depth=1)

Shallow clones are faster and smaller

Contents

Before adding this to my shell config, I would manually add –depth=1 to all my git clones.

I never saw the value in downloading the entire source history and (at times incorrectly) committed files to my local machine unless I was cloning the repo for the purpose of backing it up.

It seems strange to me that git doesn’t have a config option to set this as default behaviour.

My hack for this is a simple zsh/bash function that can wrap git commands - and as such add clone depth if it’s not specified.

function git_wrapper() {

# If invoked by another function, alias or xargs, interpret it as normal

  if [[ -n ${FUNCNAME[*]} ]] || [[ -n $ALIASES ]] || [[ -n $XARGS ]]; then
    command git "$@"
    return
  fi

# clone with depth=1 if no depth is not specified

  if [[ $1 == "clone" ]] && [[ $* != *"--depth"* ]]; then
    shift
    command git clone --depth=1 "$@"
  else
    command git "$@"
  fi
}

alias git='git_wrapper'

Maybe there’s a better way of doing this?