Clone multiple git repositories in one local direc

2019-03-24 19:37发布

问题:

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 $*


回答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.