我的主分支和开发分支在GitHub上被远程跟踪。如何克隆这两个分支?
当前回答
这是一个使用awk的答案。如果在新的回购协议中使用,这种方法就足够了。
git branch -r | awk -F/ '{ system("git checkout " $NF) }'
现有的分支将被简单地签出,或者声明为已经在其中,但是可以添加过滤器以避免冲突。
还可以修改它,以便调用显式git checkout-b<branch>-t<remote>/<branch>命令。
这个答案符合尼科斯·C的想法。
或者,我们可以指定远程分支。这是基于murphytalk的答案。
git branch -r | awk '{ system("git checkout -t " $NF) }'
它会对冲突发出致命的错误消息,但我认为它们是无害的。
这两个命令都可以别名。
使用nobody的答案作为参考,我们可以使用以下命令来创建别名:
git config --global alias.clone-branches '! git branch -r | awk -F/ "{ system(\"git checkout \" \$NF) }"'
git config --global alias.clone-branches '! git branch -r | awk "{ system(\"git checkout -t \" \$NF) }"'
就我个人而言,我会使用跟踪所有分支或跟踪所有分支。
其他回答
我正在用Python和Pytest从Udemy课程Elegant Automation Frameworks中克隆一个存储库,以便以后可以离线查看。我尝试下载zip,但这只适用于当前分支,因此这是我的2美分。
我在Windows上工作,显然,我求助于Windows Subsystem for Linux中的Ubuntu shell。克隆完成后,我的分支如下:
$ git clone https://github.com/BrandonBlair/elegantframeworks.git
$ git branch -a
* master
remotes/origin/HEAD -> origin/master
remotes/origin/config_recipe
remotes/origin/functionaltests
remotes/origin/master
remotes/origin/parallel
remotes/origin/parametrize
remotes/origin/parametrize_data_excel
remotes/origin/unittesting
remotes/origin/unittesting1
然后,在撞了几块几位数的结账砖墙之后,我终于明白了:
$ for b in `git branch -a | cut -c18- | cut -d\ -f1`; do git checkout $b; git stash; done
在此之后,以下是我的分支:
$ git branch -a
config_recipe
functionaltests
master
parallel
parametrize
parametrize_data_excel
unittesting
* unittesting1
remotes/origin/HEAD -> origin/master
remotes/origin/config_recipe
remotes/origin/functionaltests
remotes/origin/master
remotes/origin/parallel
remotes/origin/parametrize
remotes/origin/parametrize_data_excel
remotes/origin/unittesting
remotes/origin/unittesting1
我的是物理的,去掉了最初的遥控器/原点,然后过滤空间分隔符。可以说,我本可以把HEAD去掉,只剪一刀,但我会留给评论。
请注意,您当前的分支机构现在是列表中的最后一个。如果你不知道这是为什么,那你的处境就很艰难。现在你想要什么就结账吧。
关于
git checkout-b实验来源/实验
使用
git checkout -t origin/experimental
或者更冗长,但更容易记住
git checkout --track origin/experimental
在跟踪远程存储库方面可能更好。
使用我的工具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
这是一个使用awk的答案。如果在新的回购协议中使用,这种方法就足够了。
git branch -r | awk -F/ '{ system("git checkout " $NF) }'
现有的分支将被简单地签出,或者声明为已经在其中,但是可以添加过滤器以避免冲突。
还可以修改它,以便调用显式git checkout-b<branch>-t<remote>/<branch>命令。
这个答案符合尼科斯·C的想法。
或者,我们可以指定远程分支。这是基于murphytalk的答案。
git branch -r | awk '{ system("git checkout -t " $NF) }'
它会对冲突发出致命的错误消息,但我认为它们是无害的。
这两个命令都可以别名。
使用nobody的答案作为参考,我们可以使用以下命令来创建别名:
git config --global alias.clone-branches '! git branch -r | awk -F/ "{ system(\"git checkout \" \$NF) }"'
git config --global alias.clone-branches '! git branch -r | awk "{ system(\"git checkout -t \" \$NF) }"'
就我个人而言,我会使用跟踪所有分支或跟踪所有分支。
Git通常(如果未指定)从一个或多个其他存储库中获取所有分支和/或标记(refs,请参见:Git-ls-refs)以及完成其历史所需的对象。换句话说,它获取已经下载的对象可以访问的对象。看:git fetch真正做什么?
有时,您可能有一些分支/标记没有直接连接到当前的分支/标记,因此git pull-all/git fetch-all在这种情况下不会有帮助,但您可以通过以下方式列出它们:
git ls-remote -h -t origin
并通过知道引用名称手动获取它们。
因此,要获取所有信息,请尝试:
git fetch origin --depth=10000 $(git ls-remote -h -t origin)
如果您对存储库进行了浅处理,--depth=10000参数可能会有所帮助。
然后再次检查所有分支:
git branch -avv
如果上述方法无效,则需要手动将丢失的分支添加到跟踪列表中(因为它们不知何故丢失了):
$ git remote -v show origin
...
Remote branches:
master tracked
通过git远程设置分支,如:
git remote set-branches --add origin missing_branch
因此在获取后,它可能出现在remotes/origin下:
$ git remote -v show origin
...
Remote branches:
missing_branch new (next fetch will store in remotes/origin)
$ git fetch
From github.com:Foo/Bar
* [new branch] missing_branch -> origin/missing_branch
故障排除
如果您仍然无法获得主分支以外的任何内容,请检查以下内容:
仔细检查遥控器(gitremote-v),例如。验证git-config-branch.master.remote是源代码。通过:git remote show origin检查origin是否指向正确的URL(参见本文)。
推荐文章
- 为什么我需要显式地推一个新分支?
- 如何撤消最后的git添加?
- Rubymine:如何让Git忽略Rubymine创建的.idea文件
- Gitignore二进制文件,没有扩展名
- Git隐藏错误:Git隐藏弹出并最终与合并冲突
- 了解Git和GitHub的基础知识
- 没有。Git目录的Git克隆
- 在GitHub上有一个公共回购的私人分支?
- Git与Mercurial仓库的互操作性
- 忽略git中修改(但未提交)的文件?
- “git restore”命令是什么?“git restore”和“git reset”之间有什么区别?
- Git合并与强制覆盖
- Git拉另一个分支
- 在Bash命令提示符上添加git分支
- 如何更改Git日志日期格式