I like to organize Github repos so that the actual repo folder clones into a parent folder with the name of the org or user that owns the repo. This helps me organize projects under one org or user and keep my Repos folder neater.
For example, if I'm cloning github.com/xyz/project1, I create a folder called xyz
, then git clone
into that folder.
Is there a way that I can automate this so that git clone
will make the parent folder for me? I'm thinking I'll need to make a custom script to do it, but if git has a built-in feature for that, I'd prefer it. For example:
$ cd ~/Repos
$ git clone https://github.com/xyz/project1.git
$ ls
xyz
$ cd xyz && ls
project1
Git itself doesn't provide a built-in way to automatically create a parent directory based on the GitHub org/user. However, you can easily automate this with a small shell script.
Here's a Bash script that clones a GitHub repo and places it into a directory named after the org/user:
#!/bin/bash
# Usage: git-org-clone https://github.com/org/repo.git
url="$1"
if [[ -z "$url" ]]; then
echo "Usage: $0 <git-repo-url>"
exit 1
fi
# Extract org/user and repo name
org=$(echo "$url" | awk -F[/:] '{print $(NF-1)}')
repo=$(basename "$url" .git)
# Make org directory and clone into it
mkdir -p "$org"
git clone "$url" "$org/$repo"