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


当前回答

我认为这样做很有效:

mkdir YourRepo
cd YourRepo
git init --bare .git                       # create a bare repo
git remote add origin REMOTE_URL           # add a remote
git fetch origin refs/heads/*:refs/heads/* # fetch heads
git fetch origin refs/tags/*:refs/tags/*   # fetch tags
git init                                   # reinit work tree
git checkout master                        # checkout a branch

到目前为止,这对我有效。

其他回答

执行远程存储库的裸克隆,将内容保存到.git目录

git clone --bare remote-repo-url.git localdirname/.git

(使用gitclone--bare或gitinit--bare创建的裸git存储库是一个存储存储库,它没有工作目录,您无法在其中创建或修改文件。)

将目录更改为本地目录

cd localdirname

使您的git存储库可修改

git config --bool core.bare false

还原工作目录

git reset --hard

列出所有分支机构

git branch -al

所有分支机构

从Git项目下载所有分支的脚本

安装:

sudo git clone https://github.com/marceloviana/allBranches.git && sudo cp -rfv allBranches/allBranches.sh /usr/bin/allBranches && sudo chmod +x /usr/bin/allBranches && sudo rm -rf allBranches

准备好的现在只需调用命令(allBranches)并告诉Git项目目录,您希望下载所有分支

Use

示例1:

~$ allBranches /var/www/myproject1/

示例2:

~$ allBranches /var/www/myproject2/

示例3(如果已经在项目目录中):

~$ allBranches ./

or

~$ allBranches .

查看结果:

git branch

参考:

存储库allBranches GitHub:https://github.com/marceloviana/allBranches

首先,克隆一个远程Git存储库并将其cd到其中:

$ git clone git://example.com/myproject
$ cd myproject

接下来,查看存储库中的本地分支:

$ git branch
* master

但您的存储库中隐藏着其他分支!使用-a标志查看这些:

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

要快速查看上游分支,请直接查看:

$ git checkout origin/experimental

要处理该分支,请创建一个本地跟踪分支,该分支通过以下方式自动完成:

$ git checkout experimental

Branch experimental set up to track remote branch experimental from origin.
Switched to a new branch 'experimental'

这里,“新分支”只是指从索引中获取分支并在本地为您创建。正如前一行告诉的那样,正在设置分支以跟踪远程分支,这通常意味着origin/branch_name分支。

您的本地分支机构现在应该显示:

$ git branch
* experimental
  master

您可以使用gitremote跟踪多个远程存储库:

$ git remote add win32 git://example.com/users/joe/myproject-win32-port
$ git branch -a
* master
  remotes/origin/HEAD
  remotes/origin/master
  remotes/origin/v1.0-stable
  remotes/origin/experimental
  remotes/win32/master
  remotes/win32/new-widgets

在这一点上,事情变得非常疯狂,所以运行gitk看看发生了什么:

$ gitk --all &

使用--mirror选项似乎可以正确复制远程跟踪分支。然而,它将存储库设置为一个裸存储库,因此您必须在之后将其恢复为一个正常的存储库。

git clone --mirror path/to/original path/to/dest/.git
cd path/to/dest
git config --bool core.bare false
git checkout anybranch

参考:Git常见问题解答:如何克隆包含所有远程跟踪分支的存储库?

对于使用Visual Studio代码的开发人员来说,一个更好的替代解决方案是使用Git Shadow Extension。

此Visual Studio代码扩展允许克隆存储库内容和目录,这些内容和目录可以通过分支名称或提交哈希进行筛选。这样,分支或提交可以用作新项目的样板/模板。