我的主分支和开发分支在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

其他回答

关于

git checkout-b实验来源/实验

使用

git checkout -t origin/experimental

或者更冗长,但更容易记住

git checkout --track origin/experimental

在跟踪远程存储库方面可能更好。

当您执行“gitclone”时git://location“,将获取所有分支和标记。

为了在特定远程分支上工作,假设它是源远程分支:

git checkout -b branch origin/branchname

独立存储库

如果要查找包含所有远程分支和提交日志的自包含克隆或备份,请使用:

git clone http://user@repo.url
git pull --all

gitbranch-a的接受答案仅显示远程分支。如果您尝试签出分支,除非您仍然具有对源服务器的网络访问权限,否则将无法签出。

信贷:加布·科普利(Gabe Kopley)建议使用git pull-all。

注:当然,如果您不再具有远程/源服务器的网络访问权限,那么远程/源分支将不会在存储库克隆中反映任何更新。它们的修订将反映您执行上述两个存储库克隆命令的日期和时间的提交。


Checkout a *local* branch in the usual way with `git checkout remote/origin/` Use `git branch -a` to reveal the remote branches saved within your `clone` repository.

要使用一个命令将所有克隆分支签出到本地分支,请使用以下bash命令之一:

$ for i in $(git branch -a |grep 'remotes' | awk -F/ '{print $3}' \ 
| grep -v 'HEAD ->');do git checkout -b $i --track origin/$i; done

OR

如果您的回购具有嵌套分支,则此命令还将考虑这一点:

for i in $(git branch -a |grep 'remotes' |grep -v 'HEAD ->');do \
basename ${i##\./} | xargs -I {} git checkout -b {} --track origin/{}; done

上述命令将将本地分支签入本地git存储库,其名称与remote/orging/<branchname>相同,并将其设置为--如果您再次重新获得对原始存储库服务器的网络访问并以通常方式执行git pull命令,则跟踪远程/orging服务器上远程分支的更改。

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

我在将存储库从一个服务器/系统移动到另一个服务器或系统时遇到了问题。当我克隆存储库时,它只为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

只需执行以下操作:

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

$ cd myproject

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

$ git pull
Already up-to-date.

$ git branch
* branchxyz
  master

$ git branch -a
* branchxyz
  master
  remotes/origin/HEAD -> origin/master
  remotes/origin/branchxyz
  remotes/origin/branch123

你看,gitclonegit://example.com/myprojectt获取所有内容,甚至是分支,您只需签出它们,就可以创建本地分支。