在一个本地目录中克隆多个git存储库?

vir*_*2al 16 git repository git-clone

是否可以git clone使用一个命令来多个git存储库(例如:git clone "1.git,2.git,3.git.."在一个本地目录中?

Mic*_*nov 11

如果所有存储库都托管在同一命名空间(用户名)下,您可以执行以下操作:

$ echo name1 name2 name3 | xargs -n1 | xargs -I{} git clone https://github.com/username/{}
Run Code Online (Sandbox Code Playgroud)

解释

  1. 第一部分将由空格分隔的名称分成多行

    $ echo name1 name2 name3 | xargs -n1
    name1
    name2
    name3
    
    Run Code Online (Sandbox Code Playgroud)
  2. 然后,这些名称中的每一个都单独传递给下一个xargs调用,该调用git clone将调用{}URL 中的子字符串并将其替换为存储库名称,这基本上转换为

    $ git clone https://github.com/username/name1
    $ git clone https://github.com/username/name2
    $ git clone https://github.com/username/name3
    
    Run Code Online (Sandbox Code Playgroud)

如果并非所有存储库都托管在同一命名空间下,您可以将动态部分移动到echo部分,并将 URL 的公共部分保留在最后一部分。


Von*_*onC 9

你可以找到类似的脚本示例这一个:

我有一个名为"clone"的文件,其中包含几个git repos的URL(取自djangosites.com.很棒的网站.必须访问)

片段:

$ 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
Run Code Online (Sandbox Code Playgroud)

显然,一次克隆多个回购更难(git clone <repo1> <repo2> ... <repon>不起作用).所以我编写了这个简短的bash代码来使其工作:

码:

atm in /home/atm/git/django_repos
$ for f in `cat clone`; do `git clone $f`; done 
Run Code Online (Sandbox Code Playgroud)

你可以在gist.github.com上找到更多,比如这个,从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 $*
Run Code Online (Sandbox Code Playgroud)


Fáb*_*eia 8

这就是我自己几乎解决的问题:

git clone https://myrepo.com/folder.git && \
git clone https://myrepo.com/folder2.git && \
git clone https://myrepo.com/folder3.git
Run Code Online (Sandbox Code Playgroud)

使用Sublime或VSCode等代码编辑器更容易构建.

对我来说唯一的缺点是:如果你没有存储你的凭据,你将不得不一遍又一遍地输入它.


Ice*_*erg 7

将存储库一一克隆到同一目录中:

xargs -L1 git clone <<EOF
https://github.com/motdotla/dotenv.git
https://github.com/node-fetch/node-fetch.git
EOF
Run Code Online (Sandbox Code Playgroud)