这很难做好。在git中,分支只是一个自动前进的指向提交的指针,而提交可以有任意数量的分支名称。考虑这种情况下:
your master: y1---y2---y3
/
master: a---b---c---d---e
\
feature: f1---f2---f3---f4
你签出分支 “大师” 在c
,并承诺y1
,y2
和y3
。您的历史看起来像a b c y1 y2 y3
。与此同时,master已提前到d
和e
,但有人创建了功能分支,并根据c
提交了f1
到f4
。 Git没有办法确定你的分支来自master
而不是feature
,所以最好你可以选择分支来合并。
如果你自动做到这一点,你将不得不应用启发式方法来选择最短的分支或最长的分支,或者提供最多/最少提交或其他类似的方法。当然,由于有很多选择,对于内置功能来说这不是一个好的选择。但是,使用git的“管道”功能,你可以自己写:
#!/bin/bash
# Tries to determine a good merge base from among all local branches.
# Here used, a "good" merge base is the one sharing the most recent commit
# on this branch. This function will exit 1 if no branch is found,
# or exit 2 in a tie.
#
# Untested - use at your own risk.
MAX_SEARCH=20 # only search the last 20 commits on this branch
FOUND=0
LAST_BRANCH=
# iterate through the commits, most recent first
for COMMIT in $(git rev-list --max-count=$MAX_SEARCH HEAD); do
# check every local branch
for BRANCH in $(git for-each-ref --format="%(refname)" refs/heads); do
# look for the commit in that branch's history
if (git rev-list $BRANCH | fgrep -q COMMIT); then
echo $BRANCH
FOUND=$((FOUND + 1))
LAST_BRANCH="$BRANCH"
fi
done
if [ $FOUND -gt 1 ]; then
# more than one choice; exit
exit 2
elif [ $FOUND -eq 1 ]; then
git merge $LAST_BRANCH
exit 0
fi
done
exit 1 # could not find a parent
[查找分支的父分支](http:// stackoverflow。3161204 /找最父分支对的一分支COM /问题/) – pisaruk