我克隆了一个包含许多分支的Git存储库。然而,git分支仅显示一个:
$ git branch
* master
我如何在本地拉所有分支,所以当我执行git分支时,它会显示以下内容?
$ git branch
* master
* staging
* etc...
我克隆了一个包含许多分支的Git存储库。然而,git分支仅显示一个:
$ git branch
* master
我如何在本地拉所有分支,所以当我执行git分支时,它会显示以下内容?
$ git branch
* master
* staging
* etc...
当前回答
如果您在这里寻求一个解决方案来获取所有分支,然后将所有内容迁移到另一个Git服务器,我将完成以下过程。如果您只想在本地更新所有分支,请停止在第一个空行。
git clone <ORIGINAL_ORIGIN>
git branch -r | awk -F'origin/' '!/HEAD|master|main/{print $2 " " $1"origin/"$2}' | xargs -L 1 git branch -f --track
git fetch --all --prune --tags
git pull --all
git remote set-url origin <NEW_ORIGIN>
git pull
<resolve_any_merge_conflicts>
git push --all
git push --tags
<check_NEW_ORIGIN_to_ensure_it_matches_ORIGINAL_ORIGIN>
其他回答
只有这三个命令才能获得所有分支:
git clone --mirror repo.git .git (gets just .git - bare repository)
git config --bool core.bare false
git reset --hard
您需要创建跟踪远程分支的本地分支。
假设您只有一个名为origin的远程,此代码片段将为所有远程跟踪分支创建本地分支:
for b in `git branch -r | grep -v -- '->'`; do git branch --track ${b##origin/} $b; done
之后,gitfetch-all将更新远程分支的所有本地副本。
此外,gitpull-all将更新您的本地跟踪分支,但根据您的本地提交以及“合并”配置选项的设置方式,它可能会创建一个合并提交、快进或失败。
玩git时要小心,循序渐进。
$ git remote update //This will update your local
$ git branch -a //This will list all the branches(After updating you can now
see the new branch in the list)
$ git checkout your_branch_name
对于Visual Studio用户,在包管理器控制台上:
git branch | %{ git fetch upstream; git merge upstream/master}
下面是接受答案中提供的一行程序的Perl版本:
git branch-r | perl-e'while(<>){chop;my$remote=$_;my($local)=($remote=~/origin\/(.*)/);打印“git branch--track$local$remote\n”;}'>一些输出文件
如果愿意,可以将输出文件作为Shell脚本运行。
我们无意中删除了Stash项目存储库。幸运的是,就在意外损失之前,有人制造了一个叉子。我将叉子克隆到了我的本地(将省略我是如何做到这一点的细节)。当我把叉子完全放在本地时,我跑了一条单行道。我修改了远程服务器的URL(在我的例子中是源URL),以指向我们要恢复到的目标存储库:
gitremoteseturl-origin<remoteurl>
最后将所有分支都推到原点,如下所示:
git push—所有原点
我们又回到了生意上。