How-to: Install Git

Installing Git from the command-line is as straightforward as executing this Bash script with your desired version for installation:

#!/bin/bash -e

#-------------------------------------------------------------------------------
# Default values
#-------------------------------------------------------------------------------
: ${GIT_VERSION:=$1}

echo "[INFO] git  '$GIT_VERSION'"

: ${DESTDIR:=$(pwd)}
: ${PREFIX:=${DESTDIR}/${GIT_VERSION}}
: ${WORKSPACE:=${PREFIX}/workspace}

#-------------------------------------------------------------------------------
# Sanity Checks
#-------------------------------------------------------------------------------
if [ -z "${GIT_VERSION}" ]; then
  echo "Usage: $0 "
  exit 1
fi

#-------------------------------------------------------------------------------
# Meta Information
#-------------------------------------------------------------------------------
GIT_SRCDIR="${WORKSPACE}/git-${GIT_VERSION}"
GIT_TARBALL="git-${GIT_VERSION}.tar.gz"
GIT_DOWNLOAD_URL="https://www.kernel.org/pub/software/scm/git/${GIT_TARBALL}"

#-------------------------------------------------------------------------------
# Workspace
#-------------------------------------------------------------------------------
mkdir -p "${WORKSPACE}" || exit 1
pushd    "${WORKSPACE}" || exit 1

#-------------------------------------------------------------------------------
# Download and unpack
#-------------------------------------------------------------------------------
if [ ! -f "$GIT_TARBALL" -a ! -f "${GIT_TARBALL%.xz}" ]; then
    echo "[INFO] Downloading GIT '$GIT_DOWNLOAD_URL'"
    wget --no-check-certificate "$GIT_DOWNLOAD_URL" || exit 1
else
    echo "[INFO] [SKIP] git tarball already exists: '$GIT_TARBALL'"
fi

if [ ! -d "$GIT_SRCDIR" ]; then
    echo "[INFO] Unpacking git tarball: '$GIT_TARBALL'"
    tar xzvf "${GIT_TARBALL}"
else
    echo "[INFO] [SKIP] git source code already exists: '$GIT_SRCDIR'"
fi

#-------------------------------------------------------------------------------
# Build and install
#-------------------------------------------------------------------------------
cd "${GIT_SRCDIR}" || exit 1

if [ ! -e "${PREFIX}/bin" ]; then
    echo "[INFO] Configuring git"
    echo "[INFO] Installing to '$PREFIX'"

    "${GIT_SRCDIR}/configure" --prefix="$PREFIX" || exit 1

    make -j all || exit 1
    make -j install || exit 1
fi

    echo "[INFO] Creating git environment setup file"
    cat > "${PREFIX}/setup.sh" <<-EOF
#!/bin/bash
#
# Automatically generated by $0 on $(date)
export GIT_VERSION="${GIT_VERSION}"
export GIT_HOME="${PREFIX}"
export PATH="\${GIT_HOME}/bin:\${PATH}"
export LD_LIBRARY_PATH="\${GIT_HOME}/lib:\${LD_LIBRARY_PATH}"
EOF

You can copy-and-paste the script above or just run it from my Github Gist on a single command-line with your desired version for installation:

$ GIT_VERSION=<desired_version> curl -s https://gist.githubusercontent.com/justintoo/a06e5941d9601e68f29d/raw/38bca4a6aad65dd6089f1f537f54b211f6c471ca/install-git.sh | bash /dev/stdin $GIT_VERSION

See the official Git documentation for the latest instructions if this script doesn't work for you.

Explore More