search

Home  >  Q&A  >  body text

How does Git pull a certain commit of a certain branch?

There are now two branches, namely branch A and branch B. There are several submissions on each branch;
The commits of branch A include a1, a2, a3, a4, a5, a total of five submissions;
The commits of branch B include b1, b2, b3, b4, b5, there are five submissions in total;
is currently on branch A.

Note: The hash values ​​of the above 10 submissions are different.

Excuse me: How can I only pull the b2, b3, and b4 commit nodes of branch B to branch A?

Requires that after pulling to branch A, the original commit information on branch B must be retained.

Please break ╮( ̄▽  ̄)╭ ~

The cherry-pick method mentioned by @junnplus is correct, and I can do it too; but is there a better answer?

大家讲道理大家讲道理2794 days ago980

reply all(5)I'll reply

  • 迷茫

    迷茫2017-05-02 09:34:03

    On branch A, execute

    git cherry-pick <commit_id>

    reply
    0
  • 黄舟

    黄舟2017-05-02 09:34:03

    If automation is not required, the most convenient way is this:

    $ git rebase -i HEAD $name_of_branch_b

    The editor will display five lines from b1 to b5, delete the two lines b1 and b5, save and exit.

    reply
    0
  • 过去多啦不再A梦

    过去多啦不再A梦2017-05-02 09:34:03

    It may be difficult to have a better answer, right? According to the git-flow的原则,dev-feature一般不会有只合并中间几次提交到devdivided situation, maybe the subject's need is based on unreasonable version management strategy or very special scenarios.

    reply
    0
  • 怪我咯

    怪我咯2017-05-02 09:34:03

    Should only be chrry pick

    reply
    0
  • 淡淡烟草味

    淡淡烟草味2017-05-02 09:34:03

    Everyone is right, there is no good way; but I still have this special need in my actual work process.
    Tonight I used my poor shell programming skills to write a program that can achieve my needs, and it can be achieved through cherry-pick.

    This small program can fulfill my needs as long as it is executed as follows:

    # b5省略时取到最新的节点
    cherry-picks B b2 b5

    Next is my debut: github link

    #!/usr/bin/env bash
    
    # 初始化
    targetBranch=
    start=
    end= #如果没有这读到最新
    currentBranch=$(git symbolic-ref HEAD 2>/dev/null | cut -d"/" -f 3)
    
    # 切换到目标分支
    git checkout $targetBranch
    
    echo start proccess commit message...
    
    rawList=$(git log | grep '^commit' | sed '1,$s/commit//')
    
    startIndex=$(echo "$rawList" | sed -n "/$start/=" )
    
    # 如果end不为空,取得结束的偏移量,否则默认为1,也就是最新
    if [ -n "$end" ]; then
        endIndex=$(echo "$rawList" | sed -n "/$end/=")
    else
        endIndex=1
    fi
    
    # 取得需要cherry-pick的区间
    list=$(echo "$rawList" | head -$startIndex | tail +$endIndex)
    
    ## 倒序
    list=$(echo "$list" |sed '1!G;h;$!d')
    
    
    echo '待cherry-pick的有:'
    echo list:
    echo "$list"
    echo
    
    # 切换回当前分支
    git checkout $currentBranch
    
    for i in "$list"; do
        git cherry-pick $i
    done
    
    echo '完成'
    exit 0

    reply
    0
  • Cancelreply