A better way to do git clone
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
.
Saurabh Kumar
Updated on June 21, 2022Comments
-
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 thecloned-folder
. The trick here is to identify thecloned-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 appropriateregex
, 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 likegit log
ormate .
which we do 99% of the time.Thanks in advance.