Skip to content

Instantly share code, notes, and snippets.

@dineshr93
Last active August 12, 2025 09:48
Show Gist options
  • Save dineshr93/544427ffb1bf7f1e4c8d08841bfc95ed to your computer and use it in GitHub Desktop.
Save dineshr93/544427ffb1bf7f1e4c8d08841bfc95ed to your computer and use it in GitHub Desktop.
cdgit - clone a git repository and cd into the cloned directory automatically
# cdgit - clone a git repository and cd into the cloned directory automatically
#
# Usage:
# cdgit clone <repo-url> [directory]
#
# Examples:
# Just prepend to your normal git clone command with 'cd'
# cdgit clone https://github.com/dineshr93/sq.git
# - Clones the repo and cds into the folder named 'sq'
#
# cdgit clone https://github.com/dineshr93/sq.git myfolder
# - Clones the repo into 'myfolder' and cds into 'myfolder'
#
# If the clone fails, it will print an error and not cd.
#
# Add the below function to ~/bashrc and source ~/bashrc
function cdgit() {
if [ "$1" != "clone" ]; then
echo "Usage: cdgit clone <repo-url> [directory]"
return 1
fi
# Run git clone with all arguments except the first
git clone "${@:2}"
if [ $? -ne 0 ]; then
echo "git clone failed"
return 1
fi
# If user specified a directory name explicitly (third argument)
if [ $# -ge 3 ]; then
target_dir="${!#}"
else
# Extract directory name from repo URL (second argument)
repo_url="$2"
target_dir=$(basename "$repo_url" .git)
fi
cd "$target_dir" || echo "Failed to cd into $target_dir"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment