git cherry-pick or merge specific directory from another branch

后端 未结 5 1211
無奈伤痛
無奈伤痛 2020-12-31 01:00

I\'ve seen different posts on StackOverflow that explain cherry picking a bit, but the comments in their code aren\'t very specific as to what\'s a branch and what\'s a dire

5条回答
  •  隐瞒了意图╮
    2020-12-31 01:39

    To answer the original question about how to cherry-pick some directories (as commits instead of a brute-force checkout), this is possible. Imagine that featureA has diverged from master and you want to bring over the tools/my-tool commits.

    Assuming that you never made any commits that contain both stuff from /tools/my-tool and stuff from other directories

    This will get you the list of commits to master in tools/my-tool (that are not already in featureA), in reverse-chronological order:

    git log --no-merges featureA...master tools/my-tool
    

    To say it another way:

    git log --no-merges source_branch...dest_branch my/firstpath my/secondpath [...]
    

    To get just the commits you need in chronological order, you need to first reverse the order of the input lines (such as with tail -r or tac), then isolate the column for the commit hash (such as with cut):

    git log --format=oneline --no-merges featureA...master tools/my-tool \
        | tail -r \
        | cut -d " " -f 1
    

    And to do the whole operation at once, do this:

    git cherry-pick $(git log --format=oneline --no-merges featureA...master tools/my-tool | tail -r | cut -d " " -f 1)
    

提交回复
热议问题