将所有远程 Git 分支作为本地分支进行跟踪
将单个远程分支作为本地分支进行跟踪非常简单。
git checkout --track -b ${branch_name} origin/${branch_name}
将所有本地分支推送到远程,根据需要创建新的远程分支也很容易。
git push --all origin
我想做相反的事情。 如果我在单个源上有 X 个远程分支:
git branch -r
输出:
branch1
branch2
branch3
.
.
.
我可以为所有这些远程分支创建本地跟踪分支,而无需手动创建每个分支吗? 可以这样说:
git checkout --track -b --all origin
我已经用谷歌搜索并阅读了手册,但到目前为止还没有找到答案。
Tracking a single remote branch as a local branch is straightforward enough.
git checkout --track -b ${branch_name} origin/${branch_name}
Pushing all local branches up to the remote, creating new remote branches as needed is also easy.
git push --all origin
I want to do the reverse. If I have X number of remote branches at a single source:
git branch -r
Output:
branch1
branch2
branch3
.
.
.
Can I create local tracking branches for all those remote branches without needed to manually create each one? Say something like:
git checkout --track -b --all origin
I've googled and read the manuals, but have come up bunk thus far.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(17)
VonC 的解决方案可以进一步简化:更改 sed (我缺乏直接评论他的帖子的代表点):
通过替换所有内容直到最后一个斜杠都不是斜杠,剩余的分支名称适合本地使用; 重复切换到同一个分支并不是一个错误(它可能效率低下,但它可能比在管道中使用 grep 更有效:->)。
switch 命令足够智能,可以根据需要跟踪每个远程分支。
VonC's solution can be simplified even further by changing the sed (I lack the rep points to comment directly on his post):
By replacing everything that isn't a slash up to the final slash, the remaining branch name is suitable for local use; repeatedly switching to the same branch isn't an error (it may be inefficient, but it may be more efficient than having a grep in the pipe :->).
The switch command is smart enough to track each remote branch as necessary.
使用重击,
如果您想签出所有分支:
请务必注意,当您执行获取新的远程跟踪分支的操作时,您不会自动拥有它们的本地可编辑副本。
Using Bash,
if you want to checkout all branches:
It’s important to note that when you do a fetch that brings down new remote-tracking branches, you don’t automatically have local, editable copies of them.
使用:
解释:
第 1 行:'gitbranch -r'(后跟'gitremoteupdate'以更新远程更改的信息)列出所有远程分支; 'egrep -vw' 用于敲入结果中包含 HEAD 和 master 的条目。
第 3 行:在本地检出时跟踪指定的远程分支。 使用简单的 AWK 来避免“origin/”成为本地分支的后缀。
Use:
Explanation:
Line 1: 'git branch -r' (followed by 'git remote update' to update the info on changes to remote) lists all remote branches; 'egrep -vw' is used to knock entries having HEAD and master in the result.
Line 3: Track the named remote branch while checking it out locally. A simple AWK is used to avoid 'origin/' being the suffix for local branches.
使用:
这将扩展为类似:
Use:
This will expand to something like:
如果您已经签出一些分支并希望
您可以使用以下 Bash 和 Z shell 兼容脚本:
In case you already have some branches checked out and want to
you can use the following Bash- and Z shell-compatible script:
奥托给出的答案很好,但所有创建的分支都将以“origin/”作为名称的开头。 如果您只想将最后一部分(最后一个
/
之后)作为结果分支名称,请使用此命令:它还有一个好处是不会向您发出任何有关不明确引用的警告。
The answer given by Otto is good, but all the created branches will have "origin/" as the start of the name. If you just want the last part (after the last
/
) to be your resulting branch names, use this:It also has the benefit of not giving you any warnings about ambiguous refs.
使用 Bash:
Git 1.9.1 之后
Git 1.9.1 之前
更新分支,假设本地跟踪分支没有变化:
忽略不明确的引用名称警告,Git 似乎更喜欢本地分支。
Using Bash:
After Git 1.9.1
Before Git 1.9.1
Update the branches, assuming there are no changes on your local tracking branches:
Ignore the ambiguous refname warnings, Git seems to prefer the local branch as it should.
这里的大多数答案都使 gitbranch -r 的输出解析变得过于复杂。 您可以使用以下
for
循环针对远程上的所有分支创建跟踪分支,如下所示。示例
假设我有这些远程分支。
确认我们没有在本地跟踪除 master 之外的任何内容:
您可以使用这一行来创建跟踪分支:
现在确认:
要删除它们:
如果您使用
-vv
切换到 < code>gitbranch 你可以确认:for循环的分解
该循环基本上调用命令
gitbranch-r
,使用grep过滤掉输出中的任何HEAD或master分支 - vE“HEAD|master”
。 为了获取减去origin/
子字符串的分支名称,我们使用 Bash 的字符串操作${var#stringtoremove}
。 这将从变量$var
中删除字符串“stringtoremove”。 在我们的例子中,我们从变量$i
中删除字符串origin/
。注意: 或者,您也可以使用
git checkout --track ...
来执行此操作:但我并不特别喜欢这种方法,因为它会在您之间切换执行结帐时的分支。 完成后,您将停留在它创建的最后一个分支上。
参考文献
Most of the answers here are over complicating the parsing of the output of
git branch -r
. You can use the followingfor
loop to create the tracking branches against all the branches on the remote like so.Example
Say I have these remote branches.
Confirm that we're not tracking anything other than master already, locally:
You can use this one liner to create the tracking branches:
Now confirm:
To delete them:
If you use the
-vv
switch togit branch
you can confirm:Breakdown of for loop
The loop basically calls the command
git branch -r
, filtering out any HEAD or master branches in the output usinggrep -vE "HEAD|master"
. To get the names of just the branches minus theorigin/
substring we use Bash's string manipulation${var#stringtoremove}
. This will remove the string, "stringtoremove" from the variable$var
. In our case we're removing the stringorigin/
from the variable$i
.NOTE: Alternatively you can use
git checkout --track ...
to do this as well:But I don't particularly care for this method, since it's switching you among the branches as it performs a checkout. When done it'll leave you on the last branch that it created.
References
2020 年第一季度更新:Mohsen Abasi 提议评论中,基于 2014 年 slm 的 答案,更简单的替代方案:
它使用
$()
而不是过时的反引号。正如我在另一个旧答案中提到的,使用
git for-each-ref
是 可能更快。我会使用新的(Git 2.23+)
git switch
命令,它取代了令人困惑的git checkout
。这样,就不需要
grep
了。旧(2011)原始答案:
这是我使用的一句话(在 bash shell 中,使用 msysgit1.7.4 进行测试):
用于复制粘贴:
为了提高可读性:
它只会从您的远程选择上游分支在
remote
变量中指定(可以是“origin
”或您为当前 Git 存储库的远程之一设置的任何名称)。它将提取分支的名称:
origin/a/Branch/Name => a/Branch/Name
通过awk
表达式。它将通过
--set-upstream-to
(或- u
),而不是--track
:优点是,如果分支已经存在,它不会失败,也不会更改该分支源,它只会配置
branch.xxx.(remote|merge)
设置。< /p>该命令将为所有远程上游分支创建本地分支,并将其远程和合并设置设置为该远程分支。
Update Q1 2020: Mohsen Abasi proposes in the comments, based on the 2014 slm's answer, the simpler alternative:
And it uses
$()
instead of obsolete backticks.As I mention in another old answer, using
git for-each-ref
is probably faster.And I would use the new (Git 2.23+)
git switch
command, which replaces the confusinggit checkout
.That way, no
grep
needed.Old (2011) original answer:
Here is my one-liner I use (in a bash shell, tested with msysgit1.7.4):
For copy-paste:
For more readability:
it will only select upstream branches from the remote you specify in the
remote
variable (it can be 'origin
' or whatever name you have set for one of the remotes of your current Git repo).it will extract the name of the branch:
origin/a/Branch/Name => a/Branch/Name
through theawk
expression.it will set the upstream branch through
--set-upstream-to
(or-u
), not--track
:The advantage is that, if the branch already exists, it won't fail and it won't change that branch origin, it will only configure the
branch.xxx.(remote|merge)
setting.That command will create local branches for all remote upstream branches, and set their remote and merge setting to that remote branch.
没有任何脚本(在空目录中):
之后,所有远程分支将被视为本地分支。
原文(俄语)。
Without any scripting (in an empty directory):
After that, all remote branches will be seen as local.
Original (in Russian).
您可以很容易地编写脚本,但我不知道它什么时候有价值。 这些分支很快就会落后,你必须一直更新它们。
远程分支会自动保持最新状态,因此最简单的方法是在您实际想要处理的位置创建本地分支。
You could script that easily enough, but I don't know when it'd be valuable. Those branches would pretty quickly fall behind, and you'd have to update them all the time.
The remote branches are automatically going to be kept up to date, so it's easiest just to create the local branch at the point where you actually want to work on it.
如果您想使用 powershell 并且您的遥控器称为 origin。 然后这行得通。
If you want to use powershell and your remote is called origin. Then this works.
使用这个,你将不会收到这样的警告: refname 'origin/dev' is ambigacy
Use this and you will not have such warning as: refname 'origin/dev' is ambiguous
要执行与 tjmcewan 的答案相同的操作,但在 Windows 上,请从批处理文件调用此命令:
或者来自命令行:
To do the same as tjmcewan's answer but on Windows, call this from a batch file:
Or this from the command line:
这是我对 @tjmcewan 引用的 Bash 命令的解决方案:
我的目标是解决所有创建的分支都以“origin/”作为名称开头的问题,因为我测试了 $remote 变量仍然包含“起源/”:
Here is my solution of a Bash command referred to by @tjmcewan:
My goal is to solve the problem that all the created branches will have "origin/" as the start of the name, because I tested that $remote variables are still include "origin/":
从 Git 2.23 开始:
git switch
的-C
标志将创建或重置(如果已存在)。Git 切换文档
From Git 2.23 onwards:
The
-C
flag forgit switch
creates or resets if it already exists.Git switch documentation