我的主分支和开发分支在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) }"'
就我个人而言,我会使用跟踪所有分支或跟踪所有分支。
其他回答
首先,克隆一个远程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
您可以使用gitremote跟踪多个远程存储库:
$ 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主机(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中的所有文件都显示在暂存区域中。只是疯子。
我编写了这些小PowerShell函数,以便能够签出我的所有Git分支,这些分支位于源远程。
Function git-GetAllRemoteBranches {
iex "git branch -r" <# get all remote branches #> `
| % { $_ -Match "origin\/(?'name'\S+)" } <# select only names of the branches #> `
| % { Out-Null; $matches['name'] } <# write does names #>
}
Function git-CheckoutAllBranches {
git-GetAllRemoteBranches `
| % { iex "git checkout $_" } <# execute ' git checkout <branch>' #>
}
更多的Git函数可以在我的Git设置库中找到。
我也需要做同样的事情。这是我的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 checkout -b recreated-branch-name
git branch -a (to list remote branches)
git rebase remotes/remote-origin/recreated-branch-name
因此,如果我从gituser/master分支到sjp,然后将其分支到sjp/mynewbranch,则如下所示:
$ git checkout -b mynewbranch
$ git branch -a
master
remotes/sjp/master
remotes/sjp/mynewbranch
$ git fetch (habit to always do before)
$ git rebase remotes/sjp/mynewbranch
推荐文章
- 撤消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迁移文件吗?
- 错误:您对以下文件的本地更改将被签出覆盖