问题
Is it possible to git clone
multiple git repositories with one command (for example: git clone "1.git,2.git,3.git.."
in one local directory?
回答1:
You can find script example like this one:
I have this file called "clone" containing URLs of several git repos (taken from djangosites.com. Awesome site. Must visit)
Snippet:
$ cat clone
https://github.com/igorsobreira/igorsobreira.com https://github.com/ella/ella https://github.com/divio/django-cms/ https://github.com/palewire/palewire.com https://github.com/jabapyth/jfcom https://github.com/humanfromearth/snippify https://github.com/scaphilo/koalixcrm https://github.com/jlev/Boycott-Toolkit https://github.com/jbalogh/zamboni/ https://github.com/ASKBOT/askbot-devel https://github.com/emesik/djiki https://github.com/vicalloy/LBForum https://github.com/agiliq/agiliq https://github.com/bartTC/dpaste.de https://github.com/bartTC/django-paste https://github.com/bartTC/dpaste_de/ https://github.com/fotochest/fotochest https://esp.mit.edu/git/esp-project.git https://github.com/titan2x/bashoneliners.git
Apparently, it's harder to clone multiple repos at once (
git clone <repo1> <repo2> ... <repon>
does not work). So I wrote this short bash code to make it work:Code:
atm in /home/atm/git/django_repos
$ for f in `cat clone`; do `git clone $f`; done
You would find many more on gist.github.com, like this one, to clone all your repos from GitHub:
#!/bin/bash
#
# Copyright 2011, Tim Branyen @tbranyen <tim@tabdeveloper.com>
# Dual licensed under the MIT and GPL licenses.
#
# Automatically clone single or multiple repos into a folder,
# great for setting up a git projects folder.
#
# Install: curl https://gist.github.com/raw/902154/github.sh > /usr/local/bin/gh
# chmod +x /usr/local/bin/gh
#
# Internal properties
GITHUB_PREFIX=git@github.com:
GITHUB_USERNAME=$(git config --global github.user)
function main {
# Improperly configured user
detect_user
# Missing arguments
args=$1
if [ -z $args ]; then
echo '
gh: try ''`gh --help`'' for more information
'
exit
fi
# Display help text
if [ $args = '--help' ]; then
echo '
Clone repos from your GitHub
gh repo1 repo2
Clone repos from others GitHub
gh username/repo1 username/repo2
Clone mixed repos:
gh repo1 username/repo2
Clone line separated repos from file:
cat file | xargs gh
'
exit
fi
# Parse arguments and clone repos.
find_repos
}
function detect_user {
# If no username configured, attempt to pull from git --config
if [ -n "$GITHUB_USERNAME" ]; then
USERNAME=$GITHUB_USERNAME
else
echo '
gh: missing username
configure username with ''`git config --global github.user username`''
'
exit
fi
}
function find_repos {
for repo in $args; do
# If a user provides the parameter username/repo pull in that specific repository.
if [ `awk -v repo="$repo" -v delimit="/" 'BEGIN{print index(repo,delimit)}'` -ne 0 ]; then
echo "Pulling in $repo";
git clone $GITHUB_PREFIX$repo.git
# Default to you.
else
echo "Pulling in $USERNAME/$repo";
git clone $GITHUB_PREFIX$USERNAME/$repo.git
fi
done
}
main $*
More generally, a scripting approach is needed, and lilgeek mentioned bl4ckbo7/agt, a python script which includes cloning with fastest and parallel clone processing feature.
回答2:
This is how almost solved for myself:
git clone https://myrepo.com/folder.git && \
git clone https://myrepo.com/folder2.git && \
git clone https://myrepo.com/folder3.git
This is way easier to build using a code editor like Sublime or VSCode.
The only downside for me: If you did not store your credentials, you're gonna have to type it over and over.
回答3:
I have created a sample script for my project. Our project includes lots of repositories which needs to be cloned when a fresh person joins the team.
So here is the content of script, which you can save into some executable files and execute.
echo -n Please Enter your GitHub Username:
read username
echo -n Please Enter your GitHub Password
read -s password // doesn't echoes the password on screen
git clone
https://$username:$password@github.com/location/reponame1.git
https://$username:$password@github.com/location/reponame2.git
....
https://$username:$password@github.com/location/reponamen.git
Its quiet useful to avoid boring manual efforts and ensure that all required projects are cloned in one go.
Hope my answer helps someone else as well.
回答4:
You can use a mix of solutions.
Use git's credential.helper to set a cache timeout (see this), then you can use a script/list like the one suggested by Fábio. This way, you'll only have to type your credentials once (usually, unless a clone takes longer than the cache timeout.
git config --global credential.helper 'cache timeout 3600'
git clone https://myuser@bitbucket.org/myproject/myrepo.git
### password should be prompted
git clone https://myuser@bitbucket.org/myproject/mysecondrepo.git
### look ma, no password prompt!
git clone https://myuser@bitbucket.org/myproject/mythirdrepo.git
git clone https://myuser@bitbucket.org/myproject/myfourthrepo.git
git clone https://myuser@bitbucket.org/myproject/andsoforthrepo.git
That's still sequential, but it helps and is pretty simple, IMHO.
回答5:
After reading this and other posts I ended with this script.
I needed authentication on my local environment for my repositories.
So,the script is going to ask you the user, password and an option in case you want to remember your credentials on the repository.
For security reason I am erasing the user and password from the config file in git. (Not sure if those credentials are stored in another place)
#!/bin/bash
#
#This script is intended to be used with repositories that need authentication
#
#Funtion to erase the $username:$password@ from the config file from git in the repo
function erase_user_pass() {
printf "${green}lets clean $1 from $2 \n${normal}"
sed -i 's/'"$1"'/'""'/g' $2
}
#function to download the repo
#1-clone the repo
#2-fix the config file by deleting the user and password
#3-execute the git command to automatically store the credentials if the user answer yes on the startup
#
# param 1 = name of the repo
# param 2 = string to complet the url of the repo
#
# example repo in https://git.something.com/yourcompany/your.project.git
#
# the param1 should be your.project
# the param2 should be git.something.com/yourcompany/your.project.git (without 'https://')
#
# download_repo 'your.project' 'git.something.com/yourcompany/your.project.git'
#
function download_repo() {
path=$(pwd)
printf "${blue}\n Importing $1\n\n${normal}"
git clone https://$username:$password@$2
file="$(pwd)/$1/.git/config"
replace_string="$username:$password@"
erase_user_pass $replace_string $file
cd ./$1
if [ "$STORE_OK" == 'Yes' ]
then
printf "${green} store credentials for the repo $1 \n${normal}"
git config credential.helper store
fi
cd $path
}
blue=$(tput setaf 4)
green=$(tput setaf 2)
normal=$(tput sgr0)
echo -n Please Enter your GitHub Username:
read username
echo -n Please Enter your GitHub Password:
read -s password
printf "\n\nDo you want to store your credentials locally?:"
STORE_OK=
select STORE_OK in Yes No
do
case $STORE_OK in
'') echo 'Please enter 1 for Yes, or 2 for No.';;
?*) break
esac
done
printf "${blue}\n\n lets import your repos\n\n${normal}"
# repo 1 https://git.something.com/yourcompany/your.project.git
download_repo 'your.project' 'git.something.com/yourcompany/your.project.git'
# repo 2 https://git.something.com/yourcompany/your.project2.git
download_repo 'your.project2' 'git.something.com/yourcompany/your.project2.git'
printf "${blue}\n Enjoy :)"
exit 0
You should need to replace the calls to download_repo to point to your repositories, these are fake.
Regards and thanks to others answers.
回答6:
If all repositories are hosted under the same namespace (username), you can do the following:
$ echo name1 name2 name3 | xargs -n1 | xargs -I{} git clone https://github.com/username/{}
Explanation
First part will split the names separated by space into multiple lines
$ echo name1 name2 name3 | xargs -n1 name1 name2 name3
Then, each of these names is passed separately to the next
xargs
call, which invokesgit clone
and replaces{}
substring in URL with repository name, which basically translates to$ git clone https://github.com/username/name1 $ git clone https://github.com/username/name2 $ git clone https://github.com/username/name3
In case not all repos are hosted under the same namespace, you can move dynamic parts to echo
part, and keep common part of the URL in the last part.
来源:https://stackoverflow.com/questions/18900294/clone-multiple-git-repositories-in-one-local-directory