Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

A better way to do git clone

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.

like image 918
Saurabh Kumar Avatar asked Aug 21 '12 15:08

Saurabh Kumar


People also ask

Is it better to clone or download in GitHub?

When you download the repo it just gives you all the source files with no . git so you dont have the repo. When you clone you get a copy of the history and it is a functional git repo.

Is git clone necessary?

git clone is primarily used to point to an existing repo and make a clone or copy of that repo at in a new directory, at another location. The original repository can be located on the local filesystem or on remote machine accessible supported protocols. The git clone command copies an existing Git repository.

Is cloning the same as copying git?

Cloning a repository gives you a copy of that repository and configures the original repository as a remote. Copying a repository just gives you a copy of that repository. (Though you can of course just add the remote definition afterwards via git remote add .)

Can you git clone in CMD?

Open “Git Bash” and change the current working directory to the location where you want the cloned directory. Type git clone in the terminal, paste the URL you copied earlier, and press “enter” to create your local clone.


2 Answers

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
like image 105
dbr Avatar answered Sep 25 '22 05:09

dbr


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"
like image 36
jordanm Avatar answered Sep 23 '22 05:09

jordanm