我的主分支和开发分支在GitHub上被远程跟踪。如何克隆这两个分支?
当前回答
我在这里看到的所有答案都是有效的,但有一种更干净的方法可以克隆存储库并同时删除所有分支。
克隆存储库时,实际上会下载分支的所有信息,但分支是隐藏的。使用命令
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存储库,但这不是您希望对每个项目都这样做的。
其他回答
在这里,我为您编写了一个很好的函数,使其易于重复
gitCloneAllBranches() { # clone all git branches at once easily and cd in
# clone as "bare repo"
git clone --mirror $1
# rename without .git extension
with_extension=$(basename $1)
without_extension=$(echo $with_extension | sed 's/.git//')
mv $with_extension $without_extension
cd $without_extension
# change from "bare repository" to not
git config --bool core.bare false
# check if still bare repository if so
if [[ $(git rev-parse --is-bare-repository) == false ]]; then
echo "ready to go"
else
echo "WARNING: STILL BARE GIT REPOSITORY"
fi
# EXAMPLES:
# gitCloneAllBranches https://github.com/something/something.git
}
只需执行以下操作:
$ 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获取所有内容,甚至是分支,您只需签出它们,就可以创建本地分支。
当您执行“gitclone”时git://location“,将获取所有分支和标记。
为了在特定远程分支上工作,假设它是源远程分支:
git checkout -b branch origin/branchname
从本地回购中克隆不能与gitclone和gitfetch一起使用:许多分支/标签将保持不匹配。
获取包含所有分支和标记的克隆。
git clone --mirror git://example.com/myproject myproject-local-bare-repo.git
要获取包含所有分支和标记但也包含工作副本的克隆,请执行以下操作:
git clone --mirror git://example.com/myproject myproject/.git
cd myproject
git config --unset core.bare
git config receive.denyCurrentBranch updateInstead
git checkout master
如果使用Bitbucket,则可以使用import Repository。这将导入所有Git历史记录(所有分支和提交)。
推荐文章
- 为什么我需要显式地推一个新分支?
- 如何撤消最后的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日志日期格式