美文网首页git
git cherry-pick用法详解

git cherry-pick用法详解

作者: XBruce | 来源:发表于2020-08-18 14:39 被阅读0次

    对于多分支的代码库,往往需要切换不同分支。那么往往需要用到以下两个操作:

    • git merge (合并分支所有commit)
    • git cherry-pick (有选择的合并其他分支的commit)
    image.png

    假设现在的git log拓扑如下:

    a - b - c - d   master
             \
               e - f - g dev
    

    接下来想要将其中dev branch的 f应用于master branch.

    git checkout master
    git cherry-pick f
    

    执行以上命令后,目前的拓扑如下

     a - b - c - d - f   master
             \
               e - f - g dev
    

    其他操作:

    • 转移最后一次commit
    git cherry-pick dev
    
    • 转移多个commit
    #合并e和f,尖括号内为e和f两次commit对应的hash值
    git cherry-pick <hash of e> <hash of f>
    
    • 转移连续的多个commit
    # 转移e到f所有commit,不包含e
    git cherry-pick e..f 
    #转移e到f所有commit,包含e
    git cherry-pick e^..f
    

    git cherry-pick命令的帮助信息:

    $ git cherry-pick -h
    usage: git cherry-pick [<options>] <commit-ish>...
       or: git cherry-pick <subcommand>
    
        --quit                退出但是不恢复到操作前的状态
        --continue            解决完冲突后继续执行
        --abort               发生冲突后,放弃合并,恢复操作前的状态
        -n, --no-commit      更新工作区和暂存区,不自动产生新的提交。
        -e, --edit            编辑commit信息
        -s, --signoff         添加提交者的签名信息
        -m, --mainline <parent-number>
                              select mainline parent
        --rerere-autoupdate   update the index with reused conflict resolution if possible
        --strategy <strategy>
                              merge strategy
        -X, --strategy-option <option>
                              option for merge strategy
        -S, --gpg-sign[=<key-id>]
                              GPG sign commit
        -x                    在末尾添加一行(cherry picked from commit ...)以便记录此commit是cherry pick得到的
        --ff                  allow fast-forward
        --allow-empty         preserve initially empty commits
        --allow-empty-message
                              allow commits with empty messages
        --keep-redundant-commits
                              keep redundant, empty commits
    
    

    如果发生冲突,首先处理冲突的文件,然后执行 git add . (注意后面有个.),将修改后的文件加入暂存区。然后执行:

    git cherry-pick --continue
    

    相关文章

      网友评论

        本文标题:git cherry-pick用法详解

        本文链接:https://www.haomeiwen.com/subject/bazhjktx.html