Git如何拉取某个分支的某段提交
大家讲道理
大家讲道理 2017-05-02 09:32:03
0
5
893

现在有两个分支,分别为分支A、分支B。每个分支上都有若干次提交;
分支A的提交commits有 a1, a2, a3, a4, a5,一共有五次提交;
分支B的提交commits有 b1, b2, b3, b4, b5,一共有五次提交;
当前位于分支A。

注:以上10次提交的hash值不一样。

请问:怎么只拉取分支B的 b2, b3, b4 提交节点到 分支A?

要求,在拉取到分支A后,必须保留原来分支B上的提交信息。

求破 ╮( ̄▽  ̄)╭ ~

@junnplus 说的cherry-pick方法,是对的,而且我也会;但是有没有更好的回答呢?

大家讲道理
大家讲道理

光阴似箭催人老,日月如移越少年。

reply all(5)
迷茫

On branch A, execute

git cherry-pick <commit_id>
黄舟

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.

过去多啦不再A梦

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.

刘奇

Should only be chrry pick

淡淡烟草味

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
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!