Very often, I clone repositories on GitHub, at least one every day. Usually I would save the web page link in my note file, and I always go back to the page and grab that SSH link for cloning. For some reason, I always guessed wrong about the username git@, so I stopped editing the link but copied from the web pages.

I only clone via SSH, never via Git Smart HTTP, even GitHub staff tries to sell that way of cloning. But the point of this post is to have one simple way to clone a link from GitHub and you don’t need to think what type that is, you are guaranteed to have the repositories cloned via SSH.

So, after so many copy-and-pasting since 2008, I finally wrote a simple Bash function to help me:

########################################################
# ghclone: Cloning GitHub repository with different URLs

# Usage: gitclone <url>
# url could be one of:
# Smart HTTP: https://github.com/{user}/{repo}.git
# SSH       : git@github.com:{user}/{repo}.git
# Subversion: https://github.com/{user}/[repo}

ghclone() {
  local URL="${1%.git}" user repo
  repo="${URL##*/}"
  URL="${URL%/$repo}"
  user="${URL##*[:/]}"

  git clone "git@github.com:$user/$repo" && cd "$repo"
}

As the comment says, it should be able to deal with three types of links and clone the repository via SSH, then enters the cloned local copy from you, sort of like my another wt() Bash function in bash_helper_func.sh():

Smart HTTP
https://github.com/{user}/{repo}.git
SSH
git@github.com:{user}/{repo}.git
Subversion / web page URL
https://github.com/{user}/[repo}

There probably is more smarter way to do, but this is what I get now and I am happy with this way. Nevertheless, if any way is better than this, I am happy to hear about that.