Git clone repository in a specific folder but keep default folder name

坚强是说给别人听的谎言 提交于 2019-12-10 11:49:23

问题


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


来源:https://stackoverflow.com/questions/53511768/git-clone-repository-in-a-specific-folder-but-keep-default-folder-name

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!