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


当前回答

执行远程存储库的裸克隆,将内容保存到.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

其他回答

我编写了这些小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克隆应该复制整个存储库。尝试克隆它,然后运行gitbranch-a。它应该列出所有分支。如果您想切换到分支“foo”而不是“master”,请使用git checkout foo。

使用我的工具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

您可以轻松切换到分支,而无需使用花哨的“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>

我认为这样做很有效:

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

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