如何获取所有Git分支

这篇博客详细介绍了如何获取并拉取Git仓库中的所有远程分支。通过各种Git命令,如`git branch -r`,`git checkout -b <branch> origin/<branch>`等,可以实现本地对远程分支的跟踪和更新。文中还提供了不同场景下的命令组合,适用于不同的用户需求,包括Bash脚本、PowerShell命令和针对Visual Studio用户的解决方案。
摘要由CSDN通过智能技术生成

我克隆了一个Git存储库,它包含大约五个分支。 但是,当我做git branch我只看到其中一个:

$ git branch
* master

我知道我可以做git branch -a来查看所有分支,但是我如何在本地拉出所有分支,所以当我做git branch ,它会显示以下内容?

$ git branch
* master
* staging
* etc...

#1楼

$ git remote update
$ git pull --all

这假定跟踪所有分支。

如果他们不是你可以用Bash解雇这个:

for remote in `git branch -r `; do git branch --track $remote; done

然后运行该命令。


#2楼

您可以从所有遥控器中获取所有分支,如下所示:

git fetch --all

这基本上是一种权力移动

fetch更新远程分支的本地副本,因此这对于您的本地分支来说总是安全的但是

  1. fetch不会更新本地分支( 跟踪远程分支); 如果你想更新你的本地分支,你仍然需要拉每个分支。

  2. fetch不会创建本地分支( 跟踪远程分支),您必须手动执行此操作。 如果要列出所有远程分支: git branch -a

更新跟踪远程分支的本地分支:

git pull --all

但是,这仍然不够。 它仅适用于跟踪远程分支的本地分支。 要跟踪所有远程分支,请执行此oneliner BEFORE git pull --all

git branch -r | grep -v '\->' | while read remote; do git branch --track "${remote#origin/}" "$remote"; done

TL; DR版本

git branch -r | grep -v '\->' | while read remote; do git branch --track "${remote#origin/}" "$remote"; done
git fetch --all
git pull --all

(看起来拉取所有遥控器的所有分支,但我总是首先获取以确定。)

仅当服务器上存在未由本地分支跟踪的远程分支时,才运行第一个命令。

PS AFAIK git fetch --allgit remote update是等效的。



卡米尔索佐的评论 ,人们发现有用。

