我的主分支和开发分支在GitHub上被远程跟踪。如何克隆这两个分支?


当前回答

使用您可以记住的命令

我正在使用Bitbucket,一个Atlassian的存储库托管服务。所以我试着遵循他们的文档。这对我来说非常有用。使用以下简单而简短的命令,您可以签出远程分支。

首先克隆存储库,然后转到目标文件夹。最后,但并非最不重要的是,取件和结账:

git clone <repo> <destination_folder>
cd <destination_folder>
git fetch && git checkout <branch>

就是这样。这里有一个更真实的例子:

git clone https://username@bitbucket.org/team/repository.git project_folder
cd project_folder
git fetch && git checkout develop

您将在文档中找到有关命令的详细信息:克隆命令、获取命令、签出命令

其他回答

gitclone--原始repo上的镜像很好地实现了这一点。

git clone --mirror /path/to/original.git
git remote set-url origin /path/to/new-repo.git
git push -u origin

您可以轻松切换到分支,而无需使用花哨的“gitcheckout-b somebranch origin/somebranch”语法。您可以执行以下操作:

git checkout somebranch

Git将自动执行正确的操作:

$ git checkout somebranch
Branch somebranch set up to track remote branch somebranch from origin.
Switched to a new branch 'somebranch'

Git将检查在一个远程中是否存在同名的分支,如果存在,它将以与您明确指定的远程分支相同的方式对其进行跟踪。从git 1.8.2.1的git checkout手册页面:

如果未找到<branch>,但中确实存在跟踪分支只有一个具有匹配名称的远程(称为<remote>),视为相当于$git checkout-b<branch>--track<remote>/<branch>

您只需使用“gitclone”即可获得所有分支。

git clone <your_http_url>

即使您只看到主分支,也可以使用“gitbranch-a”查看所有分支。

git branch -a

你可以切换到你已经拥有的任何分支。

git checkout <your_branch_name>

不要担心,在“gitclone”之后,您不需要连接到远程存储库。当您没有Internet连接时,可以成功运行“git branch-a”和“git checkout<your_branch_name>”。因此,事实证明,当您执行“gitclone”时,它已经从远程存储库复制了所有分支。之后,您就不需要远程存储库了。您的本地已具有所有分支机构的代码。

我正在用Python和Pytest从Udemy课程Elegant Automation Frameworks中克隆一个存储库,以便以后可以离线查看。我尝试下载zip,但这只适用于当前分支,因此这是我的2美分。

我在Windows上工作,显然,我求助于Windows Subsystem for Linux中的Ubuntu shell。克隆完成后,我的分支如下:

$ git clone https://github.com/BrandonBlair/elegantframeworks.git

$ git branch -a

* master
  remotes/origin/HEAD -> origin/master
  remotes/origin/config_recipe
  remotes/origin/functionaltests
  remotes/origin/master
  remotes/origin/parallel
  remotes/origin/parametrize
  remotes/origin/parametrize_data_excel
  remotes/origin/unittesting
  remotes/origin/unittesting1

然后,在撞了几块几位数的结账砖墙之后,我终于明白了:

$ for b in `git branch -a | cut -c18- | cut -d\  -f1`; do git checkout $b; git stash; done

在此之后,以下是我的分支:

$ git branch -a

  config_recipe
  functionaltests
  master
  parallel
  parametrize
  parametrize_data_excel
  unittesting
* unittesting1
  remotes/origin/HEAD -> origin/master
  remotes/origin/config_recipe
  remotes/origin/functionaltests
  remotes/origin/master
  remotes/origin/parallel
  remotes/origin/parametrize
  remotes/origin/parametrize_data_excel
  remotes/origin/unittesting
  remotes/origin/unittesting1

我的是物理的,去掉了最初的遥控器/原点,然后过滤空间分隔符。可以说,我本可以把HEAD去掉,只剪一刀,但我会留给评论。

请注意,您当前的分支机构现在是列表中的最后一个。如果你不知道这是为什么,那你的处境就很艰难。现在你想要什么就结账吧。

除了用户没有人走在正确的轨道上之外,这些答案都无法解决问题。

我在将存储库从一个服务器/系统移动到另一个服务器或系统时遇到了问题。当我克隆存储库时,它只为master创建了一个本地分支,所以当我推送到新的远程时,只推送了master分支。

所以我发现这两种方法非常有用。

方法1:

git clone --mirror OLD_REPO_URL
cd new-cloned-project
mkdir .git
mv * .git
git config --local --bool core.bare false
git reset --hard HEAD
git remote add newrepo NEW_REPO_URL
git push --all newrepo
git push --tags newrepo

方法2:

git config --global alias.clone-branches '! git branch -a | sed -n "/\/HEAD /d; /\/master$/d; /remotes/p;" | xargs -L1 git checkout -t'
git clone OLD_REPO_URL
cd new-cloned-project
git clone-branches
git remote add newrepo NEW_REPO_URL
git push --all newrepo
git push --tags newrepo