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


当前回答

我试图找出如何删除我在本地删除的远程分支。起源不是我的,我不想经历重新克隆一切的麻烦。

这对我有用:

假设您需要在本地重新创建分支:

git checkout -b recreated-branch-name
git branch -a (to list remote branches)
git rebase remotes/remote-origin/recreated-branch-name

因此,如果我从gituser/master分支到sjp,然后将其分支到sjp/mynewbranch,则如下所示:

$ git checkout -b mynewbranch

$ git branch -a
  master
  remotes/sjp/master
  remotes/sjp/mynewbranch

$ git fetch (habit to always do before)

$ git rebase remotes/sjp/mynewbranch

其他回答

我在这里看到的所有答案都是有效的,但有一种更干净的方法可以克隆存储库并同时删除所有分支。

克隆存储库时,实际上会下载分支的所有信息,但分支是隐藏的。使用命令

git branch -a

您可以显示存储库的所有分支,并使用以下命令

git checkout -b branchname origin/branchname

然后,您可以一次手动下载一个。


然而,当您想要克隆具有大量分支的存储库时,前面的答案中所示的所有方法都是冗长和乏味的,而我将展示的是一种更加干净和快速的方法,尽管这有点复杂。您需要三个步骤来实现这一点:

1.第一步

在计算机上创建一个新的空文件夹,并从存储库克隆.git文件夹的镜像副本:

cd ~/Desktop && mkdir my_repo_folder && cd my_repo_folder
git clone --mirror https://github.com/planetoftheweb/responsivebootstrap.git .git

my_repo_folder文件夹中的本地存储库仍然是空的,现在只有一个隐藏的.git文件夹,您可以通过终端的“ls-alt”命令看到它。

2.第二步

通过将Git配置的布尔值“裸”切换为false,将此存储库从空(裸)存储库切换为常规存储库:

git config --bool core.bare false

3.第三步

获取当前文件夹中的所有内容,并在本地计算机上创建所有分支,从而使其成为正常的存储库。

git reset --hard

现在您只需键入命令“gitbranch”,即可看到所有分支都已下载。

这是一种快速的方法,您可以一次克隆一个包含所有分支的Git存储库,但这不是您希望对每个项目都这样做的。

我编写了这些小PowerShell函数,以便能够签出我的所有Git分支,这些分支位于源远程。

Function git-GetAllRemoteBranches {
     iex "git branch -r"                       <# get all remote branches #> `
     | % { $_ -Match "origin\/(?'name'\S+)" }  <# select only names of the branches #> `
     | % { Out-Null; $matches['name'] }        <# write does names #>
}


Function git-CheckoutAllBranches {
    git-GetAllRemoteBranches `
        | % { iex "git checkout $_" }          <# execute ' git checkout <branch>' #>
}

更多的Git函数可以在我的Git设置库中找到。

执行远程存储库的裸克隆,将内容保存到.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分支,它们是隐藏的。。。

所以,如果您想查看所有分支的名称,只需添加--all标志,如下所示:

gitbranch—all或gitbranch-a

如果你只是到分行结账,你就能得到你所需要的一切。

但是,如果你克隆后,其他人创建了分支,该怎么办?

在这种情况下,只需执行以下操作:

git获取

并再次检查所有分支。。。

如果您想同时取件和结账,可以执行以下操作:

git获取和git签出您的branch_name

还为您创建了下图,以简化我所说的内容:

使用我的工具git_remote_branch(grb)。您需要在机器上安装Ruby)。它是专门为使远程分支操作非常容易而构建的。

每次它代表您执行操作时,都会在控制台上以红色打印。随着时间的推移,它们最终会进入你的大脑:-)

如果您不希望grb代表您运行命令,请使用“解释”功能。这些命令将打印到您的控制台,而不是为您执行。

最后,所有命令都有别名,以便于记忆。

注意,这是alpha软件;-)

以下是运行grb帮助时的帮助:

git_remote_branch version 0.2.6

  Usage:

  grb create branch_name [origin_server]

  grb publish branch_name [origin_server]

  grb rename branch_name [origin_server]

  grb delete branch_name [origin_server]

  grb track branch_name [origin_server]



  Notes:
  - If origin_server is not specified, the name 'origin' is assumed
    (git's default)
  - The rename functionality renames the current branch

  The explain meta-command: you can also prepend any command with the
keyword 'explain'. Instead of executing the command, git_remote_branch
will simply output the list of commands you need to run to accomplish
that goal.

  Example:
    grb explain create
    grb explain create my_branch github

  All commands also have aliases:
  create: create, new
  delete: delete, destroy, kill, remove, rm
  publish: publish, remotize
  rename: rename, rn, mv, move
  track: track, follow, grab, fetch