これはうまくいきません。 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
のようになります。一方、マスターはd
とe
に進みましたが、誰かがフィーチャーブランチを作成し、c
に基づいてf4
までf1
をコミットしました。 Gitはあなたのブランチがfeature
ではなくmaster
から来たと判断する方法がないので、最高であなたはマージするブランチの選択肢を持っています。
これを自動的に行うには、最短のブランチ、最長のブランチ、最も多い/最小のコミットを持つブランチ、またはそれ以外のものを選択するヒューリスティックを適用する必要があります。当然、多くのオプションがあるので、git
組み込み関数のための実際の選択ではありません。しかし、gitの "plumbing"関数を使って、あなた自身で書くことができます:
#!/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。com/questions/3161204/find-the-parent-of-a-branch) – pisaruk