Skip to content

Instantly share code, notes, and snippets.

@cesalazar
Last active September 27, 2022 10:56
Show Gist options
  • Save cesalazar/1bed32c36c79b0dbcd54ae4dde56f323 to your computer and use it in GitHub Desktop.
Save cesalazar/1bed32c36c79b0dbcd54ae4dde56f323 to your computer and use it in GitHub Desktop.
Clone a git repository and cd into its directory
#!/bin/bash
#
# Clone a git repository and cd into it
#
# NOTE: this file needs to be *sourced*. E.g.
# alias gclin="source /path/to/git-clone-and-auto-cd.sh"
#
# (my personal mnemonic for (g)it (cl)one (in))
# gclin https://github.com/cesalazar/.vim.git my_vim
# Get the name of the folder from the URL or the 2nd argument
folder="$(echo "${@//\// }" | awk '{print $NF}')"
# Remove .git suffix
folder="${folder/.git/}"
errors=0
# Check if the folder exists already
[[ -d "${folder}" ]] && echo "Folder ${folder} exists already" && ((errors++))
# Check if the git executable is in the path
[[ ! $(command -v git) ]] && echo "No git executable found" && ((errors++))
# We intentionally do not exit so the terminal session isn't closed
[[ ${errors} -gt 0 ]] && echo "Errors found, aborting" && return
# Clone and cd into the folder
git clone "${1}" "${folder}" && cd "${folder}" || return
# vim: fdm=manual tabstop=4 softtabstop=4 shiftwidth=4 expandtab:
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment