git 拉取合并分支_合并,更新和拉取Git分支而不使用检出

短回答

只要你做快进合并,那么你可以简单地使用

git fetch :

例子:

# Merge local branch foo into local branch master,

# without having to checkout master first.

# Here `.` means to use the local repository as the "remote":

git fetch . foo:master

# Merge remote branch origin/foo into local branch foo,

# without having to checkout foo first:

git fetch origin foo:foo

虽然Amber’s answer也将工作在快进的情况下,使用git fetch这种方式,而不是只是强制移动分支引用一点,因为git fetch将自动防止意外非快进,只要你不在refspec中使用。

长答案

您不能将分支B合并到分支A中,而不检查A first,如果它将导致非快进合并。这是因为需要一个工作副本来解决任何潜在的冲突。

然而,在快进合并的情况下,这是可能的,因为根据定义,这样的合并永远不会导致冲突。要做到这一点,首先不检出分支,你可以使用git fetch与refspec。

这里有一个例子,如果您有另一个分支功能签出,更新主(不允许非快进)更改:

git fetch upstream master:master

这个用例是如此常见,你可能想要在你的git配置文件中为它做一个别名,像这样:

[alias]

sync = !sh -c 'git checkout --quiet HEAD; git fetch upstream master:master; git checkout --quiet -'

这个别名的作用如下:

> git checkout HEAD:这将使你的工作副本处于脱离头状态。这是有用的,如果你想更新master,而你恰巧有它签出。我认为这是必要的,因为否则master的分支引用不会移动,但我不记得如果这真的是我的头顶。

> git fetch upstream master:master:将本地主机快速转发到与origin / master相同的位置。

> git checkout – 检查你以前签出的分支(这是什么 – 在这种情况下)。

(非)快进合并的git fetch的语法

如果您希望fetch命令失败,如果更新是非快进,那么您只需使用形式的refspec

git fetch :

如果要允许非快速转发更新,则将ref添加到refspec的前面:

git fetch +:

请注意,您可以使用传递您的本地仓库作为“远程”参数:。

git fetch . :

文档

The format of a parameter is an optional plus +, followed by the source ref , followed by a colon :, followed by the destination ref .

The remote ref that matches is fetched, and if is not empty string, the local ref that matches it is fast-forwarded using . If the optional plus + is used, the local ref is updated even if it does not result in a fast-forward update.

也可以看看

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值