Git clone repository in a specific folder but keep

2019-06-14 04:15发布

问题:

So I'm using the git clone command but when I try to clone the repository into a specific folder it doesn't create a folder like it does normally. For example when I use it like this

git clone https://github.com/username/repositoryName.git

it creates a folder named repositoryName and stores the repository in there. When I'm using it like this

git clone https://github.com/username/repositoryName.git myFolderName

it doesn't create that default folder it would create. It just saves it in myFolderName. The thing is that I want it stored in that default folder but I want that default folder in myFolderName. I can't use mv command because I don't know the defaults folder name and I clone a lot of repositories at the same time. Any ideas?

Thanks in advance.

回答1:

What about something like this:

## Your links in an array
declare -a arr=("https://github.com/username/repositoryName" "https://github.com/username/repositoryName2")

## Folder to store each of these git repos
folder=myFolderName

## Go through each link in array
for i in "${arr[@]}"
do
    ## Use basename to extract folder name from link
    git clone $i $folder/$(basename $i)
done

Which will produce your following git repositories:

myFolderName/repositoryName
myFolderName/repositoryName2

Note: You don't need to add .git at the end of each HTTPS link for git clone.

If you need to include .git for some reason, you can strip it with:

git clone $i $folder/$(basename ${i%.*})


回答2:

You should be able to do it with,

git clone https://github.com/username/repositoryName.git myFolderName/repositoryName

PS: This is how you could split the repository name from the URL

basename -s .git $(echo "https://github.com/username/repositoryName.git")


回答3:

Assuming that myFolderName already exists the simplest solution is this:

git -C myFolderName clone https://github.com/username/repositoryName.git