我的主分支和开发分支在GitHub上被远程跟踪。如何克隆这两个分支?
当前回答
下面是根据前面的答案改编的跨平台PowerShell 7函数。
function Invoke-GitCloneAll($url) {
$repo = $url.Split('/')[-1].Replace('.git', '')
$repo_d = Join-Path $pwd $repo
if (Test-Path $repo_d) {
Write-Error "fatal: destination path '$repo_d' already exists and is not an empty directory." -ErrorAction Continue
} else {
Write-Host "`nCloning all branches of $repo..."
git -c fetch.prune=false clone $url -q --progress &&
git -c fetch.prune=false --git-dir="$(Join-Path $repo_d '.git')" --work-tree="$repo_d" pull --all
Write-Host "" #newline
}
}
注意:-c fetch.sprune=false使其包含通常会被排除的过时分支。如果你对它不感兴趣,就去掉它。
通过从函数中删除&&,可以在PowerShell 5.1(Windows 10中的默认值)中实现这一点,但这使得它即使在上一个命令失败时也会尝试git pull。因此,我强烈建议您只使用跨平台PowerShell,它总是让您在尝试时感到困扰。
其他回答
我试图找出如何删除我在本地删除的远程分支。起源不是我的,我不想经历重新克隆一切的麻烦。
这对我有用:
假设您需要在本地重新创建分支:
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
如何为远程源匹配模式上的每个分支创建本地分支。
#!/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
获取所有远程引用(分支/标记),然后创建本地引用。应该可以在大多数系统上快速运行,而无需查看索引或依靠抨击。
您正在执行的提取应该获取所有远程分支,但不会为它们创建本地分支。如果您使用gitk,您应该看到远程分支被描述为“remotes/origin/dev”或类似的名称。
要基于远程分支创建本地分支,请执行以下操作:
git checkout -b dev refs/remotes/origin/dev
应该返回如下内容:
Branch dev set up to track remote branch refs/remotes/origin/dev. Switched to a new branch "dev"
现在,当您在dev分支上时,“git pull”会将本地dev更新到与远程dev分支相同的位置。注意,它会取出所有的树枝,但只会将你所在的树枝拉到树的顶端。
从本地回购中克隆不能与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
我无法编辑Bigfish的答案。他提出了一个bash脚本,我提供它来更新并提供更好的git集成。grep已经过时,应该用grep-E替换。
#!/bin/bash
for branch in $(git branch --all | grep '^\s*remotes' | grep -E --invert-match '(:?HEAD|master)$'); do
git branch --track "${branch##*/}" "$branch"
done
您可以通过将此bash文件添加为git自定义子命令来扩展git:
$ mkdir ~/.gitbin; touch ~/.gitbin/git-fetchThemAll
$ chmod u+x ~/.gitbin/git-fetchThemAll
将bash脚本的内容放在gitfetchThemAll中。
$ echo 'export PATH="$HOME/.gitbin:$PATH"' >> ~/.bashrc
$ source ~/.bashrc # update PATH in your current shell
$ git fetchThemAll
如果您愿意,可以使用用户cfi为这个oneliner使用shell别名
aliasfetchThemAll=gitbranch-a|grep-vHEAD|perl-ne'chomp($_);s |^\*?\s*||;如果(m|(.+)/(.+)|&¬$d{$2}){print qq(git branch--track$2$1/$2\n)}否则{$d{$_}=1}‘|csh-xfs
推荐文章
- 如何撤销在git的最后一次提交
- 如何在CentOS 8.x/7.x/6.x上安装最新版本的git
- Git合并两个本地分支
- Git错误:src refspec master不匹配
- 如果本地Git存储库发生了更改,如何检入Bash脚本?
- 将当前的更改放到一个新的Git分支中
- 是否有可能将git隐藏到远程存储库?
- 用Git删除一个本地分支
- 如何git重基一个分支与上命令?
- 我可以安排仓库到Github上的文件夹?
- Git忽略所有特定类型的文件,除了特定子文件夹中的文件
- Git不能在MacOS上工作Catalina:“xcrun: error: invalid active developer path (/Library/ developer /CommandLineTools), missing”
- 在远程存储库上提交历史记录
- 更新包。Json自动版本
- 设置git父指针指向不同的父指针