Merge, update, and pull Git branches without using checkouts

前端 未结 17 1412
臣服心动
臣服心动 2020-11-22 07:42

I work on a project that has 2 branches, A and B. I typically work on branch A, and merge stuff from branch B. For the merging, I would typically do:

git mer         


        
17条回答
  •  栀梦
    栀梦 (楼主)
    2020-11-22 08:15

    If you want to keep the same tree as one of the branch you want to merge (ie. not a real "merge"), you can do it like this.

    # Check if you can fast-forward
    if git merge-base --is-ancestor a b; then
        git update-ref refs/heads/a refs/heads/b
        exit
    fi
    
    # Else, create a "merge" commit
    commit="$(git commit-tree -p a -p b -m "merge b into a" "$(git show -s --pretty=format:%T b)")"
    # And update the branch to point to that commit
    git update-ref refs/heads/a "$commit"
    

提交回复
热议问题