Git Hub一次克隆所有分支

yptwkmov  于 2023-09-29  发布在  Git
关注(0)|答案(7)|浏览(147)

我正在尝试使用Linux将整个存储库克隆到我的机器上。我以前

git clone <url>

然后我进入下载文件的文件夹并输入

git branch

在终端。它只显示了master,而不是远程仓库中的其他分支。如何克隆所有分支?
我知道对于远程中的每个分支,我可以分别使用

git checkout -b <name of local branch> origin/<name of remote branch>

但除此之外还有别的办法吗

zaq34kh6

zaq34kh61#

(1)在git本地存储库中,创建一个新的sh文件

touch getAllBranches.sh
vi getAllBranches.sh

(2)将以下内容插入getAllBranches.sh文件:

for branch in `git branch -a | grep remotes | grep -v HEAD | grep -v master `; do
   git branch --track ${branch#remotes/origin/} $branch
done

(3)获取所有分支:

chmod +x getAllBranches.sh    
sh getAllBranches.sh

(4)检查本地存储库的结果:

git branch

例如,我使用repository:https://github.com/donhuvy/spring-boot
正如你所看到的,我已经把所有的分支都取到了本地机器上:

mpgws1up

mpgws1up2#

这不是太复杂,非常简单和直接的步骤如下:
克隆存储库后,运行$ cd myproject
git branch -a这将显示所有远程分支。

$ git branch -a
* master
  remotes/origin/HEAD
  remotes/origin/master
  remotes/origin/v1.0-stable
  remotes/origin/experimental

如果你想在远程分支上工作,你需要创建一个本地跟踪分支:

$ git checkout -b experimental origin/experimental

通过以下命令验证您是否在所需的分支中;

$ git branch

输出会像这样;

*experimental
master
some branch2
some branch3

请注意表示当前分支的 * 号。

wj8zmpe1

wj8zmpe13#

git clone --bare <repository url goes here> .git

然后,在克隆了存储库及其所有分支之后,执行以下操作

git config --bool core.bare false

git reset --hard
atmip9wb

atmip9wb4#

它只显示了master,而不是远程仓库中的其他分支。如何克隆所有分支?
分支本质上是指向提交的指针。当执行git clone(或git fetch)时,您将从远程存储库检索所有提交,以及它的所有分支。
但是,git branch默认情况下不显示远程分支。相反,它会显示您的 * 本地 * 分支,这些分支可能与远程分支有关系,也可能没有关系。如果你运行git branch --all,git会报告它知道的所有分支,包括本地和远程分支。
值得注意的是,标签并不是这样操作的,本地标签和远程标签之间没有区别。

nhjlsmyf

nhjlsmyf5#

我发现这是克隆git仓库和所有远程分支的简单解决方案:

# Clone remote repository and all branches
git clone --mirror https://github.com/test/frontend.git frontend/.git

# Change into frontend directory
cd frontend

# Update git config
git config --unset core.bare

# Checkout master branch
git checkout master
qv7cva1a

qv7cva1a6#

  1. git clone --bare https://repo.git projectName
  2. cd projectName
  3. git push --mirror https://repo.git
    让你的回购完全相同
    参见:https://help.github.com/en/articles/duplicating-a-repository
oxosxuxt

oxosxuxt7#

要下载完整的存储库(包括所有分支),请使用以下命令:git clone --mirror <URI>
这将创建一个名为repository.git的文件夹,除非您给予其他名称。
现在,这将获得原始存储库的完整克隆,但由于它处于bare=true模式,因此没有工作树。实际上,您拥有的是.git文件夹,包括所有分支和内容。这是一种奇特的方式,说明你不能直接访问这些文件,因为它们被隐藏在git系统中(压缩等)。
为了让它成为一个“正常”的git repo,我们需要在一个新文件夹中创建这个克隆的.git文件夹,这将是我们通常的repo文件夹:
mkdir <repo folder name> mv repository.git <repo folder name>/.git cd <repo folder name> git checkout master
请注意,没有一个原生的git命令可以下载所有的远程分支,所以最简单的方法是确保所有的提交都被推送到源,然后使用--mirror选项重新下载整个仓库。

相关问题