合并,更新和拉动Git分支而不使用结账 [英] Merge, update, and pull Git branches without using checkouts

查看:108
本文介绍了合并,更新和拉动Git分支而不使用结账的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我工作的项目有2个分支,A和B.我通常在分支A上工作,并从分支B合并东西。对于合并,我通常会这样做:

  git merge origin / branchB 

然而,我也想保留一个分支B的本地副本,因为我偶尔会在没有首先与分支A合并的情况下检查分支。为此,我会这样做:

  git checkout branchB 
git pull
git checkout branchA

有没有办法在一个命令中执行上述操作,而不必前后切换分支?我应该为此使用 git update-ref 吗?如何?

解决方案

简答



只要你'重新进行快进合并,那么您可以简单地使用

  git fetch< remote> < sourceBranch>:其中destinationBranch> 

例子:

 #将本地分支foo合并到本地分支主机
#中,而不必首先签出主机。
#这里`.`表示使用本地存储库作为远程:
git fetch。 foo:master
$ b $#将远程分支origin / foo合并到本地分支foo,
#而不必先结算foo:
git fetch origin foo:foo

虽然 Amber的回答也可以在快进的情况下工作,以这种方式使用 git fetch 而不是仅仅强制移动分支引用,因为只要你在refspec中没有使用 + ,git fetch 就会自动防止意外的非快进。



长答案



如果分支B不检出A,如果它会导致非快速前进合并。这是因为需要工作副本来解决任何潜在的冲突。



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



以下是一个示例更新 master (禁止非快进更改),如果您有另一个分支功能已签出:

  git fetch upstream master:master 

这个用例非常常见,您可能想在您的git配置文件中为它创建一个别名,如下所示:

  [别名] 
sync =!sh -c'git checkout --quiet HEAD; git获取上游主人:主人; git checkout --quiet - '

这个别名的作用如下:


  1. git checkout HEAD :这会将您的工作副本置于分离头状态。如果您想要在检出时更新 master ,这很有用。我认为这是必要的,否则 master 的分支引用将不会移动,但我不记得这是否真的是我的顶部头部。

  2. git fetch upstream master:master :这会快速转发您的本地 master upstream / master 相同。 git checkout - 检出您之前签出的分支(这就是 - 在这种情况下所做的)对于(非)fast-only语法, git fetch 的语法如下:


  3. $ b

    正向合并



    如果您希望 fetch 命令在更新不是快进时失败,那么您只需使用表格的一个refspec就可以了。

    $ g $ fetch< remote> <远程分支>:其中localBranch>

    如果您想允许非快进更新,那么您添加一个 + 到refspec的前面:

      git fetch< remote> + LT;远程分支>:其中localBranch> 

    请注意,您可以使用将本地回购作为remote 。

      git fetch。 < sourceBranch>:其中destinationBranch> 



    文档



    git fetch 解释此语法的文档(强调我的):


    < refspec>



    < refspec> 参数的格式是可选的 + ,然后是源码ref < src> ,后跟一个冒号,接着是目标ref < dst>



    匹配< src> code>被取出,如果< dst> 不是空字符串,则与之相匹配的本地ref使用< ; SRC> 即可。如果使用可选的plus + ,即使不引起快速更新,本地ref也会被更新。




    另请参阅




    1. git checkout and merge,but not touch touch working tree


    2. 合并而不更改工作目录



    I work on a project that has 2 branches, A and B. I typically work on branch A, and merge stuff from branch B. For the merging, I would typically do:

    git merge origin/branchB
    

    However, I would also like to keep a local copy of branch B, as I may occasionally check out the branch without first merging with my branch A. For this, I would do:

    git checkout branchB
    git pull
    git checkout branchA
    

    Is there a way to do the above in one command, and without having to switch branch back and forth? Should I be using git update-ref for that? How?

    解决方案

    The Short Answer

    As long as you're doing a fast-forward merge, then you can simply use

    git fetch <remote> <sourceBranch>:<destinationBranch>
    

    Examples:

    # 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
    

    While Amber's answer will also work in fast-forward cases, using git fetch in this way instead is a little safer than just force-moving the branch reference, since git fetch will automatically prevent accidental non-fast-forwards as long as you don't use + in the refspec.

    The Long Answer

    You cannot merge a branch B into branch A without checking out A first if it would result in a non-fast-forward merge. This is because a working copy is needed to resolve any potential conflicts.

    However, in the case of fast-forward merges, this is possible, because such merges can never result in conflicts, by definition. To do this without checking out a branch first, you can use git fetch with a refspec.

    Here's an example of updating master (disallowing non-fast-forward changes) if you have another branch feature checked out:

    git fetch upstream master:master
    

    This use-case is so common, that you'll probably want to make an alias for it in your git configuration file, like this one:

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

    What this alias does is the following:

    1. git checkout HEAD: this puts your working copy into a detached-head state. This is useful if you want to update master while you happen to have it checked-out. I think it was necessary to do with because otherwise the branch reference for master won't move, but I don't remember if that's really right off-the-top of my head.

    2. git fetch upstream master:master: this fast-forwards your local master to the same place as upstream/master.

    3. git checkout - checks out your previously checked-out branch (that's what the - does in this case).

    The syntax of git fetch for (non-)fast-forward merges

    If you want the fetch command to fail if the update is non-fast-forward, then you simply use a refspec of the form

    git fetch <remote> <remoteBranch>:<localBranch>
    

    If you want to allow non-fast-forward updates, then you add a + to the front of the refspec:

    git fetch <remote> +<remoteBranch>:<localBranch>
    

    Note that you can pass your local repo as the "remote" parameter using .:

    git fetch . <sourceBranch>:<destinationBranch>
    

    The Documentation

    From the git fetch documentation that explains this syntax (emphasis mine):

    <refspec>

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

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

    See Also

    1. Git checkout and merge without touching working tree

    2. Merging without changing the working directory

    这篇关于合并,更新和拉动Git分支而不使用结账的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