我不得不使用:

 for remote in `git branch -r`; do git branch --track ${remote#origin/} $remote; done 

因为你的代码创建了名为origin/branchname本地分支,而且每当我提到它时,我得到的“refname”origin / branchname'都是模棱两可的。


#3楼

列出远程分支:
git branch -r

您可以将它们作为本地分支机构查看:
git checkout -b LocalName origin/remotebranchname


#4楼

如果你这样做:

git fetch origin

然后他们将在当地。 如果你然后执行:

git branch -a

你会看到它们被列为遥控器/原点/分支名称。 因为他们在当地,你可以随心所欲地做任何事情。 例如:

git diff origin/branch-name 

要么

git merge origin/branch-name

要么

git checkout -b some-branch origin/branch-name

#5楼

Bash for循环对我来说不起作用,但这完全符合我的要求。 我的所有分支都在本地镜像为同名。

git checkout --detach
git fetch origin '+refs/heads/*:refs/heads/*'

请参阅Mike DuPont的评论如下。 我想我试图在Jenkins服务器上执行此操作,使其处于分离头模式。


#6楼

循环似乎不适合我,我想忽略原点/主人。 这对我有用。

git branch -r | grep -v HEAD | awk -F'/' '{print $2 " " $1"/"$2}' | xargs -L 1 git branch -f --track

之后:

git fetch --all
git pull --all

#7楼

您将需要创建跟踪远程分支的本地分支。

假设您只有一个名为origin远程服务器,此代码段将为所有远程跟踪服务器创建本地分支:

for b in `git branch -r | grep -v -- '->'`; do git branch --track ${b##origin/} $b; done

之后, git fetch --all将更新远程分支的所有本地副本。

此外, git pull --all将更新本地跟踪分支,但根据您的本地提交以及如何设置'merge'configure选项,它可能会创建合并提交,快进或失败。


#8楼

如果您在这里寻求获得所有分支的解决方案,然后将所有分支迁移到另一个Git服务器,我将以下过程放在一起。 如果您只想让所有分支在本地更新,请在第一个空行停止。

git clone <ORIGINAL_ORIGIN>
git branch -r | awk -F'origin/' '!/HEAD|master/{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>

#9楼

克隆主存储库后,您就可以执行了

git fetch && git checkout <branchname>

#10楼

使用git fetch && git checkout RemoteBranchName

它对我很有用......


#11楼

我写了一个小脚本来管理克隆新的repo并为所有远程分支创建本地分支。

你可以在这里找到最新版本:

#!/bin/bash

# Clones as usual but creates local tracking branches for all remote branches.
# To use, copy this file into the same directory your git binaries are (git, git-flow, git-subtree, etc)

clone_output=$((git clone "$@" ) 2>&1)
retval=$?
echo $clone_output
if [[ $retval != 0 ]] ; then
    exit 1
fi
pushd $(echo $clone_output | head -1 | sed 's/Cloning into .\(.*\).\.\.\./\1/') > /dev/null 2>&1
this_branch=$(git branch | sed 's/^..//')
for i in $(git branch -r | grep -v HEAD); do
  branch=$(echo $i | perl -pe 's/^.*?\///')
  # this doesn't have to be done for each branch, but that's how I did it.
  remote=$(echo $i | sed 's/\/.*//')
  if [[ "$this_branch" != "$branch" ]]; then
      git branch -t $branch $remote/$branch
  fi
done
popd > /dev/null 2>&1

要使用它,只需将其复制到你的git bin目录(对我来说,那是C:\\Program Files (x86)\\Git\\bin\\git-cloneall ),然后在命令行上:

git cloneall [standard-clone-options] <url>

它像往常一样克隆,但为所有远程分支创建本地跟踪分支。


#12楼

我通常只使用这样的命令:

git fetch origin
git checkout --track origin/remote-branch

更短的版本:

git fetch origin
git checkout -t origin/remote-branch

#13楼

基于Learath2的回答,这是我在执行git clone [...]cd -ing到创建的目录后所做的事情:

git branch -r | grep -v master | awk {print\\$1} | sed 's/^origin\\/\\(.*\\)$/\\1 &/' | xargs -n2 git checkout -b

为我工作,但我不知道它对你有用。 小心。


#14楼

只需这三个命令就可以获得所有分支:

git clone --mirror repo.git  .git     (gets just .git  - bare repository)

git config --bool core.bare false

git reset --hard

#15楼

克隆存储库时,实际下载了分支的所有信息,但隐藏了分支。 用命令

$ 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. 第三步

    抓取当前文件夹中的所有内容并在本地计算机上创建所有分支,从而使其成为正常的repo。

     $ git reset --hard 

所以现在你可以输入命令git branch ,你可以看到所有的分支都被下载了。

这是您可以快速克隆包含所有分支的git存储库的快速方法,但这并不是您希望以这种方式为每个项目执行的操作。


#16楼

git remote add origin https://yourBitbucketLink

git fetch origin

git checkout -b yourNewLocalBranchName origin/requiredRemoteBranch (use tab :D)

现在本地你的yourNewLocalBranchName是你的requiredRemoteBranch


#17楼

我们可以将所有分支或标记名称放在一个临时文件中,然后为每个名称/标记执行git pull:

git branch -r | grep origin | grep -v HEAD| awk -F/ '{print $NF}' > /tmp/all.txt
git tag -l >> /tmp/all.txt
for tag_or_branch in `cat /tmp/all.txt`; do git checkout $tag_or_branch; git pull origin $tag_or_branch; done

#18楼

我相信你已经通过以下方式克隆了存储库:

git clone https://github.com/pathOfrepository

现在使用cd转到该文件夹​​:

cd pathOfrepository

如果您输入git status您可以看到所有:

   On branch master
Your branch is up-to-date with 'origin/master'.
nothing to commit, working directory clean

要查看所有隐藏的分支类型:

 git branch -a

它将列出所有远程分支。

现在,如果您想要在任何特定分支上结帐,只需输入:

git checkout -b localBranchName origin/RemteBranchName

#19楼

您可以通过以下方式获取所有分支:

git fetch --all

要么:

git fetch origin --depth=10000 $(git ls-remote -h -t origin)

如果您使存储库变浅,则--depth=10000参数可能会有所帮助。


要拉出所有分支,请使用:

git pull --all

如果上面的命令不起作用,那么在上面的命令之前用:

git config remote.origin.fetch '+refs/heads/*:refs/remotes/origin/*'

因为remote.origin.fetch在获取时只能支持特定的分支,特别是当你用--single-branch克隆你的repo时。 通过以下方式检查: git config remote.origin.fetch

之后你应该能够结账任何分支机构。

也可以看看:


要将所有分支推送到远程,请使用:

git push --all

最终--mirror镜像所有裁判。


如果您的目标是复制存储库,请参阅:在GitHub上复制存储库文章。


#20楼

对于使用PowerShell的Windows用户:

git branch -r | ForEach-Object {
    # Skip default branch, this script assumes
    # you already checked-out that branch when cloned the repo
    if (-not ($_ -match " -> ")) {
        $localBranch = ($_ -replace "^.*/", "")
        $remoteBranch = $_.Trim()
        git branch --track "$localBranch" "$remoteBranch"
    }
}
git fetch --all
git pull --all

#21楼

确保所有远程分支都可以在.git/config文件中获取。

在这个例子中,只有origin/production分支是可获取的,即使你尝试进行git fetch --all除了获取production分支之外什么都不会发生:

[origin]
fetch = +refs/heads/production:refs/remotes/origin/production

该行应替换为:

[origin]
fetch = +refs/heads/*:refs/remotes/origin/*

然后运行git fetch等...


#22楼

这是我认为强大的东西:

  • 不更新现有分支的远程跟踪
  • 不尝试更新HEAD以跟踪origin/HEAD
  • 允许以origin命名的遥控器
  • 适当的壳报价
for b in $(git branch -r --format='%(refname:short)'); do
  [[ "${b#*/}" = HEAD ]] && continue
  git show-ref -q --heads "${b#*/}" || git branch --track "${b#*/}" "$b";
done
git pull --all

没有必要git fetch --all作为传递-allgit pull将此选项传递给内部fetch

相信这个答案


#23楼

这是在接受的答案中提供的Perl版本的单行程序:

git branch -r | perl -e 'while(<>) {chop; my $remote = $_; my ($local) = ($remote =~ /origin\\/(.*)/); print "git branch --track $local $remote\\n";}' > some-output-file

如果您愿意,可以将输出文件作为Shell脚本运行。

我们意外删除了Stash项目存储库。 幸运的是,有人在意外丢失之前创造了一个分叉。 我把叉子克隆到我的本地(将省略我如何做的细节)。 一旦我把叉子完全放在我的本地,我跑了一个单线。 我修改了远程的URL(在我的情况下为origin),指向我们正在恢复的目标存储库:

git remote set-url origin <remote-url>

最后将所有分支推送到原点,如下所示:

git push --all origin

我们又回来了。


#24楼

如果你有fetch --all问题,那么跟踪你的远程分支:

git checkout --track origin/%branchname%

#25楼

要避免错误消息'致命:名为'origin / master'的分支已存在。',您需要这样:

git branch -r | grep -v '\->'  | grep -v `git branch | awk '/\*/ { print $2; }'`| while read remote; do git branch --track "${remote#origin/}" "$remote"; done

#26楼

如何获取所有Git分支跟踪单个远程。

这已经在Windows 10上的Red Hat和Git Bash上进行了测试和功能。


TLDR:

for branch in `git branch -r|grep -v ' -> '|cut -d"/" -f2`; do git checkout $branch; git fetch; done;

说明:

一个班轮检出然后取出除HEAD之外的所有分支。

列出远程跟踪分支。

git branch -r

忽略HEAD。

grep -v ' -> '

从远程(s)获取分支名称。

cut -d"/" -f2

检查所有分支跟踪单个远程。

git checkout $branch

获取签出的分支。

git fetch

从技术上讲,新的本地分支机构不需要提取。

这可以用于fetchpull fetch既是新的并且在远程中具有变化的分支。

如果您准备好合并,请确保只拉动。


测试设置

使用SSH URL检查存储库。

git clone git@repository.git

之前

检查当地的分支机构。

$ git branch
* master

执行命令

执行一个班轮。

for branch in `git branch -r|grep -v ' -> '|cut -d"/" -f2`; do git checkout $branch; git fetch; done;

检查本地分支包括远程分支。

$ git branch
  cicd
  master
* preprod

#27楼

对于Visual Studio用户,在程序包管理器控制台上:

git branch | %{git fetch upstream; git merge upstream / master}


#28楼

尝试了很多方法,只有这一个很简单,适合我。

for branch in $(git ls-remote -h git@<your_repository>.git | awk '{print $2}' | sed 's:refs/heads/::')
do
  git checkout "$branch"
  git pull
done
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值