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


当前回答

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

其他回答

为什么你只看到“大师”

gitclone下载所有远程分支,但仍将其视为“远程”,即使文件位于新存储库中。有一个例外,那就是克隆过程从名为“master”的远程分支创建了名为“master”的本地分支。默认情况下,gitbranch只显示本地分支,这就是为什么您只看到“master”。

gitbranch-a显示所有分支,包括远程分支。


如何获得本地分支机构

如果你真的想在一个分支上工作,你可能需要一个“本地”版本的分支。要从远程分支创建本地分支(不需要签出它们,从而更改工作目录的内容),你可以这样做:

git branch branchone origin/branchone
git branch branchtwo origin/branchtwo
git branch branchthree origin/branchthree

在本例中,branchone是基于origin/branchone创建的本地分支的名称;如果您希望创建具有不同名称的本地分支,可以执行以下操作:

git branch localbranchname origin/branchone

创建本地分支后,可以使用git分支查看它(记住,不需要-a来查看本地分支)。

下面是根据前面的答案改编的跨平台PowerShell 7函数。

function Invoke-GitCloneAll($url) {
    $repo = $url.Split('/')[-1].Replace('.git', '')
    $repo_d = Join-Path $pwd $repo
    if (Test-Path $repo_d) {
        Write-Error "fatal: destination path '$repo_d' already exists and is not an empty directory." -ErrorAction Continue
    } else {
        Write-Host "`nCloning all branches of $repo..."
        git -c fetch.prune=false clone $url -q --progress &&
        git -c fetch.prune=false --git-dir="$(Join-Path $repo_d '.git')" --work-tree="$repo_d" pull --all
        Write-Host "" #newline
    }
}

注意:-c fetch.sprune=false使其包含通常会被排除的过时分支。如果你对它不感兴趣,就去掉它。


通过从函数中删除&&,可以在PowerShell 5.1(Windows 10中的默认值)中实现这一点,但这使得它即使在上一个命令失败时也会尝试git pull。因此,我强烈建议您只使用跨平台PowerShell,它总是让您在尝试时感到困扰。

每当我需要推倒所有的树枝时,我都会这样做。来自Linkedin Learning的Ray Villalobos。尝试克隆所有分支,包括提交:

mkdir -p -- newproject_folder
cd newproject_folder
git clone --mirror https://github.com/USER_NAME/RepositoryName.git .git
git config --bool core.bare false
git reset --hard

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

下面是另一个简短的单行命令为所有远程分支创建本地分支:

(git branch -r | sed -n '/->/!s#^  origin/##p' && echo master) | xargs -L1 git checkout

如果已经创建了跟踪本地分支,它也可以正常工作。您可以在第一个git克隆之后或以后的任何时间调用它。

如果克隆后不需要签出主分支,请使用

git branch -r | sed -n '/->/!s#^  origin/##p'| xargs -L1 git checkout