zoukankan      html  css  js  c++  java
  • git cherry-pick 从其他分支检出指定的commit到当前分支

    http://think-like-a-git.net/sections/rebase-from-the-ground-up/cherry-picking-explained.html

    Git's own online help has a great, if characteristically terse, description of what the command does:

    Given one or more existing commits, apply the change each one introduces, recording a new commit for each.

    I've already mentioned (back on the page about garbage collection) that a Git commit's ID is a hash of both its contents and its history. So, even if you have two commits that introduce the exact same change, if they point to different parent commits, they'll have different IDs.

    What git cherry-pick does, basically, is take a commit from somewhere else, and "play it back" wherever you are right now. Because this introduces the same change with a different parent, Git builds a new commit with a different ID.

    Let's go back to this example from the reachability section:

    If you were at node H in this graph, and you typed git cherry-pick E (yes, you'd actually type part or all of the SHA for the commit, but for simplicity's sake, I'll just use the labels that are already here), you'd wind up with a copy of commit E—let's call it "E prime" or E'—that pointed to H as its parent, like so:

    Or, if you typed something like git cherry-pick C D E, you'd wind up with this when you were done:

    The important thing to notice here is that Git has copied changes made in one place, and replayed them somewhere else.

    Here's a quick slideshow that steps through the process:

    原文有一个展示的系列图,想看的,可以去看原文

    通过tortoisegit来操作cherry-pick

    http://stackoverflow.com/questions/9415534/cherry-pick-using-tortoisegit

    Cherry Pick可能遇到的问题

    http://stackoverflow.com/questions/9229301/git-cherry-pick-says-38c74d-is-a-merge-but-no-m-option-was-given

    如果操作的commit是一次合并记录的话,此次的commit是有2个父节点的,需要用户指明,cherry-pick哪一个父节点。这样做,会丢失掉这次合并的记录。

    我的处理方法是,直接忽略这次的commit,不进行cherrypick

    The way a cherry-pick works is by taking the diff a changeset represents (the difference between the working tree at that point and the working tree of its parent), and applying it to your current branch.

    So, if a commit has two or more parents, it also represents two or more diffs - which one should be applied?

    You're trying to cherry pick fd9f578, which was a merge with two parents. So you need to tell the cherry-pick command which one against which the diff should be calculated, by using the -m option. For example, git cherry-pick -m 1 fd9f578 to use parent 1 as the base.

    I can't say for sure for your particular situation, but using git merge instead of git cherry-pick is generally advisable. When you cherry-pick a merge commit, it collapses all the changes made in the parent you didn't specify to -m into that one commit. You lose all their history, and glom together all their diffs. Your call.

    cherry pick遇到冲突的时候【使用tortoisegit】

    tortoisegit正在进行的cherry-pick会停止下来,需要手动处理冲突文件,然后标记为已解决。记得要自己填写commit message,记录下是怎么处理冲突的。然后再点击commit

    之后cherry pick会继续执行

    不要经常使用cherry-pick

    http://dan.bravender.net/2011/10/20/Why_cherry-picking_should_not_be_part_of_a_normal_git_workflow.html

    ===2015年09月26日更新===

    坑爹了,之前遇到的问题,cherry pick的commit如果是个合并记录的话,现在不想跳过

     http://stackoverflow.com/questions/9229301/git-cherry-pick-says-38c74d-is-a-merge-but-no-m-option-was-given

    The way a cherry-pick works is by taking the diff a changeset represents (the difference between the working tree at that point and the working tree of its parent), and applying it to your current branch.

    So, if a commit has two or more parents, it also represents two or more diffs - which one should be applied?

    You're trying to cherry pick fd9f578, which was a merge with two parents. So you need to tell the cherry-pick command which one against which the diff should be calculated, by using the -m option.

    For example, git cherry-pick -m 1 fd9f578 to use parent 1 as the base.

    I can't say for sure for your particular situation, but using git merge instead of git cherry-pick is generally advisable.

    When you cherry-pick a merge commit, it collapses all the changes made in the parent you didn't specify to -m into that one commit.

    You lose all their history, and glom together all their diffs. Your call.

    会出现提示:

    $ git cherry-pick -m 2 fa14668b19899a92b5a4db254af90b730d4bf4ba
    On branch chucklu_master
    Your branch and 'chucklu/master' have diverged,
    and have 19 and 70 different commits each, respectively.
    (use "git pull" to merge the remote branch into yours)

    You are currently cherry-picking commit fa14668.

    nothing to commit, working directory clean
    The previous cherry-pick is now empty, possibly due to conflict resolution.
    If you wish to commit it anyway, use:

    git commit --allow-empty

    Otherwise, please use 'git reset'

    上面的分析,因为cherrypick的结果,没有任何改变,是没有意义的

    http://stackoverflow.com/questions/14096244/why-is-git-cherrypick-saying-nothing-to-commit

    It's exactly what it says: the changes you're trying to cherry-pick are already wholly contained in the branch you're on. I.e. the result of the cherry-pick is no changes. You can create an empty commit with the --allow-empty flag to indicate that you attempted to cherry-pick, but there were no changes to pull in.

    cherry pick -m

    -m parent-number--mainline parent-number

    Usually you cannot cherry-pick a merge because you do not know which side of the merge should be considered the mainline.

    This option specifies the parent number (starting from 1) of the mainline and allows cherry-pick to replay the change relative to the specified parent.

    cherry-pick always use theirs when encounter conflicts

    https://stackoverflow.com/questions/14337945/how-do-i-resolve-cherry-pick-conflicts-using-their-changes

    First you should undo your cherry-pick, try to run this

    git cherry-pick --abort
    

    Second, try to make cherry-pick, but in this time you get their changes not yours, so make this:

    git cherry-pick --strategy=recursive -X theirs {Imported_Commit}
    

    https://stackoverflow.com/questions/45604767/git-cherry-picking-with-ours-theirs-strategy

    The git cherry-pick command does have the --strategy and --strategy-option=<option> options.

    They are passed through to the merge strategies.

    So, in your case:

    git cherry-pick --strategy-option=ours HASH1 HASH2 HASH3 -n

    https://stackoverflow.com/questions/21051850/force-git-to-accept-cherry-picks-changes/32468531#32468531

    you can tell it to always prefer the changes of the commit you are cherry-picking:

    git cherry-pick commitish --strategy-option theirs   这个经过测试,是工作的
    

    commitish can be a SHA-1 hash of a commit, or a branch-name for the lastest commit of that branch, branch-name~1 for the commit before that etc.

    If you want to do the reverse, use:

    git cherry-pick commitish --strategy-option ours
    

    The shorthand for --strategy-option is -X (uppercase X).

    遇到处理完之后,还会冲突的情况,HEAD是delete,cherry-pick HEAD是modified,因为两个操作不一致导致的。



    cherry-pick a range of commits

    https://stackoverflow.com/questions/46109211/cherry-picking-few-commits-from-another-branch

    Let's say the history is A-B-C-D-E-F-G, and you'd like to cherry-pick C-D-E-F.

    git cherry-pick B..F
    

    or

    git cherry-pick C^..F
    

    or

    git cherry-pick C D E F
  • 相关阅读:
    Java--学生类(成员变量,成员方法输出学生信息和平均成绩)
    Java--水仙花数
    Java--学生类(成员变量,成员方法直接输出学生信息)
    Java--二分法查找
    矩阵转换
    建立一个框架,设置文字的颜色,字体,大小和位置
    使用字符串函数(作业)
    java 实现监听事件(使用按钮连续改变标签的颜色和字号)(上机倒数第二次作业)
    786.第k个数
    蓝桥杯
  • 原文地址:https://www.cnblogs.com/chucklu/p/4707108.html
Copyright © 2011-2022 走看看