我的主分支和开发分支在GitHub上被远程跟踪。如何克隆这两个分支?
当前回答
要创建存储在git主机(github/bitbucket/etc)中的所有分支+引用+标记+等的“完整”备份,请运行:
mkdir -p -- myapp-mirror
cd myapp-mirror
git clone --mirror https://git.myco.com/group/myapp.git .git
git config --bool core.bare false
git config --bool core.logAllRefUpdates true
git reset --hard # restore working directory
这是根据我从其他答案中学到的一切整理而成的。
然后,您可以使用此本地回购镜像转换到不同的SCM系统/git主机,也可以将其作为备份。它作为搜索工具也很有用,因为大多数git主机只搜索每个repo的“main”分支上的代码,如果您使用git log-s“specialVar”,您将看到所有分支上的所有代码。
注意:如果您想在日常工作中使用此回购,请运行:
git config --unset remote.origin.mirror
警告:如果你试图在日常工作中使用它,你可能会遇到奇怪的问题。如果您的ide/编辑器正在进行一些自动提取,您的本地主控器可能会更新,因为您做了gitclone--mirror。然后这些文件会出现在git暂存区。事实上,我有一种情况,我在当地的一个特色分支。。该分支没有提交,repo中的所有文件都显示在暂存区域中。只是疯子。
其他回答
使用别名。虽然没有任何原生Git单行程序,但您可以将自己的定义为
git config --global alias.clone-branches '! git branch -a | sed -n "/\/HEAD /d; /\/master$/d; /remotes/p;" | xargs -L1 git checkout -t'
然后将其用作
git clone-branches
我也需要做同样的事情。这是我的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]
我在这里看到的所有答案都是有效的,但有一种更干净的方法可以克隆存储库并同时删除所有分支。
克隆存储库时,实际上会下载分支的所有信息,但分支是隐藏的。使用命令
git branch -a
您可以显示存储库的所有分支,并使用以下命令
git checkout -b branchname origin/branchname
然后,您可以一次手动下载一个。
然而,当您想要克隆具有大量分支的存储库时,前面的答案中所示的所有方法都是冗长和乏味的,而我将展示的是一种更加干净和快速的方法,尽管这有点复杂。您需要三个步骤来实现这一点:
1.第一步
在计算机上创建一个新的空文件夹,并从存储库克隆.git文件夹的镜像副本:
cd ~/Desktop && mkdir my_repo_folder && cd my_repo_folder
git clone --mirror https://github.com/planetoftheweb/responsivebootstrap.git .git
my_repo_folder文件夹中的本地存储库仍然是空的,现在只有一个隐藏的.git文件夹,您可以通过终端的“ls-alt”命令看到它。
2.第二步
通过将Git配置的布尔值“裸”切换为false,将此存储库从空(裸)存储库切换为常规存储库:
git config --bool core.bare false
3.第三步
获取当前文件夹中的所有内容,并在本地计算机上创建所有分支,从而使其成为正常的存储库。
git reset --hard
现在您只需键入命令“gitbranch”,即可看到所有分支都已下载。
这是一种快速的方法,您可以一次克隆一个包含所有分支的Git存储库,但这不是您希望对每个项目都这样做的。
看着问题的答案之一,我注意到可以缩短它:
for branch in `git branch -r | grep -v 'HEAD\|master'`; do
git branch --track ${branch##*/} $branch;
done
但是要注意,如果其中一个远程分支被命名,例如admin_master,它将不会被下载!
我正在用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添加?
- 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日志日期格式