A better way to do git clone

13,014

Solution 1

bash function to do this (works in zsh also):

function lazyclone {
    url=$1;
    reponame=$(echo $url | awk -F/ '{print $NF}' | sed -e 's/.git$//');
    git clone $url $reponame;
    cd $reponame;
}

The awk command prints the part after the last / (e.g from http://example.com/myrepo.git to myrepo.git). The sed command removes the trailing .git

Usage:

$ pwd
~/
$ lazyclone https://github.com/dbr/tvdb_api.git
tvdb_api
Cloning into 'tvdb_api'...
remote: Counting objects: 1477, done.
remote: Compressing objects: 100% (534/534), done.
remote: Total 1477 (delta 952), reused 1462 (delta 940)
Receiving objects: 100% (1477/1477), 268.48 KiB | 202 KiB/s, done.
Resolving deltas: 100% (952/952), done.
$ pwd
~/tvdb_api

Solution 2

With git clone, you can specify the folder to clone to, instead of allowing it to be named automatically.

dir=myclone
git clone git://somerepo "$dir"
cd "$dir"
open "$dir"

Solution 3

An improvement from @dbr's answer:

function lazyclone {
    reponame=${1##*/}
    reponame=${reponame%.git}
    git clone "$1" "$reponame";
    cd "$reponame";
}

With the help of Bash's parameter expansion, we can get rid of awk and sed.

Share:
13,014
Saurabh Kumar
Author by

Saurabh Kumar

Updated on June 21, 2022

Comments

  • Saurabh Kumar
    Saurabh Kumar almost 2 years

    This is lazy programmer request. I would like to create a shell script automate the following process:

    git clone <remote-repo-url>
    cd <cloned-folder>
    open <cloned-folder> 
    

    So the idea here is to clone a URL and then immediately cd into the cloned-folder. The trick here is to identify the cloned-folder from the url pattern.

    For now we can assume that url structure is in this pattern .../<cloned-folder>.git i.e. the url.

    I am wondering if we can actually do with using awk or some tools like that. The part i am stuck is finding a appropriate regex, I guess.

    USE CASE: Here the use case is if you clone a url, you want to be in the repofolder as soon as possible. The is the pre-requirement if you want to run any git command like git log or mate . which we do 99% of the time.

    Thanks in advance.