Git Hub 一次克隆所有分支

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/40310932/
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-09 04:36:15  来源:igfitidea点击:

Git Hub Clone All Branches At Once

gitgithub

提问by Anant Joshi

I'm trying to clone an entire repository onto my machine using linux. I used

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

git clone <url>   

I then went into the folder where it was downloaded and typed

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

git branch 

in the terminal. It's only showing me master and not other branches which were in the remote repository. How do I clone all branches?

在终端。它只向我显示 master 而不是远程存储库中的其他分支。如何克隆所有分支?

I know that for each branch in the remote I can separately use

我知道对于远程中的每个分支我可以单独使用

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

but is there any way other than that?

但除此之外还有什么办法吗?

回答by Do Nhu Vy

(1) Inside git local repostitory, create a new sh file

(1) 在git local repostitory里面,新建一个sh文件

touch getAllBranches.sh
vi getAllBranches.sh

(2) Insert the below content to getAllBranches.shfile:

(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) Get all branches:

(3) 获取所有分支:

chmod +x getAllBranches.sh    
sh getAllBranches.sh

(4) Check result at local repository:

(4) 在本地仓库查看结果:

git branch

For example, I use repository: https://github.com/donhuvy/spring-boot

例如,我使用存储库:https: //github.com/donhuvy/spring-boot

As you can see, I have fetched all branches to local machine:

如您所见,我已将所有分支提取到本地机器:

enter image description here

在此处输入图片说明

回答by Amit Gupta

This isn't too much complicated, very simple and straight forward steps are as follows:

这不是太复杂,非常简单直接的步骤如下:

After cloning the repo, run $ cd myproject

克隆 repo 后,运行 $ cd myproject

git branch -aThis will show you all the remote branches.

git branch -a这将显示所有远程分支。

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

If you want to work on remote branch, you'll need to create a local tracking branch:

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

$ git checkout -b experimental origin/experimental

Verify whether you are in the desired branch by the following command;

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

$ git branch

The output will like this;

输出会是这样的;

*experimental
master
some branch2
some branch3 

Notice the * sign that denotes the current branch.

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

回答by Amen Ra

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

Then after the repo is clone with all its branches then do the following

然后在 repo 及其所有分支被克隆后,然后执行以下操作

git config --bool core.bare false

git reset --hard

回答by Xiong Chiamiov

It's only showing me master and not other branches which were in the remote repository. How do I clone all branches?

它只向我显示 master 而不是远程存储库中的其他分支。如何克隆所有分支?

Branches are essentially pointers to commits. When you do a git clone(or a git fetch), you retrieve all of the commits from the remote repository, and all of its branches as well.

分支本质上是指向提交的指针。当您执行 a git clone(或 a git fetch) 时,您会从远程存储库及其所有分支中检索所有提交。

However, git branchdoes not show remote branches by default. Instead, it shows you your localbranches, which may or may not have any relation to branches that exist on the remote. If you run git branch --all, git will report all of the branches it knows about, both local and remote.

但是,git branch默认情况下不显示远程分支。相反,它会向您显示您的本地分支,这些分支可能与远程存在的分支有任何关系,也可能没有。如果您运行git branch --all,git 将报告它知道的所有分支,包括本地和远程。

It's worth noting that tags do notoperate this way, and there is no distinction between a local and remote tag.

值得注意的是,标签不是这样操作的,本地标签和远程标签没有区别。

回答by Stryker

I find this to be the simple solution to clone a git repository and all remote branches:

我发现这是克隆 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

回答by shailesh sahu

  1. git clone --bare https://repo.git projectName
  2. cd projectName
  3. git push --mirror https://repo.git
  1. git clone --bare https://repo.git projectName
  2. cd projectName
  3. git push --mirror https://repo.git

that makes your repo completely identical.

这使您的回购完全相同。

See: https://help.github.com/en/articles/duplicating-a-repository

请参阅:https: //help.github.com/en/articles/duplicating-a-repository

回答by togume

To download a full repository, including all branches, use the following command: git clone --mirror <URI>

要下载完整的存储库,包括所有分支,请使用以下命令: git clone --mirror <URI>

This will create a folder called repository.gitunless you give it a different name.

这将创建一个名为的文件夹,repository.git除非您给它一个不同的名称。

Now, this gets you a full clone of the original repository, but because it's in bare=truemode, you don't have a work tree. Effectively, what you have is the .gitfolder, including all branches and content. This is a fancy way of saying that you won't have direct access to the files because they're stashed away within the git system (compressed, etc).

现在,这将为您提供原始存储库的完整克隆,但由于它处于bare=true模式,您没有工作树。实际上,您拥有的是.git文件夹,包括所有分支和内容。这是一种奇特的说法,您将无法直接访问文件,因为它们被隐藏在 git 系统中(压缩等)。

To make this a "normal" git repo, we need to make this clone the .gitfolder within a new folder, which will be our usual repo folder:

为了使它成为一个“正常”的 git repo,我们需要将这个.git文件夹克隆到一个新文件夹中,这将是我们通常的 repo 文件夹:

mkdir <repo folder name> mv repository.git <repo folder name>/.git cd <repo folder name> git checkout master

mkdir <repo folder name> mv repository.git <repo folder name>/.git cd <repo folder name> git checkout master

Note that there is no single native git command to download all remote branches, so the simplest way is to make sure you have all commits pushed to the origin, and then re-download the whole repository anew using this --mirror option.

请注意,没有单个本地 git 命令可以下载所有远程分支,因此最简单的方法是确保将所有提交推送到源,然后使用 --mirror 选项重新下载整个存储库。