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


是否有可能 git clone 多个git存储库,带有一个命令(例如: git clone "1.git,2.git,3.git.." 在一个本地目录?


7175
2017-09-19 16:33


起源

你到底是什么意思?你的意思是“合并”所有的回购单在一个? - Atropo
不。例如:我有:x.git,y.git,z.git ...要结账,我必须这样做:git clone x.git,git clone y.git,git clone z.git,..我想用1命令git克隆多个git repos。可能吗? - vir2al
好吧,你可以在一个命令中完成它 你输入的,但仍会有多个实际执行的命令。就像是 echo x.git y.git z.git | xargs -n 1 -d ' ' git clone, 例如。 - twalberg
写一个脚本。 - Johnny Z
如果你使用mu-repo,它支持这样一个克隆,只要你提交一个带有依赖信息的.mu_repo(参见: fabioz.github.io/mu-repo/cloning) - Fabio Zadrozny


答案:


您可以找到类似的脚本示例 这个

我有一个名为“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

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

码:

atm in /home/atm/git/django_repos
$ for f in `cat clone`; do `git clone $f`; done 

你会发现更多 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 $*

7
2017-09-20 05:56





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

git clone https://myrepo.com/folder.git && \
git clone https://myrepo.com/folder2.git && \
git clone https://myrepo.com/folder3.git

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

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


5
2018-05-14 17:10



这依次克隆了repos,但它仍然有用。谢谢! - Luboš Turek
没问题!我很高兴我加油了:) - Fábio Correia


我为我的项目创建了一个示例脚本。我们的项目包括许多存储库,当一个新人加入团队时需要克隆这些存储库。

所以这里是脚本的内容,您可以将其保存到一些可执行文件中并执行。

   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

它安静有用,可以避免无聊的手动操作,并确保一次性克隆所有必需的项目。

希望我的回答能帮助别人。


1
2017-11-03 12:52



根本不实用 - holms
是的..我想出来了......脚本存在问题..如果你的密码包含@,它在命令本身作为密码后的@不起作用。将不得不努力。但它起作用。 - Sanjay Bharwani


您可以使用各种解决方案。

使用git的credential.helper设置缓存超时(请参阅 这个),然后你可以使用像建议的那样的脚本/列表 法比奥。这样,您只需输入一次凭证(通常,除非克隆需要的时间超过缓存超时。

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

这仍然是顺序的,但它有帮助,而且非常简单,恕我直言。


0
2017-07-24 02:09