2

How to git clone a project and then cd into the newly created directory in one action?

git clone http//xxx (optional folder name)
cd <created directory>

I found several solutions in Bash, like this one:
A better way to do git clone

But none in PowerShell..

Community
  • 1
  • 1
Laoujin
  • 9,962
  • 7
  • 42
  • 69
  • 3
    Do you really clone so many times a day that it's worth our time to post this question? – Jonathon Reinhart Sep 12 '15 at 13:22
  • 2
    I guess it depends on where you want to stop being more efficient. Even if you only do it once a week, why not improve the workflow. A next step to this script could be to check if there is a package.json and if so also do an npm install & npm start. So you clone a repository and go get a coffee while it does its work instead of waiting at the commandline for the previous command to finish. – Laoujin Jul 20 '20 at 17:38
  • If you're not cloning nine repos before breakfast, what are you really doing in life? – Mavaddat Javid Jan 29 '22 at 05:21

3 Answers3

5

It really isn't very complicated to turn your referenced bash solution into PowerShell:

function Invoke-GitClone($url) {
  $name = $url.Split('/')[-1].Replace('.git', '')
  & git clone $url $name | Out-Null
  Set-Location $name
}
Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
0

Well, I'm doing some research again and am cloning lots of repositories.

The following snippet supports passing a custom directory name like git clone also does which Ansgar Wiechers' answer did not take into account.

function Invoke-GitClone($url, $name) {
    if (!$name) {
        $name = $url.Split('/')[-1].Replace('.git', '')
    }
    & git clone $url $name | Out-Null
    Push-Location $name
}

Set-Alias gitclone Invoke-GitClone

I never got around posting a full solution because this still doesn't support passing flags like --recursive.

Laoujin
  • 9,962
  • 7
  • 42
  • 69
0

One-liner for true git and PowerShell aficionados:

(git clone https://github.com/BLAH/BLUH.git 2>&1) -replace ".+'([^']+)'.+",'$1' | Set-Location

Why does this work? We use output stream redirection. See the git manual for more info.

Mavaddat Javid
  • 491
  • 4
  • 19