我的主分支和开发分支在GitHub上被远程跟踪。如何克隆这两个分支?
当前回答
在这里,我为您编写了一个很好的函数,使其易于重复
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
}
其他回答
您只需使用“gitclone”即可获得所有分支。
git clone <your_http_url>
即使您只看到主分支,也可以使用“gitbranch-a”查看所有分支。
git branch -a
你可以切换到你已经拥有的任何分支。
git checkout <your_branch_name>
不要担心,在“gitclone”之后,您不需要连接到远程存储库。当您没有Internet连接时,可以成功运行“git branch-a”和“git checkout<your_branch_name>”。因此,事实证明,当您执行“gitclone”时,它已经从远程存储库复制了所有分支。之后,您就不需要远程存储库了。您的本地已具有所有分支机构的代码。
如果您有许多远程分支要同时获取,请执行以下操作:
git pull --all
现在,您可以根据需要签出任何分支,而无需访问远程存储库。
注意:这不会创建任何未签出分支的工作副本,这就是问题所在。对此,请参见
大鱼的回答戴夫的回答
我在这里看到的所有答案都是有效的,但有一种更干净的方法可以克隆存储库并同时删除所有分支。
克隆存储库时,实际上会下载分支的所有信息,但分支是隐藏的。使用命令
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存储库,但这不是您希望对每个项目都这样做的。
使用我的工具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
如何为远程源匹配模式上的每个分支创建本地分支。
#!/bin/sh
git fetch --all
git for-each-ref --format='%(refname:short)' refs/remotes/origin/pattern |\
sed 's@\(origin/\)\(.*\)@\2\t\1\2@' |\
xargs -n 2 git branch --track
获取所有远程引用(分支/标记),然后创建本地引用。应该可以在大多数系统上快速运行,而无需查看索引或依靠抨击。
推荐文章
- 撤消git平分错误
- 使用.gitconfig配置diff工具
- 如何配置Mac OS X术语,使git有颜色?
- Visual Studio Code: .git文件夹/文件隐藏
- “node_modules”文件夹应该包含在git存储库中吗
- 为什么git-rebase给了我合并冲突,而我所做的只是压缩提交?
- 当我试图推到原点时,为什么Git告诉我“没有这样的远程‘原点’”?
- 如何从远程分支中挑选?
- 如何查看一个分支中的哪些提交不在另一个分支中?
- 如何取消在github上的拉请求?
- HEAD和master的区别
- GIT克隆在windows中跨本地文件系统回购
- RPC失败;卷度传输已关闭,剩余未完成的读取数据
- 我应该在.gitignore文件中添加Django迁移文件吗?
- 错误:您对以下文件的本地更改将被签出覆盖