如何将提交从一个分支复制到另一个分支? [英] How to copy commits from one branch to another?

查看:263
本文介绍了如何将提交从一个分支复制到另一个分支?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的主人有两个分支:


  • v2.1 :(版本2)我一直在努力工作几个月

  • wss :我昨天创建了一个特定功能添加到我的主人(生产中)



  • 有没有办法将昨天的提交从wss复制到v2.1?

    解决方案

    您应该有一个工作流程,可以让您通过合并来完成这项工作:

       -  x  - x  -  x(v2) -  x  -  x  -  x(v2.1)
    \
    x - x - x(wss)
    git checkout v2.1 和 git merge wss code>。如果由于某种原因,您确实无法做到这一点,并且您无法使用 git rebase 将你的wss分支移动到正确的位置,从某处抓取单个提交并将其应用于其他地方的命令是 git cherry-pick 。只需查看要应用它的分支,然后运行 git cherry-pick<提交到cherry-pick的SHA>



    rebase可能会为您节省一些费用:



    如果您的历史记录如下所示:

       -  x  -  x  -  x(v2) -  x  -  x  -  x(v2.1)
    \
    x - x - x(v2-only ) - x - x - x(wss)

    您可以使用 git rebase

       -  x  -  x -  x(v2) -  x  -  x  -  x(v2.1)
    | \
    | x - x - x(v2-only)
    \
    x - x - x(wss)

    然后你可以合并!如果真的真的 b $ b

     #wss-starting-point是第一次提交之前的SHA1 /分支
    git分支wss-to-rebase wss
    git rebase --onto v2.1 wss-starting-point wss-to-rebase
    git checkout v2.1
    git merge wss-to-rebase

    注意:为此需要额外工作的原因是它会在存储库中创建重复的提交。这并不是一件好事 - 容易分支和合并的关键是能够通过将提交放在一个位置并将它们合并到任何需要的位置来完成所有工作。重复提交意味着永远不会合并这两个分支(如果您决定稍后想要,会发​​生冲突)。


    I've got two branches from my master:

    • v2.1: (version 2) I've been working on for several months
    • wss: that I created yesterday to add one specific feature to my master (in production)

    Is there a way to copy yesterday's commits from wss to v2.1?

    解决方案

    You should really have a workflow that lets you do this all by merging:

    - x - x - x (v2) - x - x - x (v2.1)
               \
                x - x - x (wss)
    

    So all you have to do is git checkout v2.1 and git merge wss. If for some reason you really can't do this, and you can't use git rebase to move your wss branch to the right place, the command to grab a single commit from somewhere and apply it elsewhere is git cherry-pick. Just check out the branch you want to apply it on, and run git cherry-pick <SHA of commit to cherry-pick>.

    Some of the ways rebase might save you:

    If your history looks like this:

    - x - x - x (v2) - x - x - x (v2.1)
               \
                x - x - x (v2-only) - x - x - x (wss)
    

    You could use git rebase --onto v2 v2-only wss to move wss directly onto v2:

    - x - x - x (v2) - x - x - x (v2.1)
              |\
              |  x - x - x (v2-only)
               \
                 x - x - x (wss)
    

    Then you can merge! If you really, really, really can't get to the point where you can merge, you can still use rebase to effectively do several cherry-picks at once:

    # wss-starting-point is the SHA1/branch immediately before the first commit to rebase
    git branch wss-to-rebase wss
    git rebase --onto v2.1 wss-starting-point wss-to-rebase
    git checkout v2.1
    git merge wss-to-rebase
    

    Note: the reason that it takes some extra work in order to do this is that it's creating duplicate commits in your repository. This isn't really a good thing - the whole point of easy branching and merging is to be able to do everything by making commit(s) one place and merging them into wherever they're needed. Duplicate commits mean an intent never to merge those two branches (if you decide you want to later, you'll get conflicts).

    这篇关于如何将提交从一个分支复制到另一个分支?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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