如何在Git中克隆所有远程分支?
我有一个master和一个development分支,都被推送到GitHub上。我已经完成了d,pull和fetch的操作,但是除了master分支以外,我仍然无法获得任何东西。
我敢肯定我遗漏了一些明显的东西,但是我已经阅读了手册,一点都没有高兴。
解决方案
回答
git clone应该复制整个存储库。尝试克隆它,然后运行git branch -a
。它应该列出所有分支。如果那么你想切换到分支" foo"而不是" master",使用git checkout foo
。
回答
当我们执行" git clone git:// location"时,将提取所有分支和标记。
为了在特定的远程分支之上工作,假设它是源远程:
git checkout -b branch origin/branchname
回答
我们正在执行的访存应获取所有远程分支,但不会为它们创建本地分支。如果我们使用gitk,则应该看到远程分支描述为" remotes / origin / dev"或者类似内容。
要基于远程分支创建本地分支,请执行以下操作:
git checkout -b dev refs/remotes/origin/dev
哪个应该返回类似:
Branch dev set up to track remote branch refs/remotes/origin/dev. Switched to a new branch "dev"
现在,当我们在dev分支上时," git pull"将把本地dev更新到与远程dev分支相同的位置。请注意,它将获取所有分支,但仅将我们所在的分支拉到树的顶部。
回答
首先,克隆一个远程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
实际上,我们可以使用git remote
跟踪多个远程存储库。
$ 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 &
回答
使用我的工具git_remote_branch(我们需要在机器上安装Ruby)。它是专门为使远程分支操作变得简单而构建的。
每次它代表我们执行操作时,都会在控制台上将其打印为红色。随着时间的流逝,它们最终会刺入大脑:-)
如果我们不想让grb代表我们运行命令,则只需使用"说明"功能。这些命令将被打印到控制台上,而不是为我们执行。
最后,所有命令都具有别名,以便于记忆。
请注意,这是Alpha软件;-)
这是运行grb help时的帮助:
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 experimental origin/experimental
使用
$ git checkout -t origin/experimental
或者更冗长但更容易记住
$ git checkout --track origin/experimental
在跟踪远程存储库方面可能会更好。
回答
我需要做完全一样的事情。这是我的Ruby脚本。
#!/usr/bin/env ruby local = [] remote = {} # Prepare %x[git reset --hard HEAD] %x[git checkout master] # Makes sure that * is on master. %x[git branch -a].each_line do |line| line.strip! if /origin\//.match(line) remote[line.gsub(/origin\//, '')] = line else local << line end end # Update remote.each_pair do |loc, rem| next if local.include?(loc) %x[git checkout --track -b #{loc} #{rem}] end %x[git fetch]