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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18900294/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 17:00:54  来源:igfitidea点击:

Clone multiple git repositories in one local directory?

gitrepositorygit-clone

提问by vir2al

Is it possible to git clonemultiple git repositories with one command (for example: git clone "1.git,2.git,3.git.."in one local directory?

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

回答by VonC

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:

我有一个名为“clone”的文件,其中包含几个 git 存储库的 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

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:

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

代码:

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:

你会在gist.github.com上找到更多,比如这个,从 GitHub 克隆你的所有存储库:

#!/bin/bash
#
# Copyright 2011, Tim Branyen @tbranyen <[email protected]>
# 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
[email protected]:
GITHUB_USERNAME=$(git config --global github.user)

function main {
  # Improperly configured user
  detect_user

  # Missing arguments
  args=
  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 lilgeekmentioned bl4ckbo7/agt, a python script which includes cloning with fastest and parallel clone processing feature.

更一般地说,需要一种脚本方法,并lilgeek提到了bl4ckbo7/agt,这是一个 Python 脚本,包括具有最快和并行克隆处理功能的克隆。

https://raw.githubusercontent.com/bl4ckbo7/agt/master/screenshots/agt_clone1.png

https://raw.githubusercontent.com/bl4ckbo7/agt/master/screenshots/agt_clone1.png

回答by Fábio Correia

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.

使用 Sublime 或 VSCode 等代码编辑器可以更轻松地构建。

The only downside for me: If you did not store your credentials, you're gonna have to type it over and over.

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

回答by Sanjay Bharwani

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:[email protected]/location/reponame1.git
https://$username:[email protected]/location/reponame2.git
....
https://$username:[email protected]/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.

希望我的回答对其他人也有帮助。

回答by Michael Radionov

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

解释

  1. First part will split the names separated by space into multiple lines

    $ echo name1 name2 name3 | xargs -n1
    name1
    name2
    name3
    
  2. Then, each of these names is passed separately to the next xargscall, which invokes git cloneand 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
    
  1. 第一部分将由空格分隔的名称分成多行

    $ echo name1 name2 name3 | xargs -n1
    name1
    name2
    name3
    
  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
    

In case not all repos are hosted under the same namespace, you can move dynamic parts to echopart, and keep common part of the URL in the last part.

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

回答by Priit Paris

If you use windows, you can use powershell.

如果您使用 Windows,则可以使用 powershell。

Just make list in favourite text editor like this:

只需在最喜欢的文本编辑器中制作列表,如下所示:

git clone https://github.com/1.git;
git clone https://github.com/2.git;
git clone https://github.com/3.git;
git clone https://github.com/4.git;
git clone https://github.com/5.git;

In powershell console go do your directory.

在 powershell 控制台中执行您的目录。

Example

例子

cd d:/myGitRepos

Then copy repo list directly do powershell console. First time it will ask username and password, but after that it will remember it.

然后直接复制repo list做powershell控制台。第一次它会询问用户名和密码,但之后它会记住它。

回答by Renato Back

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 的 credential.helper 设置缓存超时(请参阅),然后您可以使用Fábio建议的脚本/列表。这样,您只需键入一次凭据(通常,除非克隆所需的时间超过缓存超时。

git config --global credential.helper 'cache timeout 3600'
git clone https://[email protected]/myproject/myrepo.git
### password should be prompted 
git clone https://[email protected]/myproject/mysecondrepo.git
### look ma, no password prompt!
git clone https://[email protected]/myproject/mythirdrepo.git
git clone https://[email protected]/myproject/myfourthrepo.git
git clone https://[email protected]/myproject/andsoforthrepo.git

That's still sequential, but it helps and is pretty simple, IMHO.

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

回答by Juano7894

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)

出于安全原因,我正在从 git 的配置文件中删除用户和密码。(不确定这些凭据是否存储在其他地方)

#!/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  from  \n${normal}"
  sed -i 's/'""'/'""'/g' 
}

#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 \n\n${normal}"
  git clone https://$username:$password@
  file="$(pwd)//.git/config"
  replace_string="$username:$password@"
  erase_user_pass $replace_string $file
  cd ./
  if [ "$STORE_OK" == 'Yes' ]
    then
     printf "${green} store credentials for the repo   \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.

您应该需要替换对 download_repo 的调用以指向您的存储库,这些都是假的。

Regards and thanks to others answers.

问候并感谢其他人的回答。