Questo è difficile da fare bene. In git, un ramo è solo un puntatore che avanza automaticamente su un commit e un commit può avere qualsiasi numero di nomi di ramo su di esso. Consideriamo questo caso:
your master: y1---y2---y3
/
master: a---b---c---d---e
\
feature: f1---f2---f3---f4
hai estratto ramo "master" a c
, e impegnata y1
, y2
e y3
. La tua cronologia assomiglia quindi a a b c y1 y2 y3
. Nel frattempo il master è avanzato a d
e e
, ma qualcuno ha creato un ramo di funzionalità e ha impegnato f1
tramite f4
in base a c
. Git non ha modo di stabilire che il tuo ramo provenga da master
anziché da feature
, quindi nella migliore delle ipotesi avrai una scelta di filiali da unire.
Se lo facessi automaticamente, dovresti applicare un'euristica per scegliere il ramo più corto, o il ramo più lungo, o quello con il più/minimo commit, o qualcos'altro come quello. Naturalmente, poiché ci sono così tante opzioni, non è davvero una buona scelta per una funzione integrata git
. Tuttavia, utilizzando le funzioni di "idraulico" di Git si può scrivere il proprio:
#!/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
fonte
2012-11-01 17:01:18
possibile duplicato [Trova ramo genitore di un ramo] (http: // StackOverflow.com/domande/3161204/find-the-genitore-ramo-of-a-branch) – pisaruk